0

I'm trying to add items to an arraylist using this class template:

public class Template {
public String username;
public String email;

}

Here's the whole code:

public void JsonToArrayList(JSONArray myJsonArray) throws JSONException
{
    ArrayList<Template> listItems = new ArrayList<Template>();
    JSONObject jo = new JSONObject();
    Template tem = new Template();
    ListView lv = (ListView) findViewById(R.id.listView1);

    for(int i = 0; i<myJsonArray.length(); i++)
    {
        jo = myJsonArray.getJSONObject(i);
        tem.username = jo.getString("username");
        tem.email = jo.getString("user_email");

        listItems.add(tem);         
        Log.e("Ninja Archives", tem.username);

    }
    // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
    ArrayAdapter<Template> arrayAdapter = new ArrayAdapter<Template>(this,android.R.layout.simple_list_item_1, listItems);
    lv.setAdapter(arrayAdapter); 

}   

The problem is, instead of filling my listview with nice username and email strings, it's filling up with items like this:

com.android.ninjaarchives. Template@40585690

I think somewhere along the line I have become lost, but I've been trying all sorts for ages now and getting nowhere. Can anyone point me in the right direction?

Thanks for any help.

Note: not really sure what's going on with the code; it doesn't appear to be pasting correctly.

2
  • you are adding template objects and not strings..i wld say use Custom adapter Commented Jan 22, 2013 at 14:57
  • You need to get the strings like tem.username and tem.email isteam of arryalist of template itself Commented Jan 22, 2013 at 14:58

3 Answers 3

4

Use below code, it can be a solution for you

public void JsonToArrayList(JSONArray myJsonArray) throws JSONException
{
    ArrayList<Template> listItems = new ArrayList<Template>();
    JSONObject jo = new JSONObject();
    Template tem = new Template();
    ListView lv = (ListView) findViewById(R.id.listView1);

    String listItemString[] = new String[myJsonArray.length];

    for(int i = 0; i<myJsonArray.length(); i++)
    {
        jo = myJsonArray.getJSONObject(i);
        tem.username = jo.getString("username");
        tem.email = jo.getString("user_email");
       listItemString[i]  = tem.username +" - " + tem.email; // u can change it according to ur need.
        listItems.add(tem);         
        Log.e("Ninja Archives", tem.username);

    }
    // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItemString);
    lv.setAdapter(arrayAdapter); 

}

But better to write Custom adapter by extending BaseAdapter, and do listItem handling in getView method here is one simple tutorial

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent solution; I totally get it now. Thanks very much.
It is just a temporary solutions. U need to mention both the list and String[] to track click items etc.
1

Take a class extending Base

    private class CustomAdapter extends BaseAdapter
{
    LayoutInflater inflater;
    public CustomAdapter(Context context)
    {
        inflater = LayoutInflater.from(context);
    }

    public int getCount()
    {
        return listItems.size();
    }

    public Object getItem(int position)
    {
        return listItems.get(position);
    }

    public long getItemId(int position)
    {
        return position;
    }

    public View getView(final int position, View convertView,ViewGroup parent)
    {
        //if(convertView==null)
        //convertView = inflater.inflate(R.layout.listlayout, parent, false);
        Template data = (Template) getItem(position);
        TextView v=new TextView(context);
        v.setText(data.name);
        return v;
    }
}

and set adapter to your listview

lv.setAdapter(new CustomAdapter(this));

Comments

0

In this case you have to use a custom adapter (that extends from ArrayAdapter) and override the getView method to display in a custom layout the username and the email.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.