[英]android ListView pass hidden values from one Activity to another
I have a ListView created with an ArrayAdapter which gets different values from a MySQL database using JSON parsing. 我有一个用ArrayAdapter创建的ListView,它使用JSON解析从MySQL数据库获取不同的值。 Right now I'm passing some of these values to the next activity when the user clicks on an item from the list: 现在,当用户单击列表中的项目时,我会将其中一些值传递给下一个活动:
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent intent = new Intent(ListActivity.this, ListingDescription.class);
String place_name = ((TextView) v.findViewById(R.id.textView3)).getText().toString();
intent.putExtra("place_name",place_name);
startActivity(intent);
That's working fine, but now I want to get 2 more values from the MySQL database which should not be shown in the ListView. 一切正常,但现在我想从MySQL数据库中获取另外2个值,这些值不应显示在ListView中。 Which way should I go? 我应该走哪条路? I tried adding the values in the ArrayAdapter but then I realized I'm passing the values by getting the textview information. 我尝试在ArrayAdapter中添加值,但是后来我意识到我正在通过获取textview信息来传递值。 How can I pass in an intent some information from the ArrayList adapter without having it shown in the ListView? 我如何从ArrayList适配器中传递一些信息而又不将其显示在ListView中? Maybe there's a different way to do this? 也许有另一种方法可以做到这一点?
This is how I parse the JSON: 这就是我解析JSON的方式:
for (int i=0; i<theJson.length(); i++){
Places place = new Places();
JSONObject jRealObject = theJson.getJSONObject(i);
place.setImage(jRealObject.getString("image"));
place.setName(jRealObject.getString("name"));
place.setLocation_address(jRealObject.getString("location_address"));
place.setOpen_until(jRealObject.getString("open_until"));
placeslist.add(place);
I also thought about just putting the values as Invisible in the layout, but I think that's definitely not the way to go. 我还考虑过在布局中将值设置为“不可见”,但这绝对不是可行的方法。 I'll appreciate any help! 我将不胜感激!
You can pass Places object through intent
. 您可以通过intent
传递Places对象。
intent.putExtra("some_name",adapter.getItem(position).get(position).toString());
In another Activity
: 在另一个Activity
:
JSONObject obj = new JSONObject(getIntent().getStringExtra("some_name"));
Create a getter method in Places as you have setter method already. 在已有Placer方法的地方,在Places中创建一个getter方法。
List placeslist = new ArrayList<>(); 列出placeslist = new ArrayList <>();
add placeslist to your adapter. 将场所列表添加到适配器。
OnItemClick. OnItemClick。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String image = adapter.getItem(position).getImage();
String name= adapter.getItem(position).getName();
//use these in intents
}
EDIT 编辑
final CustomAdapter adapter = new CustomAdapter(this, R.layout.activity_list_view, placeslist );
ListView listView = (ListView) findViewById(R.id.radioGroup);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String image = adapter.getItem(position).getImage();
String name= adapter.getItem(position).getName();
}
});
}
The easiest way you can do its using setTag(Object o)
and getTag()
method. 使用setTag(Object o)
和getTag()
方法最简单的方法。
Procedure 程序
Set the object to the view when you implement the getView()
method inside adapter. 在适配器内部实现getView()
方法时,将对象设置为视图。
Then OnItemClicked
call the getTag()
and manually cast it into the class you defined for your data. 然后OnItemClicked
调用getTag()
并将其手动转换为您为数据定义的类。
Pass the data to your intent before calling startActivity(Intent i)
在调用startActivity(Intent i)
之前将数据传递给您的意图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.