I have a ListView which is created with SimpleCursorAdapter.
The list represents merchants. When someone clicks on a merchant i want to view full details of this particular merchant.
on this list (lv1) im setting a listener
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
//Cursor merchant = (Cursor) adapter.getItem(pos);
Intent merchant = new Intent(v.getContext(), MerchantView.class);
merchant.putExtra("merchantPosition", pos);
startActivity(merchant);
}
});
How should I pass the data to merchant view in most optimal way?
I have static reference to adapter so I guess I could use somehow getItem call (just as in commented out line) and then pass it as putExtra to Merchant. If that is the way to do it how should I use getItem (I tried couple of times but failed to extract data that i want).
P.S Adapter is making sql query earlier to database with columns - ID,NAME,DESCRIPTION,STATUS
Thanks!
What I do in my Apps, is override the SimpleCursorAdapter.setViewBinder() to set the Tag of Views inside the ListView with the ID from the DB and pass this ID to the intent in the setOnItemClickListener(). Check this question which is a similar case to what you want to do
Related
I have GridView which shows a list of thumbnails. After clicking on one of them I'd like to navigate to another activity with detailed view of a corresponding "thing".
How should I store some additional data/identifiers such that on click I can pass it to the detailed activity? By default I only get position and id. I'd like to somehow store my custom identifier which I use to query external services for details.
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent = new Intent(this, MovieDetails.class);
intent.putExtra(MY_ID, <some_extra_ID>);
startActivity(intent);
}
});
EDIT:
For example, I have a dynamic list of dishes with only photos of that dish (using GridView). When user clicks on one of them I want to show a detailed view with a recipe and comments.
For that I need to pass name of that dish to load information in the detailed view.
Actually it is as good as a click listener can get as it passes the view, position and id. However you can improve the data you get by defining a unique item id inside your adapter.
If you are using an ArrayAdapter or SimpleAdapter the id is usually the same as position but for a CursorAdapter or SimpleCursorAdapter the id returns the row id of the table.
Now you can extend you adapter and return the desired id inside the getItemId method:
public class MyAdapter extends ArrayAdapter<MyItem>{
...
#Override
public long getItemId(int position) {
return myItems.get(position).myUniqueId;
}
}
I've an issue with onItemclicklistener.
I'm getting real time data from firebase
What I want is, getting real time orders and when I click any specific order, it will go to another activity where specific details of that specific order should be shown.
How I get this?
Switch Statements will not work in this scenario.
How I get some id that will match in database, so that I can get that particular details.
I tried this method
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String name=(String)adapterView.getItemAtPosition(i);
Intent i=new Intent(order.this,details.class);
i.putExtra("CATEGORY", name);
startActivity(i);
}
});
and in details class
Intent i = new Intent();
String name=i.getStringExtra("CATEGORY");
databaseReference=FirebaseDatabase.getInstance().getReference(Constans.CATEGORIES).child(name);
But it is crashing.
Assuming that the ListView contains the order names and the item on which the user click is the name of the order, to get that name of the order you need to use this code inside onItemClick() method:
String orderName = (String) adapterView.getItemAtPosition(position);
Hope it helps.
You can pass the item_id with firebase notification and store it in the listview adapter.
Later you can pass this id to the new activity by putting in extras.
I am beginning android development and in my sample application I have a ListView which I populate as follows:
ListView listView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getEntityLabels());
listView.setAdapter(adapter);
The getEntityLabels() is a function that returns a list of strings.
In the underlying model, the labels are not unique. Instead each entity has an id field which is unique for every entity.
class Entity{
int id;
String label;
}
When the user clicks on an item in the ListView, I would like to do some processing. So, I added an ItemClickListener as follows:
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
//I would like to get the Id of the Entity, not just the label
}});
The processing depends on the id of the entity. However, as the view in the list is just a text and contains only the label, I cannot access it.
Is there a way to embed some information like id(in my case) with the views or an alternate way to get the ids?
I am aware of the way where I can override the toString() method of Entity to return label and use ArrayAdapter<Entity> instead of ArrayAdapter<String>. However, I am not looking for this. I am looking for a more generic way where I can embed any information with any View, if possible!
You can use tags and assign tag to each view set in your adapter. Then you can read your tag from clicked view in OnClickListener and proceed. As tag can be anything (Object) you like that should solve your problem. See setTag() and related.
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();
First of all I want to know if this is possible. Essentially I have a listview that is being populated programatically with a custom list item view and a custom adapter. This works like a charm but the problem is that I have to sort this list in a specific way once it is populated. The values that are being put into the list are stored in String arrays. The problem is that when I sort the listview it changes the index of where I thought the values would be stored. I want to start a new activity with certain values found in the list view. Is this possible? I could take a string from the list item and can look it up in my arrays storing the values when that item is clicked. I can then start a new intent with extras so that I can pass this new data to the new activity. So, is it possible to extract a string from a list item?
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3){
Object o=lv.getItemAtPosition(position);
UtilityClass u=(UtilityClass)o;
String s=u.getMyData();
Intent edit = new Intent(context, AnotherActivity.class);
edit.putExtra("your_identifier", Integer.toString(position));
startActivity(intent);
}});
If u have implemented onItemClickListner / onItemSelectListner then you can get a callback onItemClicked()/ onItemSelected() from there using the Adapter get the item from selected position. and the same can be sent to another activity using a bundle/extra