我正在做一个名为电话簿的 android 应用程序。我正在使用 php 脚本从服务器中提取联系方式。联系方式包含姓名、电话号码、电子邮件地址。我想在列表视图中显示它。我已经尝试过,但我能够在不同的行中显示各个人的姓名、ph.no 和 email.id。我怎样才能在同一行显示它?
问问题
222 次
1 回答
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;
}
}
于 2012-11-24T09:13:25.350 回答