Begginner working with ListAdapters / SQLite Database
My programming knowledge is slim, i discovered Java and the Android SDK some days ago.
I have a SQLiteDatabase, a SQLiteOpenHelper, and a Cursor working properly ( means i think i understood how to create/open a DB using a SQLiteOpenHelper, make queries on my DB and get a cursor )
currently, i'm wrapping i + labName + labCity into a single results String, and display into a single TextView R.id.label
Is it possible to bind labName to R.id.label, and bind labCity to another TextView R.id.label2 ?
Here is the code of my ListActivity i need help with :
public class MainLabList extends ListActivity implements OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_lab_list);
// DB
GestionDB gdb = new GestionDB(this, GestionDB.DATABASE_NAME, null, GestionDB.DATABASE_VERSION);
SQLiteDatabase theDB = gdb.getWritableDatabase();
// Cursor
String[] columns = {GestionDB.LAB_NAME, GestionDB.LAB_ADDRESS_CITY};
Cursor c =
theDB.query(GestionDB.TABLE_NAME, columns, null, null, null, null, null);
////////
this.setTitle(" " + c.getCount() + " Labs in Local Database");
ArrayList<String> results = new ArrayList<String>();
int i = 0;
c.moveToFirst();
/* Loop through all Results */
do {
i++;
String labName = c.getString(c.getColumnIndex(GestionDB.LAB_NAME));
String labCity = c.getString(c.getColumnIndex(GestionDB.LAB_ADDRESS_CITY));
/* Add current Entry to results. */
results.add("" + i + ": " + labName
+ " (" + labCity + ")");
} while (c.moveToNext());
//need rework
ListView lvLabListing = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> labListAdapter = new ArrayAdapter<String>(this,
R.layout.listing, R.id.label, results );
lvLabListing.setAdapter(labListAdapter);
lvLabListing.setOnItemClickListener(this);
//
// don't forget to close the database
gdb.close();
And the listing.xml i would like to modify :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/label"></TextView>
</LinearLayout>
My GestionDB class is simple, just define some columns if the database doesn't exist, along with some variables.
Asked by: Steven952 | Posted: 25-01-2022
Answer 1
You should use a CursorAdapter
, perhaps a SimpleCursorAdapter
, for this. Here is an example.
Similar questions
android - Can I have separate ListAdapters for different TextView items in a ListView?
I'm new here and a fairly newbish developer overall. I have looked online for an answer to my problem but it seems that noone has had such requirement. Perhaps my approach is flawed. Please help me.
I want to have a ListView with multiple TextViews that are populated by three different sets of data. First TV is a title of the item, second a summary and third a reference. The only approach I can think of is three se...
android - How to avoid recycled item issue with ListAdapters?
I believe to be following the standard pattern for avoiding recycled listview data,yet, I fail. As usual, the last two items of the listview (which are below the fold...get data from the other previous positions)
public static class ViewHolder {
public ImageView img1;
public ImageView img2;
public TextView text1;
public TextView text2;
public TextView text3;
public TextView text4;
...
Multiple ListAdapters or a single one, using filtering (Android Performance)
Try to focus on general "dos and donts" regarding lists, e.g. is
adding and removing 30 items of a 200 item list better then clearing
and repopulating? Or any other tips in this area - I cant really try
on my phone, to fast for that :-)
Is there any way to calculate the memory overhead / calculation power of list operations. The background is as following:
I have a list vi...
android - json, listadapters and multiple on click activity for listviews
I am using api calls to pull out a json entry and fill in my list view. The problem I am having is implementing different listeners inside of my list view. The list view looks like this:
The listView is to implement an onItemClick listener and a checkBox listener. Till now I was able to populate the listView and generate the ...
Does one use ListAdapters with Android Room?
In an older app, I used straight SqlLite, built list adapters, had and XML file for the activity and another one for each item.... is there a new "magic" way to do this with Room? Room I have up and running, it can create the tables and stick data in, fine. But what is(are) the proper way(s) to show the data? Is it LiveData or nothing?
For this app, we're talking about displaying small (under 100) items in a scroll...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android