-1

I am building an android application where I am parsing the raw array to my baseAdapter of listview. Like :

  1. I take global variable

    ListView l1;
    String[] t1={"video1","video2"};
    String[] d1={"lesson1","lesson2"};
    int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};
    
  2. Initialy listview.

    l1=(ListView)findViewById(R.id.list);
    l1.setAdapter(new dataListAdapter(t1,d1,i1));
  1. Use baseAdapter Class
    class dataListAdapter extends BaseAdapter {
    String[] Title, Detail;
    int[] imge;

    dataListAdapter() {
        Title = null;
        Detail = null;
        imge=null;
    }

    public dataListAdapter(String[] text, String[] text1,int[] text3) {
        Title = text;
        Detail = text1;
        imge = text3;

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Title.length;
    }

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

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

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

        LayoutInflater inflater = getLayoutInflater();
        View row;
        row = inflater.inflate(R.layout.custom, parent, false);
        TextView title, detail;
        ImageView i1;
        title = (TextView) row.findViewById(R.id.title);
        detail = (TextView) row.findViewById(R.id.detail);
        i1=(ImageView)row.findViewById(R.id.img);
        title.setText(Title[position]);
        detail.setText(Detail[position]);
        i1.setImageResource(imge[position]);

        return (row);
    }
}

This is working fine for only static data. Now I am parsing the value using Json like

              try {
              jsonObj = new JSONObject(result);
              array = jsonObj.getJSONArray("list");

               for (int i = 0; i < array.length(); i++) {
                      JSONObject obj = array.getJSONObject(i);
                          profilePic= ""+obj.getString(TAG_PROFILEPIC);
                          firstName= ""+obj.getString(TAG_FIRSTNAME);
                          lastName= ""+obj.getString(TAB_LASTNAME);
                      //  status= ""+obj.getInt("status");
                          HashMap<String, String> contact = new HashMap<String, String>();
                          contact.put(TAG_PROFILEPIC, profilePic);
                          contact.put(TAG_FIRSTNAME, firstName);
                          contact.put(TAB_LASTNAME, lastName);


                          // do code for adding these values to adapter.
                      }

          } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }           }

Now, I need to parse this Json value in place of the static data of baseAdapter.

How can I do this?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
hitesh
  • 3
  • 2

1 Answers1

0

Your BaseAdapter implementation is incorrect. It should be maintain some sort of a collection of data. You are instead storing a single item which then repetitively displays itself for however many characters are found in the title. In addition, you are not using a ViewHolder which is a must for every adapter. I highly recommend subscribing to Commonsware to learn Android. Here is a nice excerpt of his which explains adapters and AdapterViews. Note, it's from a fairly old version.

Otherwise you should Google around more to learn the ins and outs of making your own BaseAdapter. Here's a nice little SO question for that.

As for your JSON data. You need to either store and read that from the adapter itself or convert your JSON data into something else more usable by an existing data structure supported by an adapter. Considering you are still trying to learn just how to build a simple custom adapter, I would hold off on creating your own JSON adapter. Instead, use a pre-existing one which solves most of these problems for you.

Community
  • 1
  • 1
Ifrit
  • 6,791
  • 8
  • 50
  • 79