我在活动中有一个列表视图,我已经创建了相同的自定义列表视图。在自定义列表中,我有一个imageview和两个textview。在向列表添加值时,它只显示最后一个元素。
public class ContactUs extends Activity {
ImageView imageView;
TextView tvDetails,tv1;
private static String details[] = {"details1","details2","details3","details4","details5","details6"};
private static String desc[] = {"Name","Call","Email",
"Skype","Head Office","Website"};
private static int icons[] = {R.drawable.user, R.drawable.smartphone,
R.drawable.e_mail_envelope, R.drawable.facebook_logo, R.drawable.map_localization,
R.drawable.grid_world};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_us);
imageView=(ImageView)findViewById(R.id.imageView);
tvDetails=(TextView)findViewById(R.id.tvDetails);
tv1=(TextView)findViewById(R.id.tv11);
//final ListView lv1 = (ListView) findViewById(R.id.lvContact);
ArrayList<addValuesContact> searchResults = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.lvContact);
lv1.setAdapter(new MyCustomAdapterContact(this, searchResults));
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
addValuesContact fullObject = (addValuesContact) o;
//Toast.makeText(getBaseContext(), "You have chosen: " + " " + fullObject.getDetails(), Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<addValuesContact> GetSearchResults() {
ArrayList<addValuesContact> results = new ArrayList<>();
addValuesContact sr1 = new addValuesContact();
for (int i=0;i<6;i++) {
sr1.setImage(icons[i]);
sr1.setTv1(desc[i]);
sr1.setDetails(details[i]);
results.add(sr1);
}
return results;
}}
我的自定义适配器类详细信息如下所示
public class MyCustomAdapterContact extends BaseAdapter {
private static ArrayList<addValuesContact> searchArrayList;
private LayoutInflater mInflater;
public MyCustomAdapterContact(Contextcontext,ArrayList<addValuesContact>results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.contact_list, null);
holder = new ViewHolder();
holder.imageView(ImageView)convertView.findViewById(R.id.imageView);
holder.tvDetails (TextView)convertView.findViewById(R.id.tvDetails);
holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
addValuesContact bean = (addValuesContact)searchArrayList.get(position);
holder.imageView.setImageResource(bean.getImage());
holder.tvDetails.setText(searchArrayList.get(position).getDetails());
holder.tv1.setText(searchArrayList.get(position).getTv1());
return convertView;
}
static class ViewHolder {
ImageView imageView;
TextView tvDetails;
TextView tv1;
}}
Pojo课程在下面给出
public class addValuesContact {
private String details = "";
private String tv1 = "";
int image;
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public void setDetails(String details) {
this.details = details;
}
public String getTv1() {
return tv1;
}
public void setTv1(String tv1) {
this.tv1 = tv1;
}
public String getDetails() {
return details;
}}
答案 0 :(得分:3)
更新以下方法:
./start -Dhttps.port=9443
原因:您需要每次都创建模型类的新对象。
完成强>
答案 1 :(得分:0)
要创建包含数组中所有可用项的列表,每次遍历用于设置数据的循环时,都需要创建模型类的对象。因此,在设置变量之前,请在for循环中添加addValuesContact sr1 = new addValuesContact();
。
private ArrayList<addValuesContact> GetSearchResults() {
ArrayList<addValuesContact> results = new ArrayList<>();
for (int i=0;i<6;i++) {
addValuesContact sr1 = new addValuesContact();
sr1.setImage(icons[i]);
sr1.setTv1(desc[i]);
sr1.setDetails(details[i]);
results.add(sr1);
}
return results;
}