Getting the _id of the rows in ListVew right - android

I have a ListView which shows a bunsh of values from a SQLite table. First I used a SimpleCursorAdapter to fill the ListView based on a Cursor from an SQL query. I switched over to using a SimpleAdapter in stead because I had to manipulate/add data in the list before sending it over to the ListView.
Using the SimpleCursorAdapter the id returned from the ListView after tapping a row is the correct ID from the database table, but using a SimpleAdapter the id looks like its just generated by the ListView because it is the same as the position.
My table looks like this:
_id | col1 | col2 | col3
The method producing the cursor for the SimpleCursorAdapter looks like this:
public Cursor fetchDataAsCursor()
{
return db.query("table_name", new String[] { "_id", "col1", "col2"}, null, null, null, null, null);
}
The method filling in the ListView using SimpleCursorAdapter looks like this:
private void simpleFillData()
{
Cursor cursor = dbAdapter.fetchDataAsCursor();
startManagingCursor(cursor);
String[] from = new String[] {"col1", "col2"};
int[] to = new int[] {R.id.col1, R.id.col2};
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.list_row, cursor, from, to);
setListAdapter(notes);
}
This works fine as the id returned is ok in the following method:
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, DetailActivity.class);
i.putExtra("_id", id);
startActivityForResult(i, ACTIVITY_EDIT);
}
Now switching over to the SimpleAdapter.
The code for producing the List:
public ArrayList<HashMap<String, Object>> getList()
{
ArrayList <HashMap<String, Object>> list = new ArrayList();
c = fetchDataAsCursor();
c.moveToFirst();
for(int i = 0; i < c.getCount(); i++)
{
HashMap<String, Object> h = new HashMap<String, Object>();
h.put("_id", c.getLong(0));
h.put("col1", c.getString(1));
h.put("col2", c.getString(2));
//This is the extra column
h.put("extra", calculateSomeStuff(c.getString(1), c.getString(2));
list.add(h);
c.moveToNext();
}
return list;
}
And then for the method which fills the ListView:
private void fillData()
{
ArrayList<HashMap<String, Object>> list = dbAdapter.getList();
String[] from = new String[] {"col1", "col2", "extra"};
int[] to = new int[] {R.id.col1, R.id.col2, R.id.extra};
SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.list_row, from, to);
setListAdapter(notes);
}
In this last method the ListView failes to pick up the _id value in the list. I would have guessed that it would do this automaticly as it does when using a SimpleCursorAdapter
Is there a way to manipulate the id of a row in a ListView to be sure that it has the same value as the _id key in the database table?
(All code examples is greatly simplified)
Edit:
I figured it out. I had to make my own subclass of SimpleAdapter which overrides public long getItemId(int position)
public class MyListAdapter extends SimpleAdapter
{
private final String ID = "_id";
public PunchListAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
}
#Override
public long getItemId(int position)
{
Object o = getItem(position);
long id = position;
if(o instanceof Map)
{
Map m = (Map)o;
if(m.containsKey(ID))
{
o = m.get(ID);
if(o instanceof Long)
id = (Long)o;
}
}
return id;
}
}

That is bad way to work with cursor by using SimpleAdapter. You should implement CursorAdapter.
public class MyCursorAdapter extends CursorAdapter
{
LayoutInflater inflater;
public MyCursorAdapter(Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
//cursor is already setted to requared position, just get your column
TextView tv1 = (TextView)view.findViewById(R.id.textView1);
TextView tv2 = (TextView)view.findViewById(R.id.textView2);
tv1.setText(cursor.getString(1));
tv2.setText(cursor.getString(2));
viev.addOnClickListener(new OnClickListener{
public void onClick(View v){
...
cursor.getLong(0);
...
}
});
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.my_raw_view, parent, false);
}
}
Than just set adapter to listview in your activity.
Cursor cursor = fetchDataAsCursor();
ListView myListView = (ListView)findViewById(R.id.my_list_view);
myListView.setAdapter(new MyCursorAdapter(this,cursot));

Related

Custom ListView from SQLite Cursor

