【问题标题】:Show the Elements from ArrayList of map ,in list form in Android在Android中以列表形式显示地图的ArrayList中的元素
【发布时间】:2015-04-13 08:52:37
【问题描述】:

我有一个存储在 ArrayList > 中的 A 数据,从 JSON 中检索到 以形式(即)

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

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

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

已编辑:

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

在 MySimpleArrayAdapter 类中,在构造函数中

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;

    }


}

我在这里编辑 SearchResultsTask 中由 AsyncTask 扩展的 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);
          }


    }

【问题讨论】:

  • 向我们展示您尝试过但未成功的许多方法
  • 向我们展示您的适配器的代码
  • MySimpleArrayAdapter#getView(){ // p 是位置,你会从 getView() 的 args 中得到它 HashMap yourMapAtIndexP = yourList.get(P); String key = "你的键值"; String valueToShow = yourMapAtIndexP.get(key); // 现在在你想要的任何视图中显示它 }
  • @GauravGupta ,我无法通过这个充气机 = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  • 嘿@GauravGupta,我的地图结构就像{name: =Monographie Der Gattung Pezomachus (Grv.) by Arnold F.Rster, price: =1685} ,我无法通过你的方法访问这些值。帮我 ..!我做了这个System.out.println(mylist.get("price")); System.out.println(mylist.get("name"));,它给了null

标签: android android-arrayadapter


【解决方案1】:
    @Override
    public int getCount() {
        return ProductList.size() ;
    }

【讨论】:

    【解决方案2】:
       //Constructor
        public MySimpleArrayAdapter( ArrayList<HashMap<String,String>> pl, Context c) {
    
            this.ProductList = pl;
            inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        }
    

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

    【讨论】:

    • 我应该在这个构造函数中传递什么(即哪个上下文)?我尝试使用 this , this.getBaseContext , getApplicationContext .....
    • 可以通过this的Activity。那是活动上下文。
    • 我已经在上面询问的 qstn 中提供了我的 OnPostExecuteTask 和 MySimpleArayAdapter 的调用请找到它
    • 您正在破坏活动生命周期。生命周期回调由 f/w 提供,您正在调用自己。因此,到那时,上下文不会被初始化。我建议您重新访问 android 教程并查看生命周期。您还将在 vogella 上找到自定义适配器实现。您的代码需要重组。
    • 请帮我兄弟重组它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    相关资源
    最近更新 更多