I can add to a db and list as a listview. When I click a list item using onListItemClick, what statement do I need to get the value?
String[] from = new String[] {NotesDbAdapter.KEY_BODY, NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_NUMBER};
int[] to = new int[] {R.id.toptext, R.id.middletext, R.id.circle};
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row, notesCursor, from, to);
setListAdapter(notes);
//some other code
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
super.onListItemClick(l, v, position, thisID);
**String **resposponseid** = Activity2.getData();**
}
I have responseid for every row unique which I want to use when I click on list position. So how to get responseid when I click on list?
In general, it should something like this. You will need to tailor it to suit your needs.
#Override
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
super.onListItemClick(l, v, position, thisID);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
}
I would suggest you go through a tutorial to learn, especially if you are new to Android and Java. Here's one - Android ListView and ListActivity - Tutorial.
In the onListItemClick method, the parameter int position is exactly what you're looking for. The list is filled by an ArrayAdapter which utilizes some List (let's say an ArrayList) to get Views for each row.
The fact that the View in each row can be pretty complicated (not consist of a single value), leads to the way it is implemented, and you don't get the value itself, but the position in the list. What you need to do to get the value is to query that ArrayList (its get(int index) method) you used to fill the ListView with the position param.
Related
I'm having somewhat of an issue in an application I'm developing.
So, all my tables have _id fields for primary keys, and I use SimpleCursorAdapter to bind them to ListViews and Spinners.
What I wanted to know is how can I make the ListView or Spinner selected item have the same ID as the corresponding row?
The strange thing is that this works with the ContextMenu, which I am using straight of the NotePad example:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
info.id
This ID field IS the same as the RowID on the table, and I can delete items fine, but when I try something like this:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg) {
Intent i = new Intent(getApplicationContext(),
FuelingList.class);
long id = view.getId();
The ID field is some random rubberish.
So my question is, in the first code bit, what Id is the AdapterContextMenuInfo getting and how can I retrieve it in other parts of my code?
Since you are using a SimpleCursorAdapter, the onItemClick is passing the database row id in to you..
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg)
The long arg part is actually your row id from the database.
So your code should be:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg) {
Intent i = new Intent(getApplicationContext(),
FuelingList.class);
long id = arg;
Since you know you are using a SimpleCursorAdapter, then in your onItemClick method, you can call
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
String value = ((SimpleCursorAdapter) adapter.getAdapter()).getCursor().getString(COLUMN_INDEX);
}
Where COLUMN_INDEX in the column that you want to fetch from the currently select row in the cursor.
Alternately, calling
adapter.getAdapter().getItem(position)
and then casting that to a Cursor, works as well.
NOTE 1: The AdapterView is actually your ListView.
NOTE 2: Since you using a SimpleCursorAdapter then getItem(position) on the Adapter returns the Cursor positioned at the row your specifed
NOTE 3: When you have Cursor, you can fetch data by providing the column index (0 based) or by using cursor.getString(cursor.getColumnIndex("COLUMN_NAME"))
NOTE 4: As per Barak's comment the arg parameter is the value from the _ID field when using a CursorAdapter, if all you need to know is the row _ID, then just use the arg value.
Can you store the ids of rows in an arraylist and take the id of the row from corresponding position of arraylist. This worked for me..
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent intent=new Intent(this , Details.class);
Bundle extras = new Bundle();
extras.putLong("ID", IDList.get(position));
intent.putExtras(extras);
startActivityForResult(intent, SHOW_DETAILS);
}
You can use listview.setonitenclicklistener... Hope it helps
I have a ListView that I'm populating with a CursorAdapter like this:
SimpleCursorAdapter.ViewBinder viewBinder = new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == cursor.getColumnIndex(MyTableColumns._ID))
{
view.setTag(cursor.getInt(columnIndex));
}
// some other stuff
}
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.my_item_renderer, cursor, from, to);
adapter.setViewBinder(viewBinder);
The aim is to get the ID from the list item clicked:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Object obj = v.getTag();
int myId = Integer.parseInt(obj.toString());
}
However this is always returning null. What am I overlooking? For now I'm just using a hidden text field but I'd like to know what I was doing wrong.
onListItemClick() provides you with a view that is the row in the list. ViewBinder binds values to the TextViews inside this row. Thus the view you call setTag() on is not the same as the view you call getTag() on.
You can either extend SimpleCursorAdapter so you can call setTag() on the correct view, or you can get the first child view of v in onListItemClick() and get the tag of that.
Have you tried this when setting your tag?
view.setTag(new Integer(cursor.getInt(columnIndex)));
Maybe your "if" statement simply returns false and thus no Tag value gets set?
In my project I use a custom adapter to define the items of a ListView. So I want to define special behavior of item in ListView, which will be depend on the value of the field in adapter. Like this :
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
So, here, in this method I want to get access to the element of adapter to get the value from it, number of which is defined by the int position. So, what is code to do this?
you can simply cast it if you're confident enough it is always your adapter that is used:
protected void onListItemClick(ListView l, View v, int position, long id) {
((MyAdapter) l.getAdapter()).myMethod(position);
}
If you are defining an custom adapter you can save a reference to the adapter in the activity that registers the onClick behaviour. You now can call getItem on the Adapter, if you have implemented the getItem method properly.
You can get selected item(object) from list. Lets say your custom adapter containing Restaurent object per row, and your listView name foodJntListView, then
Restaurent rest= (Restaurent) foodJntListView.getSelectedItem();
will give you the selected item(object).
I am using a custom list view. I have tried to get the value of the row which the user clicked.
Can anybody tell me how to get the value?
Thanks
You may want to implement a new method in your class, specifically onItemClick:
[...]
private ListView lv;
[...]
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
String itemValue = lv.getItemAtPosition(position);
[... do something ...]
}
Then you can do whatever you want with with itemValue.
Hope this helps.
I have a ListView that gets filled from a cursor (using rawQuery) that I need to get the text of the selected item from on click.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent mViewChaptersIntent = new Intent(this, ViewWeb.class);
String s = ((TextView) l.getItemAtPosition(position)).getText().toString(); // Tried this, didn't work.
mViewChaptersIntent.putExtra("extension", s);
mViewChaptersIntent.putExtra("itmClicked", String.format("%d", id));
startActivity(mViewChaptersIntent);
}
But I'm not sure about the right way to do it. The getItemAtPosition that I've seen in other posts doesn't seem to work...
getItemAtPosition() should return you a Cursor that is positioned at the specified row. You will then need to call getString() to retrieve whatever column you are looking for.