简体   繁体   English

android ListView将隐藏的值从一个活动传递给另一个

[英]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 程序

  1. Set the object to the view when you implement the getView() method inside adapter. 在适配器内部实现getView()方法时,将对象设置为视图。

  2. Then OnItemClicked call the getTag() and manually cast it into the class you defined for your data. 然后OnItemClicked调用getTag()并将其手动转换为您为数据定义的类。

  3. Pass the data to your intent before calling startActivity(Intent i) 在调用startActivity(Intent i)之前将数据传递给您的意图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图将值从列表视图传递到另一个活动 - Trying to pass values from listview to another activity 如何从一个活动的列表视图传递数据并将其传递给另一活动? - How to pass the data from listview of one activity and pass it to another activity? Android:如何不断更新/传递值从一个活动到另一个活动? - Android: How to constantly update/pass values from one activity to another? 将数据从一个活动传递到另一个活动列表视图 - Pass data from one activity to another activity listview 将2个值从一个活动传递给另一个活动 - pass 2 values from one to another activity 如何在Android中的另一个活动中将两个edittext值传递给listview? - How to pass two edittext values to listview in another activity in android? 我如何将整个ListView项目从一个Activity传递到android中的另一个项目 - How can I pass the entire ListView item from one Activity to another in android 将值从Android中的listView获取到textView或另一个Listview相同的活动 - Getting values from listView in Android to textView or another Listview same activity Android:将数据从一个活动传递到ListView中的另一个活动 - Android: Passing data from one activity to another activity in ListView Android:在另一个活动中将TextView数据从ListView传递到TextView - Android: pass TextView data from ListView to TextView in another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM