在Android中以列表形式显示地图的ArrayList中的元素

时间:2015-04-13 08:52:38

标签: android android-arrayadapter

我有一个A数据存储在 ArrayList<的HashMap<字符串,字符串> > 从JSON检索 形式(即)

[{price: =1685 name: =Monographie Der Gattung Pezomachus (Grv.) by Arnold F.    Rster}]

我需要在Android中将所有地图元素显示为列表形式。 我尝试了很多方法,但我无法做到。

还帮助我了解要在其中使用的布局

编辑:

MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(myarr_list);
setListAdapter(adapter);

在MySimpleArrayAdapter类中,在Constructor中

public MySimpleArrayAdapter( ArrayList<HashMap<String,String>> pl) {
LayoutInflator inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

此后控件不会继续, MySimpleArrayAdapter类

public class MySimpleArrayAdapter extends BaseAdapter{
    ArrayList<HashMap<String, String>> ProductList = new ArrayList<HashMap<String, String>>();
    LayoutInflater inflater;

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }


    //Constructor
    public MySimpleArrayAdapter( ArrayList<HashMap<String,String>> pl) {

        this.ProductList = pl;
        inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }





    public View getView(int position, View convertView, ViewGroup parent) {

        View myview = convertView;

        if (convertView == null) {
            myview = inflater.inflate(R.layout.show_search_result, null);
        }


        TextView price = (TextView) myview.findViewById(R.id.price);
        TextView name = (TextView) myview.findViewById(R.id.name);



        HashMap<String, String> pl = new HashMap<String, String>();
        pl = ProductList.get(position);
        //Setting 
        price.setText(pl.get("price"));
        name.setText(pl.get("name"));
        return myview;

    }


}

我在这里编辑一个来自AsyncTask扩展的SearchResultsTask的onPostExecute类

protected void onPostExecute(JSONObject json) {

            if (json != null && json.length() > 0) {
                try {                   
                JSONArray json_results = (JSONArray)(json.get("results"));               
                String parsedResult = "";
                System.out.println("-> Size ="+ json_results.length());


                    for(int i = 0; i < json_results.length(); i++){         
                        HashMap<String, String> map = new HashMap<String, String>();
                        JSONObject json_i = json_results.getJSONObject(i);


                        map.put("name: ",json_i.getString("name") + "\n");
                        map.put("price: ",json_i.getString("price") + "\n");    
                        arr_list.add(map);

                    }                        
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("-> Size =====arr_llist ="+ arr_list.size());
        //  CustomListAdapter adapter = new CustomListAdapter (arr_list);
            //final StableArrayAdapter adapter = new StableArrayAdapter(this, R.id.result, arr_list);
        //  listview.setAdapter(adapter);
            MyListActivity obj1 = new MyListActivity();
            Bundle icicle = null;
            obj1.onCreate(icicle); 


        }
    public class MyListActivity extends Activity {

          public void onCreate(Bundle icicle) {
            //  System.out.println("In my list Activity");
            // super.onCreate(icicle);

            //populate list
           MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this,arr_list);
            //  System.out.println("in 2");

           adapter.getView(0, listview, listview);
           listview.setAdapter(adapter);
          }


    }

2 个答案:

答案 0 :(得分:0)

    @Override
    public int getCount() {
        return ProductList.size() ;
    }

答案 1 :(得分:0)

   //Constructor
    public MySimpleArrayAdapter( ArrayList<HashMap<String,String>> pl, Context c) {

        this.ProductList = pl;
        inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

getSystemService()是上下文的方法,您在适配器的实例上调用它。