2

我有一个名为的SearchActivity活动ListView来自 JSON 下载的数据子集(标题和距离)。我想在另一个名为ResultsActivity. 我知道如何从ListView自身获取数据并将其传递给其他活动,但我不确定如何获取所有数据以传递给第二个活动。

例如,我希望 onClickListener 对当前选定项目的 ArrayList 进行查找,以便我可以执行以下操作:

in.putExtra(TAG_NAME, name); in.putExtra(TAG_STREET, 街道); 等等。

问题是 ListView 中显示的唯一内容是名称和距离。如何使用当前的 Listview 选择进入 ArrayList 条目,以便显示街道?

任何帮助表示赞赏。

SearchActivity.java
package com.chance.squat;

import com.chance.squat.R;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SearchActivity extends ListActivity {

    // url to make request
    private static String url = "http://192.168.1.115:3000/bathrooms/nearby.json/?lat=45.580639&lon=-122.677682";

    // JSON Node names
    private static final String TAG_BATHROOMS = "bathrooms";
    private static final String TAG_ID = "ID";
    private static final String TAG_ACCESS = "access";
    private static final String TAG_CITY = "city";
    private static final String TAG_COMMENT = "comment";
    private static final String TAG_DIRECTIONS = "directions";
    private static final String TAG_NAME = "name";
    private static final String TAG_STREET = "street";
    private static final String TAG_BATHROOMTYPE = "bathroomtype";
    private static final String TAG_DISTANCE = "distance";
    private static final String TAG_AVAIL = "avail";
    private static final String TAG_COUNTRY = "country";
    private static final String TAG_STATE = "state";
    private static final String TAG_POSTAL = "postal";

    // contacts JSONArray
    JSONArray bathrooms = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_search);

        // Hashmap for ListView
        ArrayList<HashMap<String, String>> bathroomList = new ArrayList<HashMap<String, String>>();

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Contacts
            bathrooms = json.getJSONArray(TAG_BATHROOMS);

            // looping through All Contacts
            for(int i = 0; i < bathrooms.length(); i++){
                JSONObject c = bathrooms.getJSONObject(i);


                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String access = c.getString(TAG_ACCESS);
                String city = c.getString(TAG_CITY);
                String comment = c.getString(TAG_COMMENT);
                String directions = c.getString(TAG_DIRECTIONS);
                String name = c.getString(TAG_NAME);
                String street = c.getString(TAG_STREET);
                String bathroomtype = c.getString(TAG_BATHROOMTYPE);                
                String distance = c.getString(TAG_DISTANCE);
                String distanceTrimmed = distance.substring(0,4) + " " + "miles away";
                String avail = c.getString(TAG_AVAIL);
                String country = c.getString(TAG_COUNTRY);
                String state = c.getString(TAG_STATE);
                String postal = c.getString(TAG_POSTAL);

                System.out.println(name);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_ACCESS, access);
                map.put(TAG_CITY, city);
                map.put(TAG_COMMENT, comment);
                map.put(TAG_DIRECTIONS, directions);
                map.put(TAG_NAME, name);
                map.put(TAG_STREET, street);
                map.put(TAG_BATHROOMTYPE, bathroomtype);
                map.put(TAG_DISTANCE, distanceTrimmed);
                map.put(TAG_AVAIL, avail);
                map.put(TAG_COUNTRY, country);
                map.put(TAG_STATE, state);
                map.put(TAG_POSTAL, postal);

                // adding HashList to ArrayList
                bathroomList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        /**
         * Updating parsed JSON data into ListView
         * */

        ListAdapter adapter = new SimpleAdapter(this, bathroomList,
                R.layout.list_item,
                new String[] { TAG_NAME, TAG_DISTANCE}, new int[] {
                         R.id.name, R.id.distance });

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        // Launching new screen on Selecting Single ListItem
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                //String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
                //String distance = ((TextView) view.findViewById(R.id.distance)).getText().toString();


                // Starting new intent
                //Intent in = new Intent(getApplicationContext(), SeachActivity.class);
                //in.putExtra(TAG_NAME, name);
                //in.putExtra(TAG_DISTANCE, distance);
                //startActivity(in);

            }
        });


    }

}
4

4 回答 4

0

您可以在适配器上调用getItem 。

或者,您可以创建自己的从 BaseAdapter 扩展的适配器。在那里,您将完全控制要使用和返回的数据(使用getItem)。

此外,只要有可能,谷歌建议使用片段而不是活动。

于 2013-01-19T20:20:11.783 回答
0

您可以使用 ListView 适配器的方法从 ListView 中获取特定的对象和值,该onItemClickListener()方法获得了点击。下一步是在 ArrayList 中搜索(假设您将完整数据保存在 ArrayList 中),使用for loop.

例如,您可以从 ListView 中获取标题字符串并在 ArrayList 中搜索。为此,您需要访问 ListActivity 中的 ArrayList。如果无法访问,请尝试使用捆绑包,或者如果数据在 2 个以上的活动中经常使用,您还可以使用静态类来存储数据。

于 2013-01-19T20:21:53.507 回答
0

适配器 getItem(position)

获取数据集中与指定位置关联的数据项。

这是你想要的?

于 2013-01-19T20:20:04.450 回答
0

尝试获取选定的 ListItem ArrayList :

lv.setOnItemClickListener(new OnItemClickListener() {

 @Override
  public void onItemClick(AdapterView<?> parent, View view,
               int position, long id) {
   // getting values from selected ListItem
   ArrayList<HashMap<String, String>> arrhashmap=
             (ArrayList<HashMap<String, String>>) lv.getItemAtPosition(position);

   // Starting new intent
   Intent in = new Intent(getApplicationContext(), SeachActivity.class);
   in.putExtra("arralsthasmap", arrhashmap);
   startActivity(in);

} });

并在其他活动中检索 ArrayList 为:

ArrayList<HashMap<String, String>> arrhashmap 
                      =(ArrayList<HashMap<String, String>>) 
          getIntent().getSerializableExtra("arralsthasmap");
于 2013-01-19T20:26:15.923 回答