//我想在列表视图中显示列表项我将所有项放在列表中我想显示它正在显示的列表视图中的所有项目,但获取相同的名称,我在getview中保持for循环。请帮助我如何在列表视图中显示列表项。
public class Listjava extends Activity {
/** Called when the activity is first created. */
int i = 0;
List<Contact> contacts;
MyBaseAdapter adapter;
final DatabaseHandler db = new DatabaseHandler(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
contacts = db.getAllContacts();
ListView list = (ListView) findViewById(R.id.listView1);
adapter = new MyBaseAdapter(getApplicationContext(), contacts);
list.setAdapter(adapter);
}
public class MyBaseAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
public MyBaseAdapter(Context applicationContext, List<Contact> contacts) {
// TODO Auto-generated constructor stub
inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return contacts.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.layoutlist, null);
}
for (Contact cn : contacts) {
String log = "Id: " + cn.getID() + " ,Name: " + cn.getName()
+ " ,Phone: " + cn.getPhoneNumber();
// Writing Contacts to logsout
System.out.println("log" + log);
TextView title2 = (TextView) vi
.findViewById(R.id.txt_ttlsm_row); // title
// String song = title.get(position).toString();
title2.setText(cn.getName());
TextView title22 = (TextView) vi
.findViewById(R.id.txt_ttlcontact_row2); // notice
title22.setText(cn.getPhoneNumber());
}
return vi;
}
}
}
答案 0 :(得分:2)
如下所示更改MyBaseAdapter类。
public class MyBaseAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
List<Contact> contacts;
public MyBaseAdapter(Context applicationContext, List<Contact> contacts) {
// TODO Auto-generated constructor stub
this.contacts = contacts;// Initialize here
inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return contacts.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return contacts.get(position);// Changed
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position; //Changed
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.layoutlist, null);
}
Contact cn = (Contact) getItem(position);
TextView title2 = (TextView) vi
.findViewById(R.id.txt_ttlsm_row); // title
title2.setText(cn.getName());
TextView title22 = (TextView) vi
.findViewById(R.id.txt_ttlcontact_row2); // notice
title22.setText(cn.getPhoneNumber());
return vi;
}
}
答案 1 :(得分:1)
你不应该在getView方法中添加for循环,而是使用这样的联系人列表:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.layoutlist, null);
}
String log = "Id: " + contacts.get(position).getID() + " ,Name: " + contacts.get(position).getName()
+ " ,Phone: " + contacts.get(position).getPhoneNumber();
// Writing Contacts to logsout
System.out.println("log" + log);
TextView title2 = (TextView) vi
.findViewById(R.id.txt_ttlsm_row); // title
// String song = title.get(position).toString();
title2.setText(contacts.get(position).getName());
TextView title22 = (TextView) vi
.findViewById(R.id.txt_ttlcontact_row2); // notice
title22.setText(contacts.get(position).getPhoneNumber());
return vi;
}