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....
Related
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);
}}
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"))
}
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);
}
});
I've got a functioning search interface on my app. Now I'm trying to make each search result list item clickable and display in a new activity. I've got them clickable, but I can't figure out how to pass the ListAdapter values (records) into the onItemClick method.
Here's my code so far:
private void showResults(String query) {
Cursor cursor = DBHelper.searchDB(query);
startManagingCursor(cursor);
String[] searchFrom = new String[] { DBAdapter.KEY_MAKE,
DBAdapter.KEY_YEAR, DBAdapter.KEY_MAKE,
DBAdapter.KEY_MODEL };
int[] displayHere = new int[] { R.id.rDateTV, R.id.rYearTV,
R.id.rMakeTV, R.id.rModelTV };
final SimpleCursorAdapter records = new SimpleCursorAdapter(this,
R.layout.record_2, cursor, searchFrom, displayHere);
setListAdapter(records);
DBHelper.close();
// --- Click on list item ---
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//--- here's the problem: how do I pass the ListAdapter values into onItemClick. the below doesn't like .getText()
String sYear = (view.findViewById(R.id.rYearTV)).getText().toString();
String sMake = (view.findViewById(R.id.rMakeTV)).getText().toString();
String sModel = (view.findViewById(R.id.rModelTV)).getText().toString();
// -- then pass these strings to an intent to launch a new activity
}
});
// --- END click on list item ----
}
Any ideas? I'm a rookie here, so if you could show me the code, that would be AWSOME!
In OnItemClickListener.onItemClick call parent.getItemAtPosition(position). That method returns cursor which is set to the given position. Then get your data from that cursor. Don't close cursor inside OnItemClickListener.onItemClick.
for example:
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor)parent.getItemAtPosition(position)
String sMake = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_MAKE));//get data from cursor
...
}
});
is possible get the value of a single row.
In partycular i wont get in a string the value of id in my row of mt list with adapter.
In each row there are value id and label
Thaks a lot and sorry for my english
ListView list;
list = (ListView) findViewById(R.id.listView1);
mSchedule = new SimpleAdapter(getApplicationContext(), result ,R.layout.row_meteo,
new String[]{"id", "label"}, new int[] {R.id.textView1,R.id.textView2});
list.setAdapter(mSchedule);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String idPlace = mSchedule.getItem(position).toString();
Toast.makeText(getApplicationContext()," Cliccato sul luogo :"+idPlace,Toast.LENGTH_LONG).show();
}
}
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view.findViewById(R.id.textView1); //<---------
String str = tv.getText();
}
Replace
String idPlace = mSchedule.getItem(position).toString();
with
String idPlace =((TextView)view.findViewById(R.id.textView1)).getText().toString();