My list contains elements with name and status. The name is a String and the status is an int.
I want to have a ListView where each element shows the name and a drop-down for selecting the status. Also, the background color will change based on the status int.
I have the Cursor:
String[] ItemsData= { "id", "name", "status" };
Cursor ItemsCursor = ItemsDatabase.query(
Database.ITEMS_TABLE, ItemsData, null, null, null, null, null
);
ItemsCursor.moveToFirst();
and I have created the list (programmatically is how I must do it):
ListView ItemsListView = new ListView(this);
Now, I know that I can use and Adapter (like SimpleCursorAdapter) but I don't know how to do it, taking into account that the name is a String inside a TextView and the status is a Spinner with predefined values saved as int in the database, with the default value being the one corresponding to the status. Also, the status changes the background color, too.
Maybe I can't use an Adapter. Also, I don't know how to include a Spinner inside each list element, if possible.
Here is how i do it :
listV = (ListView) findViewById(R.id.viewTest);
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
int i = 1;
String value = null;
HashMap<String, String> map;
while ((value = database.getStuff(i)) != null) {
map = new HashMap<String, String>();
map.put("title", value);
map.put("desc", "RANDOM TEXT");
map.put("img", String.valueOf(R.drawable.images));
listItem.add(map);
i++;
}
SimpleAdapter s = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list_content,
new String[] {"img", "title", "desc"}, new int[] {R.id.img, R.id.stuff, R.id.stuff});
listV.setAdapter(s);
And here is where i deal with the cursors :
public String getStuff(int id) {
Cursor c = db.rawQuery("SELECT value FROM test WHERE _id = " +id, null);
return cursorToString(c);
}
private String cursorToString(Cursor c) {
if (c.getCount() == 0)
return null;
c.moveToFirst();
String s = c.getString(0);
c.close();
return s;
}
The "R.layout.list_content" is the .xml file used to setup the shape of each item of your list, you can add images TextView and everything else you want !
Just tell me if you want more infos about the .xml stuff
You can use a CursorAdapter to build the rows from any layout you want.
public class CustomAdapter extends CursorAdapter {
public CustomAdapter(Context context, Cursor c) {
super(context, c, false);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.row, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// Populate view with the cursor, add listeners...
}
}

How to display more than one column data from the same table on ListView?

My ListView currently is able to display only one column data. I want it to display two column data.
Here is the code for my ListView:
public class ProjectExplorer extends ListActivity {
private projectdatabase database;
protected Cursor cursor;
protected ListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = new projectdatabase(ProjectExplorer.this);
getinfo();
}
private void getinfo() {
database.open();
cursor = database.getDataforDisplay();
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] {"project_name"}, new int[] { android.R.id.text1 }, 0);
setListAdapter(adapter);
}
#Override
public void onListItemClick(ListView parent, View view, int position, long id) {
super.onListItemClick(parent, view, position, id);
Cursor c = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
c.moveToPosition(position);
// get project name here
String str_projectname= c.getString(c.getColumnIndex("project_name"));
Toast.makeText(this, str_projectname, Toast.LENGTH_LONG).show();
}
Here is the method in database class which return the cursor:
public Cursor getDataforDisplay () {
String[] columns = new String[] {KEY_ROWID, PROJECT_NAME, PROJECT_FINISH_DATE, PROJECT_DIFFICULTY, PROJECT_STATUS};
Cursor c = projectDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
projectDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
c.moveToFirst();
return c;
}
I also want to display KEY_ROWID which is defined as _id
You can use android.R.layout.simple_list_item_2 specifying column no.2 from table from where data can be retrieved and specifies location with help of it can be displayed on Activity - android.R.id.text2
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, new String[] {"project_name","column_no2"}, new int[] { android.R.id.text1, andriod.R.id.text2 }, 0);
You can also create your on custom Adapter specifying your own Listview and creating it from scratch and customized everything.
The best way to achieve this at least in my opinion is to create custom Adapter for your ListView in that case you can set your own design how single listview element should look like . Just populate some arrays using your Cursor and set them to your custom adapter.

simple cursor adapter query

I have created a SimpleCursorAdapter. I would like a way to compare two rows.
lets say the columns are called realData pracData. If i want to compare two columns how would I write the statement to achieve this.
Below is my code for my SimpleCursorAdapter
db = new DBAdapter(this);
db.open();
// db.insertContact("how do you print hello world", "print 'hello world';", "print hello", "hello", 1);
// db.insertContact("what is x=2 and y=5", "5", "7", "2", 2);
db.close();
/*
* Open the same SQLite database
* and read all it's content.
*/
db = new DBAdapter(this);
db.open();
Cursor cursor = db.getAllContacts();
startManagingCursor(cursor);
String[] from = new String[]{DBAdapter.question, DBAdapter.possibleAnsOne};
int[] to = new int[]{R.id.label, R.id.TextView01};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
db.close();
This is from the ArrayAdapter
if (s.startsWith("how do you print hello world") || s.startsWith("iPhone")
|| s.startsWith("Solaris")) {
imageView.setImageResource(R.drawable.plus);
I want to do something similar but compare two rows in my database.
public class MyAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
public MyAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
// TODO Auto-generated constructor stub
}
#Override
public void bindView(View v, Context context, Cursor c) {
// do your magic inhere :) the cursor will be at the correct row position
System.out.println("please select a cursor");
// int nameCol = c.getColumnIndex(People.NAME);
// ListView listContent = (ListView)findViewById(R.id.contentlist);
// ImageView imageView = (ImageView)findViewById(R.id.ImageView01);
int nameCol = c.getColumnIndex(DBAdapter.question);
System.out.println("What is the column index" +nameCol);
}
}
Extending the SimpleCursorAdapter and overriding bindView should work. I usually extend CursorAdapter, but try something à la
public class MyAdapter extends SimpleCursorAdapter {
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView label = view.findViewById(R.id.yourid);
// and then do something with cursor.getString(columnindex);
}
}

