In my (first) Android App I have a list of items. Each item is displayed using item.xml in which I have a RelativeLayout. In this layout I defined android:onClick="showItemDetails"
of which the method looks like this:
public void showItemDetails(View view) {
Intent intent = new Intent(this, itemDetails.class);
intent.putExtra("itemId", "5");
this.startActivity(intent);
}
As you can see I pass on a hardcoded id, so whatever item you currently click on, it obviously only shows item nr 5. I now need to dynamically give this method the id. The itemlist is created in the ItemListAdapter class of which a part is this:
holder.description.setText(rowItem.eventDescription);
holder.amount.setText(rowItem.getDisplayAmount());
I also have the id under rowItem.id
, but I'm kinda lost on what I should do with it.
Can anybody help me out a bit here? What do I need to do to have the list items correctly pass the id through to the itemDetails class? All tips are welcome!
First few assumptions:
Fragments
just plain Activity
called OuterActivityClass
rowItem
var has typ RowItemClass
ItemListAdapter
is extending ArrayAdapter<RowItemClass>
ItemListAdapter
do not have own List<RowItemClass>
field, rowItem
in getView(...)
is assigned like rowItem = getItem(possition);
not like rowItem = yourListFieldInAdapter.get(position);
(I do not know why Ppls using such field since ArrayAdapter<?>
already has it internally) ... if you do it "bad" replace all getItem(possition)
occurrence in my answer with yourListFieldInAdapter.get(position)
RowItemClass
is long or int (if not use "or:" version) What you have to do:
android:onClick
from item.xml
hasStableIds()
in ItemListAdapter
: public boolean hasStableIds () { return true; }
public boolean hasStableIds () { return true; }
getItemId(...)
in adapter: public long getItemId (int position) { return getItem(position).id; }
public long getItemId (int position) { return getItem(position).id; }
Activity
onCreate(...)
get ListView: ListView listView = findViewById(R.id.your_listview_id);
code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
void onItemClick(AdapterView<?> parent, View view, int position, long id){
Intent intent = new Intent(OuterActivityClass.this, itemDetails.class);
intent.putExtra("itemId", id + ""); //+ "" to make it string, of course you can use long but you have to remeber to use intent.getLongExtra(...) in details Activity
startActivity(intent);
}
});
or:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
void onItemClick(AdapterView<?> parent, View view, int position, long id){
final RowItemClass clickedItem = (RowItemClass)parent.getItemAtPosition(position);
Intent intent = new Intent(OuterActivityClass.this, itemDetails.class);
intent.putExtra("itemId", clickedItem.id + "");
startActivity(intent);
}
});
with second method you can omit points 2. and 3. (if you are using "bad" way remeber to override getItem
( return yourListFieldInAdapter.get(pos)
) and getCount
( return yourListFieldInAdapter.size()
)in your adapter implementation)
Offtopic hint: do not use lower case as first letter in class name itemDetails
=> ItemDetails
... for your sanity :) (OMG, OMG, itemDetails is variable or class name )
In your Main Activity
String item = country.getName();
Intent intent= new Intent(MainActivityActivity.this,AnotherActivity.class);
intent.putExtra(EXTRA_RESPONSE, item);
startActivity(intent);
In Next Activity
String country = getIntent().getStringExtra(MainActivity.EXTRA_COUNTRY);
TextView textView = (TextView) findViewById(R.id.tvPhone);
textView.setText(country);
If I understand correctly your problem then you might try to set OnClickListener in your adapter(don't set onClick in xml). And in your adapter try something like:
public View getView(int position, View convertView, ViewGroup parent) {
...
View rootLayout = convertView.findViewById(R.id.item_layout_root);
...
rootLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showItemDetails(rowItem.id);
}
});
return convertView;
And your showItemDetails will change to
showItemDetails(int id) {
Intent intent = new Intent(this, itemDetails.class);
intent.putExtra("itemId", id);
this.startActivity(intent);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.