Get specific item property on onListItemClick() using custom ArrayAdapter<> - android

I have a ListActivity that displays a list of search results I grab from a webservice off the internet. I parse the XML I receive into an ArrayList<MyObjects> which I then bind to a ListView using my own adapter (as in MyObjectAdapter extends ArrayAdapter<MyObject>).
So then I want the user to be able to click on one of the items in the list. Each item has an identifier which will then be put into an intent and sent to a new activity (which then triggers a new webservice request based on this, downloads the rest of the data). But I don't know how to get this one property of the MyObject that was selected.
Here's the onListItemClick() method as it stands:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String myObjectId;
// I want to get this out of the selected MyObject class here
Intent i = new Intent(this, ViewObject.class);
i.putExtra("identifier_key", myObjectId);
startActivityForResult(i, ACTIVITY_VIEW);
}

If you are using ArrayAdapter you must initialize that adapter with an array, right? So, the better you can do is to use the postition that you get from onListItemClick and take the object from the original array.
For instance:
// somewhere you have this
ArrayList<MyObjects> theItemsYouUsedToInitializeTheArrayAdapter;
// and inside onListItemClick....
String myObjectId = theItemsYouUsedToInitializeTheArrayAdapter.get(position).getObjectId();

try this getIntent().getExtras().getString(key);

Related

Pass selected item data from ListView from current activity to next activity

I have a Custom ListView of products. Each row of listview contains product name, image, price and description. I want to send the listview's selected item's title, price, description and image be passed from current ListView activity to next activity where it is to be displayed. How do I pass data of selected list item in intent?
This is part of code from my java file which displays the list:
private ArrayList<ThemeTourModel> GetThemeTourResults(){
ArrayList<ThemeTourModel> results = new ArrayList<ThemeTourModel>();
ThemeTourModel item_details = new ThemeTourModel();
item_details.settourname("Solo Woman Travellers");
item_details.settourDescription("Women searching the ultimate liberation may discover it in exploring the tourist destinations of world on their own. More freedom and security while traveling for leisurely getaways and adventures.");
item_details.setImageNumber(1);
item_details.setPrice("Rs.6999 - Rs.8999");
results.add(item_details);
return results;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
if(position==0){
Intent i0 = new Intent(this,EnquireActivity.class);
startActivity(i0);
}
You can put the primitive data through the Intent to another activity by intent.putExtra(name,value) and get the values through getIntent().getExtra() on the another Activity. You can find the reference here: http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Bundle)
The other way is to let your item object implement Parcelable to exchange between activities, check this out: Android: How to implement Parcelable to my objects?.
Hope this helps

Getting a particular detail from XML file in android

I am working on a CRM app in android, in which, I am showing details of all contacts in a list view. Now, my requirement is when I click on a particular item in the list, it should only display the details about selected contact, such as, name, address, email, etc.. The data is coming from XML file, which I am parsing using SAX Parser. How can I query a XML to get selected data?
You are filling the ListView using Adapter right? Now you can get the item at the selected view inside the ListView and pass this item to an Activity.
E.g. inside your Adatper class implement the onItemClickListener:
public void onItemClick(AdapterView<?> a, View v, int position, long l) {
// Remembers the selected Index
Data item =getItem(position);
Intent intent = new Intent(getApplicationContext(), DetailedActivity.class);
intent.put("object",item);
startActivity(intent);
}
Note: the item "Data" class should implement the Parsable interface so it can be passed to the Activity in your DetailedActivity onCreate method get that object and update the UI based on its Values.

Implementing OnItemClickListener() for a dynamic GirdView/ListView

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();

passing row data from adapter to new Activity from ListView

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

What is the best way to add an OnClickListener to Endless ListActivity

What is the best way to add an OnClickListener to a ListActivity that's Endless?
I am using cwac-endless adapter for my endless list.
The problem I am encountering is only my first batch of data that populates the list get's the click listener. The new data that get's fetched doesn't get the onclick listener.
I've tried adding a onclick listener in the following manner.
I used ListActivity onListItemClick
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String url = prodList.get(position).getProdUrl();
Log.i("URL",url);
Intent webViewIntent = new Intent(ProductListActivity.this, ProductWebView.class);
webViewIntent.putExtra("URL", url);
startActivity(webViewIntent);
}
UPDATE Rookie mistake, the above code works fine, the problem was that I was referencing the original list that was passed in instead of referencing the list in my custom ArrayAdapter.

Categories

Resources