I have a ListView which is implemented using a customs adapter. For making the adapter i am using a holder class. The class has various TextViews and ImageViews as well as an Int variable id to store the id being fetched from the database. Now when i click the particular list i want to get the id so that using it I can further display the information on a new activity. The id is not meant to be displayed in the ListView. How can i get the id from onItemClickListener()
You can set a tag to a View (.setTag()) and then retrieve it, it happens on getView() method inside your custom Adapter
Here is a sample code:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String transactionId = ((TextView) view
.findViewById(R.id.tvTID)).getText().toString();
handler.getTransactionDetails(callback, transactionId);
}
});
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
YourObject selected = adapter.getItem(position);
Then you can send the selected list item (Object) to your new activity
Intent mIntent = new Intent(ThisActivity.this, NewActivity.class);
mIntent.putExtra("list_selected", selected);
startActivity(mIntent);
as well as an Int variable id to store the id being fetched from the
database.
You didn't provide more infomation about that id (i don't know exactly what it presents) but to make it simple you can set this id for each widget in ListAdapter via setTag() method and then simply retrieve it from your onItemClick() method.
Related
I want retrieve the data from the Listview, where my list is having 4 items like, name, date, sem, category.
i want to read the text present in the list view and pass it to another activity.
If you are retrieving your data from server then you need to get item on item click of ListView. Like
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name =urarraylist.get(position).get("name");// here you have to pas keyname which is put inyour Hashmap arraylist
String category =urarraylist.get(position).get("category");
String date=urarraylist.get(position).get("date");
String sem=urarraylist.get(position).get("sem");
Intent i = new Intent(getBaseContext(),anotheractivity.class);
i.putExtra("name", name);
i.putExtra("category ", category );
i.putExtra("date", date);
i.putExtra("sem", sem);
startActivity(i);
}
});
Now retrieve this data in your next Activity on onCreate() method. Like
Intent n = getIntent();
String name = n.getStringExtra("name");
String category = n.getStringExtra("category ");
String date= n.getStringExtra("date");
String sem= n.getStringExtra("sem");
As it's name suggest, a ListView doesn't contains data, it only contains Views. These views are often created from an array or a List via an Adapter. So you have to ask your adapter to retrieve the data.
Unfortunately there is no default method for that, so you'll have to create your own getter, or you can also loop on the getItem() method associated with the getCount() one on your ListView's adapter.
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name=(String)parent.getItemAtPosition(position);
Intent i = new Intent(getBaseContext(),anotheractivity.class);
i.putExtra("USERNAME", name);
startActivity(i);
}
});
if you click the listview row the value from corresponding position will be fetched and passed to intent and further to next activity.
The magic lies here:
String name=(String)parent.getItemAtPosition(position);
I have generated a list of customers. On click this should open edit view to edit the customer. Here the parameter should pass the _id of the row according to stored in the database. But everytime passing it's position in the list. So the edit view is opening wrong data. Please help.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + id);
customerEdit(customerEditUri);
}
});
Answer:
Thank you all. Your comments helped me to solve this. I have created following function inside my CustomerObject class:
#Override
public String toString() {
return this.name;
}
After that created an array of CustomerObject in activity like following:
List<CustomerObject> customers = new ArrayList<CustomerObject>();
Created ArrayAdapter by following:
adapter = new ArrayAdapter<CustomerObject>(this, R.layout.list, R.id.customer_name, customers);
Finally called setOnItemClickListener() like this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
CustomerObject custObj = adapter.getItem(position);
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + custObj.pkid);
customerEdit(customerEditUri);
}
});
You will have to set the ID you would like it to return in the adapter, the List View Adapter that you used to bind data to the ListView.
If I am not wrong, the method is in the adapter class under the following method name:
public long getItemId(int position) {
return myitem[position].getId();
}
Returning the appropriate ID will get you the results you wanted.
I believe that the "long id" is not the record id but the internally generated view id.
If you want to get back to the datasource id then you need to use position and something like:
// Assuming datasource is an ArrayAdapter<Customer>
Customer customer = customerAdapter.getItemAtPosition(position);
// then you can do
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + customer.getId());
customerEdit(customerEditUri);
Replace id with position.
Use
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + position);
customerEdit(customerEditUri);
}
})
In my opinion, through the position, you can get row item with adapter's getItem(position).
So, the position mean the data position in the adapter.
For the id parameter, I think it is a help method, as you know, the data in adapter always is a recorder. general speaking, a recorder should have an id column(something like the database id). when coding, you can get the item through position, then get the item's id(if the item has id). but you can get such "id" directly with "id" parameter.
By the way, if you want use the id parameter, you have to implement the getItemId() method in adapter. the default implement in ArrayAdapter is just return the position.
I try to get the value of a selected Item within a custom adapter on a listview. I try this with following code:
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
View curr = parent.getChildAt((int) id);
TextView c = (TextView)curr.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
At the beginning, if I click, the values are good, but once I scrolled and I click on another Item, I get the wrong value of that clicked item... Any idea what is causing this?
The parameter v is the current row. so use:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
(Or you could use getChildAt(position) but this would be slower.)
Understand you might be able to simplify this more depending on your layout.
Just Small Change is Required
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
Since you are using custom view you need to pass the View argument in your OnItemClickListener then you need to use that value to get the Details of TextViews present in that
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(ListOfAstrologers.this,AstroProForUser.class);
i.putExtra("hello",adapter.getItem(position).getUsername() );
startActivity(i);
}
});
Here username is a string field declared in a class and you must over ride getItem() method in adapter class to get the details of inflated row when you click.
I dont have much experience but I think if you want to pass a variable from an OnClick (which is an anonimous class) to the outside of the onClick and to the other activity, you want to pass it via Intent intent.putExtra... (it's quite easy)
Otherwise you might end up using "public static" variable, which might be a memory leak...
I have this ListView setup:
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long id) {
//
Intent i = new Intent(getApplicationContext(), Rate.class);
startActivity(i);
}
});
setListAdapter(new ArrayAdapter<String>(Items.this, R.layout.list,
items));
It is a standard ListView as you see. Each row has a single TextView populated with the List "items". I am pulling that in from a MySQL Database and outputting through a JSON loop.
I simply want to be able to click on the row, with its single name, and take that name and pass it into the Rate.class activity as a String. How can I do this in code?
Add putExtra to your intent (see below). I am assuming you have the list 'items' as the member of the activity, the arg2 argument is basically the index of the item clicked in the list, which you can use to get the name from the 'items' list.
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long id) {
//
Intent i = new Intent(getApplicationContext(), Rate.class);
i.putExtra("name", items.get(arg2));
startActivity(i);
}
You could create a specific array adapter instance instead of just creating one to pass to the function. Then in the OnItemClick() function, int arg2 is the index in the array adapter that the user selected. You can get the view from the array adapter and getText() from the text view.
One of the things passed to onItemClick is the view that was clicked:
Cast view to the appropriate type and call getText() on it; for example:
final String text = ((TextView)view).getText();
That GridView adapter creates ImageView from a layout.
All images are downloaded from URLs respect to the database item IDs where the ID is got from a JSONArray.
Let say, the view is now showing items with
ID: 1,3,4,7.
As the GridView items are dynamic, the position (starting from 0) cannot really identify my item on the GridView.
Is there any other ways to identify that image from the database item IDs?
public OnItemClickListener ClickListner = new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(),
// ""+position, Toast.LENGTH_SHORT).show();
//Identification code for the item to be added here
Intent view =
new Intent(main.this, View.class);
view.putExtra("ID", id);
//expected to have an ID equal to database item ID
startActivity(view);
}
};
First option (the nice way)
You keep a reference to your adapter (or you get it by calling parent.getAdapter() and then cast it)
In your adapter make sure that you've overridden getItem(position) to return the object you used to fill up your adapter (probably something like return arrayList.getItem(position)if you used BaseAdapter)
On the adapter you call getItem(position) and this will give you the very same object, so you should have all the info you need now
Second option (easy way out)
You can put info in the gridviewitem's view using setTag()
then in onItemClick you call getTag() and there you have your unique id
You can use a POJO class to set the URL and ID of Image in that class and create and ArrayList for the same and passing that to the Adapter class. By, doing this you will bind your ImageView and the ImageID from your database. And, then inside onItemClick() you can simply use
POJO pojo = listview.getAdapter().getitem(position);
int id = pojo.getId();