当 SimpleCursorAdapter 将值从 table 添加到 listView 时检查列值

Checking column value when SimpleCursorAdapter is adding values from table to listView

在我的 table 中,我的列名 "FAVOURITE" 的值为 0 或 1 (false/true)。当我将 table 中的值添加到 listView 时,我想检查 FAVOURITE 列中的值,如果值为 1 或为空 space,则添加心形表情符号如果值为 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);
}

以及此 class 的完整代码:

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 来做到这一点? 凹凸

我在这个postWhosebug

中无意中找到了解决方案

我的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);
    }
}