Search code examples
androidlistviewsimpleadapter

How to use simpleadapter with activity


I am trying to use a simpleadapter for getting contents from a server into a listview. But the problem is that I want to use it as an activity and not a listactivity. so how can i do that? Here's my code

    try
      {
          JSONArray jArray = new JSONArray(result);
          String s="";
          Log.w("Lengh",""+jArray.length());
          for(int i=0;i<jArray.length();i++){
                 ListView lv=getListView();
                 JSONObject json_data = jArray.getJSONObject(i);
                 s=json_data.getString("Cname");
                 HashMap<String, String> map = new HashMap<String, String>();
                 map.put("Zname",s/*json_data.getString("Cname")*/);
                 val.add(map);
         ListAdapter ad=new SimpleAdapter(Second.this,val,R.layout.v,new String[]{"Zname","Zname"},new int[]{R.id.textView1,R.id.textView2});
                setListAdapter(ad);  
                 Log.i("cname",s);
      }

Solution

  • Simply set the adapter to your ListView as defined in layout xml.

    SimpleAdapter adapter = new SimpleAdapter(Second.this,val,R.layout.v,new String[]{"Zname","Zname"},new int[]{R.id.textView1,R.id.textView2});
    ListView list = (ListView) findViewById(R.id.list);
    list.setAdapter(adapter);
    

    and in your activity layout xml file would be:

    <ListView
      android:id="@+id/list"
      ...
      />