Android: ListView og Cursor
Jeg har et problem med at få data fra en cursor ned i et listview...Jeg er kommet frem til følgende kode men det vil bare ikke vise noget resultat:
Cursor cur = dbHelper.myDataBase.query("Item", new String[] {"_id", "Name", "Rank", "Description"}, "Name like '%" + etSearch.getText() + "%'", null, null, null, "Name", "100");
cur.moveToFirst();
if(cur.getCount() == 0)
{
TextView tvItems = (TextView) findViewById(R.id.tvItems);
tvItems.setText("No items matched your query");
}
else if(cur.getCount() == 1)
{
setContentView(R.layout.itemview);
final TextView tvName = (TextView) findViewById(R.id.tvName);
tvName.setText(cur.getString(1));
final TextView tvRank = (TextView) findViewById(R.id.tvRank);
tvRank.setText(cur.getString(2));
final TextView tvDescription = (TextView) findViewById(R.id.tvDescription);
tvDescription.setText(cur.getString(3));
}
else
{
/*final ListView lvItems = (ListView) findViewById(R.id.lvItems);
ListAdapter adapter = lvItems.getAdapter();*/
//Trying to get it to work with an adapter
final ListView lvItems = (ListView) findViewById(R.id.lvItems);
String[] from = new String[]{"Name", "Rank"};
int[] to = new int[] {R.id.tv1, R.id.tv2};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listofitems, cur, from, to);
lvItems.setAdapter(adapter);
}
cur.close();
dbHelper.close();
Desuden har jeg main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<EditText
android:id="@+id/etSearch"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/et_search"
/>
<Button
android:id="@+id/btnSearch"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/btn_search"
android:onClick="SearchForItem"
/>
<TextView
android:id="@+id/tvItems"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text=" "
/>
<ListView
android:id="@+id/lvItems"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
or listofitems.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView
android:id="@+id/tv1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=" "
android:textColor="@color/app_maincolor"
/>
<TextView
android:id="@+id/tv2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=" "
android:textColor="@color/app_maincolor"
/>
</LinearLayout>
Er der nogen der kan komme med et bud på hvorfor der ikke bliver vist noget?