Android: adding an extra String to TextView which populates its content from Database

I have a listview which populates its content from SQLite Database.
Here's my code:
ListView listView = (ListView) findViewById(R.id.lstText);
listView.setOnItemClickListener(this);
listView.setAdapter(new MySimpleCursorAdapter(this, R.layout.listitems,
managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
Database.Project.NAME), new String[] { BaseColumns._ID,
Database.Project.C_PROJECTTITLE,
Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE}, null, null, null),
new String[] { Database.Project.C_PROJECTTITLE,
Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE}, new int[] {
R.id.txt_title, R.id.image, R.id.txt_list_desc, R.id.txt_org}));
I want to put an extra String to some TextViews above when its displayed on the list. For example, I want to add a String with the word "from" on R.id.txt_org, before the populated String from the database which is Database.Project.C_ORGANIZATIONTITLE
Let's say the populated String is: New Organisation,
with an extra String "from" what will be displayed is: from New Organisation
Can anybody help me with that? Thank you very much.
EDITED:
FYI, this is my SimpleCursorAdapter method:
class MySimpleCursorAdapter extends SimpleCursorAdapter {
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
loader = new ImageLoader(context);
this.context = context;
}
Context context=null;
ImageLoader loader = null;
public void setViewImage(ImageView v, String value) {
v.setTag(value);
loader.DisplayImage(value, context, v);
}
}
Since you're already using a custom adapter, override the adapter's bindView() and newView() methods, rather than getView(). That way you will not have to manually deal with recycling the row's view.
Within these method you can get the data from the resulting Cursor and manipulate it before binding it to your row's view.
GetView Vs. BindView in a custom CursorAdapter?
How to override CursorAdapter bindView
//Edit: some more code below. Note that this is just a rough outline and by no means complete or tested.
class MySimpleCursorAdapter extends SimpleCursorAdapter {
private ImageLoader mLoader = null;
private LayoutInflater mInflater = null;
private int mBusinessNameIndex = -1;
private int mSmallImageIndex = -1;
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
mLoader = new ImageLoader(context);
mInflater = getLayoutInflater();
mBusinessNameIndex = c.getColumnIndexOrThrow(Database.Project.NAME);
mSmallImageIndex = c.getColumnIndexOrThrow(Database.Project.C_SMALLIMAGE);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.row, null);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// Get your views from 'view'
TextView someTextView = (TextView) view.findViewById(R.id.xxx);
ImageView someImageView = (ImageView) view.findViewById(R.id.yyy);
// Set the data
someTextView.setText("from " + cursor.getString(mBusinessNameIndex));
mLoader.DisplayImage(cursor.getString(mSmallImageIndex ), context, someImageView);
}
}

Make listview from SQLite database

I'm trying to populate listview from my SQLite database... this is how I get my data from database:
Cursor c = database.rawQuery("SELECT * FROM " + TableName, null);
int Column1 = c.getColumnIndex("uri");
int Column2 = c.getColumnIndex("file");
int Column3 = c.getColumnIndex("id");
c.moveToFirst();
if (c != null) {
do {
String uri = c.getString(Column1);
String file = c.getString(Column2);
int id = c.getInt(Column3);
} while (c.moveToNext());
}
I would normally add an array to listview like that:
ListView my_listview2 = (ListView) findViewById(R.id.listView1);
String my_array[] = {"Android", "iPhone"};
my_listview2.setAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.my_custom_row, my_array));
How can I make an array to setadapter from my sql query?
The best way to do this is to use a CursorAdapter or a SimpleCursorAdapter. This will give you the best performance and once you figure it out you'll find it's the simplest approach when using a SQLite db.
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
Below is a simple CustomCursorAdapter that I use frequently. Just add the CustomCursorAdapter class as an inner class.
protected class CustomCursorAdapter extends SimpleCursorAdapter {
private int layout;
private LayoutInflater inflater;
private Context context;
public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.i("NewView", newViewCount.toString());
View v = inflater.inflate(R.layout.list_cell, parent, false);
return v;
}
#Override
public void bindView(View v, Context context, Cursor c) {
//1 is the column where you're getting your data from
String name = c.getString(1);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.textView);
if (name_text != null) {
name_text.setText(name);
}
}
Create an instance of the CustomCursorAdapter like so...
You'll need to create your cursor just like you're already doing.
protected String[] from;
protected int[] to;
//From is the column name in your cursor where you're getting the data
//to is the id of the view it will map to
from = new String[]{"name"};
to = new int[]{R.id.textView};
CustomCursorAdapter adapter = new CustomCursorAdapter(this, R.layout.list, cursor, from, to);
listView.setAdapter(adapter);
I found working with the notepad tutorial very useful for learning about this.
It shows you how to implement the listview using the sqlite database in very easy steps.
http://developer.android.com/resources/tutorials/notepad/index.html

Categories

Resources