Android getting data out of listview - android

I'm at the moment programming an APP with an listview which is filled with data from an SQLite Table like this:
public void printDatabase(){
Cursor c = handler.getAllRows();
String[] fieldNames = new String[] {DBHandler.COLUMN_WORKER_ID, DBHandler.COLUMN_WORKER_NAME, DBHandler.COLUMN_WORKER_SURNAME, DBHandler.COLUMN_WORKER_COST};
int[] toView = new int[] {R.id.item_worker_id, R.id.item_worker_name, R.id.item_worker_surname, R.id.item_worker_cost};
SimpleCursorAdapter cAdapter;
cAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.worker_items, c, fieldNames, toView, 0);
list = (ListView) findViewById(R.id.worker_listView);
list.setAdapter(cAdapter);
}
Now I'd like to get the data from the listview of an item by clicking the item. I've searched everywhere to find a solution for this and found this:
class ItemListener implements AdapterView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Object o = list.getItemAtPosition(i);
String s = list.toString();
}
}
But all I get from this is the reference to the cursor I've used for filling the listview. What do I need to get the data from the listview?

You are half way to your solution already.
Cursor itemCursor = (Cursor) list.getItemAtPosition(i);
This returns you a cursor pointing to the row that is clicked. You can get the data out of it like:
class ItemListener implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Cursor itemCursor = (Cursor) list.getItemAtPosition(i);
String workerId = itemCursor.getString(itemCursor.getColumnIndex(DBHandler. COLUMN_WORKER_ID));
String workerName = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.COLUMN_WORKER_NAME));
String workerSurname = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.COLUMN_WORKER_SURNAME));
String workerCost = itemCursor.getString(itemCursor.getColumnIndex(DBHandler.COLUMN_WORKER_COST));
}
}

Make the cursor global ;
Cursor cursor ;
In your OnItemClick
class ItemListener implements AdapterView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
cursor.moveToPosition(position);
String Id = cursor.getString(itemCursor.getColumnIndex(DBHandler. COLUMN_WORKER_ID));
}
}

Try this:
class ItemListener implements AdapterView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView textView = (TextView) view.findViewById(R.id.list_content);
String text = textView.getText().toString();
//Log.d("Choosen: " + text);
}}

Related

Android database to listview clicklistener

Trying to get name when click on the list view which connected to database.
Searched for solutions but can't find exactly like this. Trying to solve for long time by myself but failed to do. My code:
public void v() {
Cursor cursor = myDBHandler.getDatabaseCursor();
final String []arr = new String[]{myDBHandler.getColumnName()};
int idView[]=new int[]{R.id.lv_tv2};
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.lvlayout,
cursor, arr, idView);
ListView lv=(ListView)findViewById(R.id.ap_Lv1);
lv.setAdapter(simpleCursorAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor1 = myDBHandler.getDatabaseCursor();
String s = cursor1.getString(cursor1.getColumnIndex(myDBHandler.getColumnName()));
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
cursor1.close();
}
});
}
in MyDBHandler class getColumnName() method is:
public String getColumnName(){
return COLUMN_NAME;
}
mCursor.moveToPosition(position);
String s = mCursor.getString(mCursor.getColumnIndex(myDBHandler.getColumnName()));
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();

get Item from MatrixCursor

