自定义ListView只显示一条记录

时间:2017-12-20 05:19:05

标签: android listview arraylist android-sqlite

您好我的自定义列表视图只显示一条记录。一切都很好,它在控制台中显示多条记录,但是当我试图在ListView中显示记录时,它只显示其覆盖的第一条记录的最后一条记录,目前有两条记录在我的控制台中显示但在{{1它只显示最后一条记录,我从昨天开始寻找解决方案,我花了6个多小时来解决这个问题。

Cart_Adapter.java

ListView

从我获取数据库记录的类

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);
   }
}

Activity_cart.xml

 ArrayList<String> arrayList=mydatabase.getpid(userid);
    System.out.println("--outside"+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(",")) {

            int pid = Integer.parseInt(s);
            myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
            lv.setAdapter(myadapter);
            }

        }

Cart.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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="match_parent"
tools:context="com.example.bhm.ishopping.Cart">

<android.support.constraint.ConstraintLayout

    android:id="@+id/customlayout"
    android:layout_width="410dp"
    android:layout_height="63dp"
    android:layout_marginStart="8dp"
    android:background="#39a423"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.617"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/textView15"
        android:layout_width="86dp"
        android:layout_height="29dp"
        android:layout_marginStart="320dp"
        android:textColor="@android:color/holo_blue_bright"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="16dp" />


</android.support.constraint.ConstraintLayout>

<TextView
    android:id="@+id/textView19"
    android:layout_width="171dp"
    android:layout_height="34dp"
    android:layout_marginEnd="120dp"
    android:layout_marginTop="76dp"
    android:text="Your Cart Detail"
    android:textColor="@android:color/black"
    android:textSize="22sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ListView
    android:id="@+id/lv"
    android:layout_width="373dp"
    android:layout_height="411dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/customlayout" />

<Button
    android:id="@+id/submit"
    android:layout_width="230dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="76dp"
    android:layout_marginTop="40dp"
    android:text="Submit Your Order"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/lv" />

 </android.support.constraint.ConstraintLayout>

控制台输出显示两条记录

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="99dp"
    android:layout_height="87dp"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/remove"
    android:layout_marginStart="16dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_launcher_background" />

<TextView
    android:id="@+id/textView16"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView"
    android:layout_marginStart="15dp"
    android:layout_toEndOf="@+id/imageView"
    android:text="Ttile"
    android:textColor="@android:color/background_dark"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.456"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView17"
    android:layout_width="wrap_content"
    android:layout_height="24dp"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@+id/textView16"
    android:layout_marginTop="102dp"
    android:text="Color"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView16" />

<TextView
    android:id="@+id/textView18"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/textView17"
    android:layout_below="@+id/textView17"
    android:text="Size"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView17" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/size"
    android:layout_alignTop="@+id/remove"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/color"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView"
    android:layout_alignStart="@+id/size"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/title" />

<TextView
    android:id="@+id/size"
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:layout_above="@+id/textView18"
    android:layout_marginEnd="26dp"
    android:layout_toStartOf="@+id/remove"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/color" />


<Button
    android:id="@+id/remove"
    android:layout_width="46dp"
    android:layout_height="67dp"
    android:layout_alignBottom="@+id/size"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="13dp"
    android:background="#5d8f37"
    android:text="X"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

 </RelativeLayout>

5 个答案:

答案 0 :(得分:2)

 for (String s : ll.split(",")) {

        int pid = Integer.parseInt(s);
        myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
        lv.setAdapter(myadapter);
        }

你在内部设置适配器。这是完全错误的。将此代码放在Loop之外。 还有一件事是升级到RecyclerView的时候了。它可以轻松,更好地实现ViewHolder Pattern。

 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);

这样做,如果您的数据集构建不正确,那么您使用错误的数据集作为列表。或者你可能需要一个ExpandableListView。首先找出dataSet,它是&#39; ArrayList&#39;。

答案 1 :(得分:2)

您需要将cartxml hight设为android:layout_height="wrap_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().

答案 2 :(得分:1)

在getCount()中打印列表大小,以确保列表是否在内部发送。检查你的项目的layout_height,它应该是wrap_content。

答案 3 :(得分:1)

因为您正在为每个项目设置适配器。

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()

<强>解决方案: 收集循环中的记录列表,您可以继续在数组列表中添加产品详细信息,然后在适配器的外部循环中设置最终列表。

答案 4 :(得分:1)

检查您的cartxml布局文件。根RelativeLayout高度为wrap_content,否则将占用单个项目的所有可用空间。在android:layout_width="wrap_content"

中使用RelativeLayoutin cartxml