0

我正在尝试使用此类模板将项目添加到数组列表:

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

}

这是整个代码:

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); 

}   

问题是,不是用漂亮的用户名和电子邮件字符串填充我的列表视图,而是用这样的项目填充:

com.android.ninjaarchives。模板@40585690

我想我在某个地方迷路了,但我已经尝试了很多年,但一无所获。谁能指出我正确的方向?

谢谢你的帮助。

注意:不太确定代码发生了什么;它似乎没有正确粘贴。

4

3 回答 3

4

使用下面的代码,它可以为您提供解决方案

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); 

}

但是最好通过扩展 BaseAdapter 来编写自定义适配器,并在 getView 方法中进行 listItem 处理,这是一个简单的教程

于 2013-01-22T15:01:56.103 回答
1

参加一个扩展 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;
    }
}

并将适配器设置为您的列表视图

lv.setAdapter(new CustomAdapter(this));
于 2013-01-22T15:03:44.333 回答
0

在这种情况下,您必须使用自定义适配器(从 ArrayAdapter 扩展)并覆盖 getView 方法以在自定义布局中显示用户名和电子邮件。

于 2013-01-22T15:02:42.603 回答