Pregunta

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);
      }
¿Fue útil?

Solución

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"
  ...
  />

Otros consejos

Try something like this

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

You can also make custom adapter.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top