简体   繁体   中英

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. 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. 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. How can I pass in an intent some information from the ArrayList adapter without having it shown in the ListView? Maybe there's a different way to do this?

This is how I parse the 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.putExtra("some_name",adapter.getItem(position).get(position).toString());

In another Activity :

JSONObject obj = new JSONObject(getIntent().getStringExtra("some_name"));

Create a getter method in Places as you have setter method already.

List placeslist = new ArrayList<>();

add placeslist to your adapter.

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.

Procedure

  1. Set the object to the view when you implement the getView() method inside adapter.

  2. Then OnItemClicked call the getTag() and manually cast it into the class you defined for your data.

  3. Pass the data to your intent before calling startActivity(Intent i)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM