解析JSON并将对象显示在列表视图上

时间:2012-11-24 06:39:14

标签: android json

我正在做一个名为phonebook的android应用程序。我正在使用php脚本从服务器中提取联系人详细信息联系方式包含姓名,ph.no,email.id。我想在列表视图中显示它。我已经尝试但我可以在不同的行中删除相应人员的姓名,ph.no和email.id。如何在同一行显示它?

1 个答案:

答案 0 :(得分:0)

您可以使用自定义ArrayAdapter自定义Android ListView项目。

你可以这样做,

/* In main activity */
ListView myListView = (ListView)findViewById(R.id.myListView);
final ArrayList<Phonebook> todoItems = new ArrayList<Phonebook>();
final YourAdapter x= new YourAdapter(this,R.layout.your_listviewlayout,todoItems);
myListView.setAdapter(x);

Phonebook phnbk= new Phonebook();
// enter code for set values to Phonebook class variables
/* Inserting the values to array */
todoItems.add(phnbk);

/* Customized array adaptor class */
private class YourAdapter extends ArrayAdapter<Phonebook> 
{
    private ArrayList<Phonebook> items;
    public YourAdapter (Context context, int textViewResourceId, ArrayList<Phonebook> items) 
    {
        super(context, textViewResourceId, items);
        this.items = items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View v = convertView;
        if (v == null) 
        {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.your_listviewlayout, null);
        } 
        Phonebook o = items.get(position);
        if (o != null) 
        {
            //insert values to each text view in your list view layout
            tv1.setText(o.name);
            tv2.setText(o.phnnum);
                        tv3.setText(o.email);                   
        }
        return v;
    }
}

/* Phonebook class */
public class Phonebook{    
    public String name;
    public String phnnum;
    public String email;
    public Phonebook(){
        super();
    }

    public Phonebook(String name, String phnnum, String email) {
        super();
        this.name = name;
        this.phnnum = phnnum;
        this.email = email;
    }
}