当 SimpleCursorAdapter 将值从表添加到 listView 时检查列值
Posted
技术标签:
【中文标题】当 SimpleCursorAdapter 将值从表添加到 listView 时检查列值【英文标题】:Checking column value when SimpleCursorAdapter is adding values from table to listView 【发布时间】:2020-08-25 23:50:23 【问题描述】:在我的表中,我有列名"FAVOURITE"
,其值为 0 或 1(假/真)。当我将表中的值添加到listView
时,我想检查FAVOURITE
列中的值,如果值为1,则添加心形表情符号,如果值为0,则添加空格。我正在使用SimpleCursorAdapter
将我的数据添加到列表视图。
这是我向listView
添加值的片段:
cursor = db.query("SERIAL",new String[]"_id","NAME","SERVICE","SEASON", "CURRENT_EPISODE", "EPISODES", "FAVOURITE",null,null,null,null,null);
if(cursor.moveToFirst())
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.my_custom_list,
cursor,
new String[]"NAME","SERVICE","SEASON", "CURRENT_EPISODE", "EPISODES", "FAVOURITE", //"Ulubione" mean "Favourite"
new int[]R.id.textNazwa,R.id.textSerwis,R.id.Sezon,R.id.aktualnyOdcinek,R.id.iloscOdcinkow, R.id.Ulubione,
0);
listViewSeriale.setAdapter(listAdapter);
以及该类的完整代码:
public class BazaDanych extends AppCompatActivity
private ProjektSQLiteOpenHelper bazaDanych;
private SQLiteDatabase db;
private Cursor cursor;
private ListView listViewSeriale;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baza_danych);
listViewSeriale = (ListView) findViewById(R.id.listViewSeriale);
try
bazaDanych = new ProjektSQLiteOpenHelper(this);
db = bazaDanych.getWritableDatabase();
catch (SQLException e)
Toast toast = Toast.makeText(this, "Baza danych niedostępna", Toast.LENGTH_LONG);
toast.show();
finish();
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> listViewOrganizacje, View view, int position, long id)
Intent intent = new Intent(BazaDanych.this, BazaDanychAktualizacja.class);
intent.putExtra(BazaDanychAktualizacja.EXTRA_ORGANIZACJA_ID, (int) id);
startActivity(intent);
;
listViewSeriale.setOnItemClickListener(itemClickListener);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(BazaDanych.this, BazaDanychDopisz.class);
startActivity(intent);
);
@Override
public void onResume()
super.onResume();
cursor = db.query("SERIAL", new String[]"_id", "NAME", "SERVICE", "SEASON", "CURRENT_EPISODE", "EPISODES", "FAVOURITE", null, null, null, null, null);
if (cursor.moveToFirst())
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.my_custom_list,
cursor,
new String[]"NAME", "SERVICE", "SEASON", "CURRENT_EPISODE", "EPISODES", "FAVOURITE",
new int[]R.id.textNazwa, R.id.textSerwis, R.id.Sezon, R.id.aktualnyOdcinek, R.id.iloscOdcinkow, R.id.Ulubione, //"Ulubione" mean "Favourite"
0);
listViewSeriale.setAdapter(listAdapter);
public void odejmijOdcinekZListy(View view)
TextView serwTest = findViewById(R.id.textSerwis);
Toast toast = Toast.makeText(this, "Odejmij odcinek" + serwTest.getText(), Toast.LENGTH_LONG);
toast.show();
也许我应该在创建列表视图后阅读列并通过findViewById(R.id.Ulubione).getText and .setText
检查所有元素,但我如何使用listView
做到这一点?
撞
【问题讨论】:
【参考方案1】:我偶然在这篇帖子***找到了解决方案
我的onResume()
方法:
@Override
public void onResume()
super.onResume();
cursor = db.query("SERIAL", new String[]"_id", "NAME", "SERVICE", "SEASON", "CURRENT_EPISODE", "EPISODES", "FAVOURITE", null, null, null, null, null);
if (cursor.moveToFirst())
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.my_custom_list,
cursor,
new String[]"NAME", "SERVICE", "SEASON", "CURRENT_EPISODE", "EPISODES", "FAVOURITE",
new int[]R.id.textNazwa, R.id.textSerwis, R.id.Sezon, R.id.aktualnyOdcinek, R.id.iloscOdcinkow, R.id.Ulubione, //"Ulubione" mean "Favourite"
0)
// XDDDDD
@Override
public View getView(int position, View convertView, ViewGroup parent)
View v = super.getView(position, convertView, parent);
TextView tx = (TextView) v.findViewById(R.id.Ulubione);
if (Integer.parseInt(tx.getText().toString()) == 1)
tx.setText("❤️");
else
tx.setText("♡");
return v;
;
listViewSeriale.setAdapter(listAdapter);
【讨论】:
以上是关于当 SimpleCursorAdapter 将值从表添加到 listView 时检查列值的主要内容,如果未能解决你的问题,请参考以下文章
在 Angular 中,当组件不是父/子时,如何将值从一个组件传递到另一个组件?
将值从presentingViewController 传回presentingViewController