[英]How to make Android list item pass id on to next view?
In my (first) Android App I have a list of items. 在我的(第一个)Android应用中,我有一个项目列表。 Each item is displayed using item.xml in which I have a RelativeLayout. 每个项目都使用item.xml显示,其中我有一个RelativeLayout。 In this layout I defined android:onClick="showItemDetails"
of which the method looks like this: 在此布局中,我定义了android:onClick="showItemDetails"
,其方法如下所示:
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. 如您所见,我传递了一个硬编码的ID,因此无论您当前单击什么项目,它显然仅显示项目5。我现在需要动态地将此方法指定为ID。 The itemlist is created in the ItemListAdapter class of which a part is this: itemlist是在ItemListAdapter类中创建的,该类的一部分是:
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. 我在rowItem.id
下也有id,但是我有点不知道该怎么办。
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? 我需要怎么做才能使列表项正确地将ID传递给itemDetails类? All tips are welcome! 欢迎所有提示!
First few assumptions: 前几个假设:
Fragments
just plain Activity
called OuterActivityClass
您不使用Fragments
只是称为OuterActivityClass
普通Activity
rowItem
var has typ RowItemClass
rowItem
var具有典型的RowItemClass
ItemListAdapter
is extending ArrayAdapter<RowItemClass>
您的ItemListAdapter
扩展了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)
您做到了“正确”( ItemListAdapter
没有自己的List<RowItemClass>
字段, rowItem
getView(...)
中的rowItem = getItem(possition);
分配为rowItem = getItem(possition);
不像rowItem = yourListFieldInAdapter.get(position);
(我这样做不知道为什么为什么Ppls使用这样的字段,因为ArrayAdapter<?>
已经在内部拥有它了)...如果您这样做“不好”,请用yourListFieldInAdapter.get(position)
替换我答案中所有的getItem(possition)
yourListFieldInAdapter.get(position)
RowItemClass
is long or int (if not use "or:" version) RowItemClass
id字段为long或int(如果不使用“ or:”版本) What you have to do: 您要做的是:
android:onClick
from item.xml
从item.xml
摆脱android:onClick
hasStableIds()
in ItemListAdapter
: public boolean hasStableIds () { return true; }
在ItemListAdapter
覆盖hasStableIds()
: public boolean hasStableIds () { return true; }
public boolean hasStableIds () { return true; }
getItemId(...)
in adapter: public long getItemId (int position) { return getItem(position).id; }
在适配器中重写getItemId(...)
: 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);
在Activity
onCreate(...)
获取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) 使用第二种方法,您可以省略点2和3。(如果在适配器实现中使用了“不好的”方式return yourListFieldInAdapter.get(pos)
重写getItem
( return yourListFieldInAdapter.get(pos)
)和getCount
( return yourListFieldInAdapter.size()
))
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 ) 脱离主题的提示:出于您的理智, ItemDetails
在类名itemDetails
=> ItemDetails
...中使用小写字母作为首字母:)(OMG,OMG,itemDetails是变量或类名)
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). 如果我正确理解您的问题,则可以尝试在适配器中设置OnClickListener(不要在xml中设置onClick)。 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将更改为
showItemDetails(int id) {
Intent intent = new Intent(this, itemDetails.class);
intent.putExtra("itemId", id);
this.startActivity(intent);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.