I am doing a listView with MatrixCursor but I can get data about item clicked.
See this code:
String[] columnDB = new String[] {"cant", "image"};
final MatrixCursor cursor = new MatrixCursor(columnDB);
cursor.addRow(new Object[] {"0","my image"});
final SimpleCursorAdapter adapter = new SimpleCUrsorAdapter(this,R.layout.item_registro,cursor,desdeEstasColumnas, aEstasVistas, 0);
list.setOnItemClickListerner(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Object item = parent.getItemAtPosition(position);
}
});
Later, How can I found "0" or "my Image"??
http://developer.android.com/reference/android/database/MatrixCursor.html
You can get items from the MatrixCursor by using column names ("cant", "image") or index of the column (note, it will start at 0, as array indices in java start at 0: so 0 or 1
since the onclick gives position, use this postion... so index
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
MatrixCursor matrix = (MatrixCursor)parent;
String image = matrix.getString(position);
Not tried the code, but must be something like that....

How get item from a listView Android?

How can I get a item from a listView 2 items , I am using to fill the listView a database of queries but there do not know how to do that to get the name of the item ?
from = new String[]{manager.CN_NAME,manager.CN_CREDITS};
to = new int[]{android.R.id.text1,android.R.id.text2};
manager = new DataBaseManager(this);
listaMateria = (ListView) findViewById(R.id.listView);
cursor = manager.cargarCursorMateria();
adapter = new SimpleCursorAdapter(this,android.R.layout.two_line_list_item,cursor,from,to,0);
listaMateria.setAdapter(adapter);
listaMateria.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String item = ((TextView)view).getText().toString();
String a = Integer.toString(to[position]);
Toast.makeText(getBaseContext(), a, Toast.LENGTH_LONG).show();
}
});
You could also get the cursor for that row.
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Cursor cursor = (Cursor)adapter.getItem(position);
String value = cursor.getString(cursor.getColumnIndex("column_name"))
}

Trying to get String from ListView

My current problem is I have a ListView I'm using to display my contacts now I'd like to set the onItemClick to grab the name of the contact and set that to a TextView called p2 the code I currently have is:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.setp2);
cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
String[] from = new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
int[] to = new int[]{R.id.id_text,R.id.name_text};
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(setp2.this,R.layout.setp2_layout,cursor,from,to);
ListView list = (ListView)findViewById(R.id.contact_list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
}
So basically I'm unsure of what I need to do in the onItemClick, any help appreciated!
In your onItemClick, you will get the instance of the view associated with that list item.From that view you can get the text associated with it and set it to the p2 TextView. If my understanding of your requirement is correct, the code will be something like this:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
CharSequence name = ((TextView)view.findViewById(R.id.name_text)).getText();
//Set this to your textview in P2
tvP2.setText(name);
}
});
Set up a SimpleCursorAdapter.CursorToStringConverter and set it on the adapter, then use convertToString(Cursor cursor) to get the text in on item click :
(Cursor) simple.getItem(position)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
cursor.moveToPosition(position);
String name = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
p2.setText(name);
}
});

How to get info from ListFragment

I have a ListFragment that is populated using a CursorLoader. Once the ListFragment is populated, I have an OnItemClickListener method in which I want to identify which item from the List Fragment the user chose. How do I do this? I've tried:
String item = getListAdapter().getItem(position);
String item = getListAdapter().getItem(position).toString();
and
String item = (String) ((Fragment) getListAdapter().getItem(position)).getString(0);
where position is an integer passed to the onClickItemListener method like so:
public void onListItemClick(ListView l, View v, int position, long id)
All of these throw Cast Exceptions of one type or another. I'm stumped. This seems like a simple thing to do. For your reference, here's how the List Fragment is populated:
private SimpleCursorAdapter mAdapter;
private static final String[] PROJECTION = new String[] { "_id", "stitchname" };
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Intent myData = getActivity().getIntent();
Bundle info = myData.getExtras();
String[] dataColumns = { "stitchname" };
int[] viewIDs = { R.id.stitchlist1 };
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.stitchlist, null, dataColumns, viewIDs, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, info, (LoaderCallbacks<Cursor>) this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selection = "stitchlevel=?";
String[] selectionArgs = new String[] {args.getString("Level")};
return (Loader<Cursor>) new CursorLoader(getActivity(), STITCHES_URI, PROJECTION, selection, selectionArgs, null);
}
Any suggestions would be most welcome, thanks!
You are overriding the wrong method. You said you are using an ListFragment, so you may have been declaring your CursorAdapter/ArrayAdapter somewhere in your Fragment. In that case, do the following:
class yourFragemt extends ListFragment{
//Member Variable
private SimpleCursorAdapter mAdapter;
....
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Cursor c = (Cursor) mAdapter.getItem(pos);
String value = c.getString(c
.getColumnIndex(ColumnIndex));
}
}
The answer is as follows:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
c.moveToPosition(position);
String item = c.getString(1);
}
I got the answer from the second link provided to me by Georgy Gobozov in a comment, above.

Categories

Resources