in my table I have column name "FAVOURITE"
which has value 0 or 1 (false/true). When I'm adding the values from table to my listView
I would like to check the value in FAVOURITE
column and add the heart emoji if value is 1 or empty space if value is 0. I'm using SimpleCursorAdapter
to add my data to list view.
Here is a fragment where I'm adding values to 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);
}
And full code of this 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();
}
}
Maybe I should read columns after create list view and check all elements by findViewById(R.id.Ulubione).getText and .setText
but how I can do this with listView
?
Bump
I find the solution by accident in this post stackoverflow
My onResume()
method:
@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);
}
}