Android 自定义ListView仅显示一条记录
您好,我的自定义列表视图仅显示一条记录。一切正常控制台中显示多条记录,但当我尝试在Android 自定义ListView仅显示一条记录,android,listview,arraylist,android-sqlite,Android,Listview,Arraylist,Android Sqlite,您好,我的自定义列表视图仅显示一条记录。一切正常控制台中显示多条记录,但当我尝试在列表视图中显示记录时,它仅显示最后一条记录其覆盖的第一条记录,当前有两条记录显示在我的控制台中,但在列表视图中它仅显示最后一条记录,我从昨天开始搜索解决方案,我花了6个多小时来解决这个问题 Cart\u Adapter.java public class Cart_Adapter extends BaseAdapter { Activity activity; ArrayList<Produc
列表视图中显示记录时,它仅显示最后一条记录其覆盖的第一条记录,当前有两条记录显示在我的控制台中,但在列表视图中它仅显示最后一条记录,我从昨天开始搜索解决方案,我花了6个多小时来解决这个问题
Cart\u Adapter.java
public class Cart_Adapter extends BaseAdapter {
Activity activity;
ArrayList<ProductBean> list;
String abc;
public Cart_Adapter(Activity activity, ArrayList<ProductBean> list) {
this.activity = activity;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v=view;
if(v==null){
LayoutInflater layoutInflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=layoutInflater.inflate(R.layout.cartxml,null);
}
TextView title=(TextView) v.findViewById(R.id.title);
TextView size=(TextView) v.findViewById(R.id.size);
TextView color=(TextView) v.findViewById(R.id.color);
Button remove=(Button) v.findViewById(R.id.remove);
ImageView imageView=(ImageView) v.findViewById(R.id.imageView);
title.setText(list.get(i).getTitle());
size.setText(list.get(i).getSize());
color.setText(list.get(i).getColor());
ArrayList<ImagesBean> aa =list.get(i).getImagesList();
if(aa!=null){
for(i=0; i<aa.size(); i++) {
abc = String.valueOf(aa.get(i).getImgone());
System.out.println("--testing" + abc);
}
}
System.out.println("===="+abc);
if(abc !=null ) {
imageView.setImageBitmap(getBitmapFromString(abc));
// imageView.setImageBitmap(getBitmapFromString(list.get(i).getImagesList()));
}else{}
return v;
}
private Bitmap getBitmapFromString(String encode){
byte[] b = Base64.decode(encode,Base64.DEFAULT);
return BitmapFactory.decodeByteArray(b,0,b.length);
}
}
在getCount()中打印列表大小,以确保列表是否在内部发送。检查项目的布局高度,它应该是包裹内容。因为您正在设置循环中的每个项目
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
lv.setAdapter(myadapter);
}
}
因此,它将从迭代中获取最后一个pid,并获取您在卡适配器中设置的产品详细信息
错误的部分是:您在循环中每次迭代都调用setAdapter()
解决方案:
收集循环中的记录列表,您可以继续在数组列表中添加产品详细信息,然后在适配器中的循环外部设置最终列表
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
lv.setAdapter(myadapter);
}
您正在循环内设置适配器。这是完全错误的。将此代码置于循环之外。
还有一件事,是时候升级了。它可以轻松更好地实现ViewHolder模式
ArrayList<String> arrayList=mydatabase.getpid(userid);
ArrayList<String> dataList=new ArrayList();;
for(int i =0; i<arrayList.size(); i++) {
String l = arrayList.get(i);
final String ll = l.toString().replace("[", "").replace("]", "").replace(" ", "");
System.out.println("--before loop" + ll);
for (String s : ll.split(",")) {
// Do your stuff add data to list here
dataList.add(stringToAdd);
}
}
myadapter = new Cart_Adapter(Cart.this, dataList);
lv.setAdapter(myadapter);
ArrayList ArrayList=mydatabase.getpid(userid);
ArrayList dataList=新的ArrayList();;
对于(int i=0;i您需要将您的cartxml
high设置为android:layout\u height=“wrap\u content”
像下面的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
编辑
ArrayList<ProductBean> list= new ArrayList();
ArrayList<String> arrayList=mydatabase.getpid(userid);
System.out.println("--outside"+arrayList);
myadapter = new Cart_Adapter(Cart.this, list);
lv.setAdapter(myadapter);
for(int i =0; i<arrayList.size(); i++) {
String l = arrayList.get(i);
final String ll = l.toString().replace("[", "").replace("]", "").replace(" ", "");
System.out.println("--before loop" + ll);
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
list.addAll(mydatabase.getproduct_details(pid));
}
}
myadapter.notifyDataSetChanged().
ArrayList list=new ArrayList();
ArrayList ArrayList=mydatabase.getpid(userid);
System.out.println(“--outside”+arrayList);
myadapter=新的购物车适配器(Cart.this,list);
lv.设置适配器(myadapter);
对于(int i=0;i检查您的cartxml
布局文件。根RelativeLayout
高度将为wrap\u content
否则它将占用单个项目的所有可用空间。使用android:layout\u width=“wrap\u content”
对于cartxml中的RelativeLayoutin
显示布局文件我已添加适配器和活动的xml文件也请检查如果我没有将适配器放在循环中,那么我如何获得多条记录,因为pid在同一列表上每次调用setAdapter
一个是覆盖。解决方案是在循环中构建记录列表,然后在loop.bro外部设置适配器。我这样做了,我刚刚放置了lv.setAdapter(myadapter)在循环之外,但它仍然只显示最后一条记录什么是你的列表兄弟?我如何在循环之外填充多条记录?这就是我说的,你需要弄清楚它,因为我不知道你的模型。它应该是一个列表,包含listView的所有数据。请参阅更新的答案,它显示了如何构建列表。尽管我不知道这个列表到底包含什么以及这个拆分循环的原因,但它会给出一个想法。thx如何做?在getCount()中记录数组列表大小-'Log.d('TAG','size-“+list.size())”方法。布局“R.layout.cartxml”中的第一个视图应具有高度“wrap_content”。我是这样做的,兄弟,让我再试一次,它可以工作。非常感谢Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;在循环之外,但仍然只显示一条记录显示您的PID记录和结构,将能够帮助您快速完成!我添加了控制台输出
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
lv.setAdapter(myadapter);
}
ArrayList<String> arrayList=mydatabase.getpid(userid);
ArrayList<String> dataList=new ArrayList();;
for(int i =0; i<arrayList.size(); i++) {
String l = arrayList.get(i);
final String ll = l.toString().replace("[", "").replace("]", "").replace(" ", "");
System.out.println("--before loop" + ll);
for (String s : ll.split(",")) {
// Do your stuff add data to list here
dataList.add(stringToAdd);
}
}
myadapter = new Cart_Adapter(Cart.this, dataList);
lv.setAdapter(myadapter);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
ArrayList<ProductBean> list= new ArrayList();
ArrayList<String> arrayList=mydatabase.getpid(userid);
System.out.println("--outside"+arrayList);
myadapter = new Cart_Adapter(Cart.this, list);
lv.setAdapter(myadapter);
for(int i =0; i<arrayList.size(); i++) {
String l = arrayList.get(i);
final String ll = l.toString().replace("[", "").replace("]", "").replace(" ", "");
System.out.println("--before loop" + ll);
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
list.addAll(mydatabase.getproduct_details(pid));
}
}
myadapter.notifyDataSetChanged().