2017-12-20 4 views
2

Hallo meine Customlist-Ansicht zeigt nur einen Datensatz. Alles ist in Ordnung, es zeigt mehrere Datensätze in der Konsole, aber wenn ich versuche, die Datensätze in ListView zeigen, zeigt es nur letzten Datensatz seiner überschreiben ersten Datensatz, derzeit gibt es zwei Datensätze, die in meiner Konsole angezeigt werden, aber in ListView zeigt es nur letzten Datensatz, Ich suche seit gestern eine Lösung, ich habe mehr als 6 Stunden damit verbracht, dieses Problem zu lösen.Benutzerdefiniertes ListView zeigt nur einen Datensatz an

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

Klasse von wo ich bin immer Datenbankeintrag

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

     } 

Activity_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> 

210 Cart.xml

<?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> 

Konsolenausgabe zeigt zwei Datensätze

enter code here System.out: ===pids1 System.out: ==database record[ProductBean{id='', title='ddd', color='dff', size='Select Size', type='Shart', cost='ddf', imagesList=null System.out: ===pids2 System.out: ==database record[ProductBean{id='', title='aa', color='aa', size='Select Size', type='Select Type', cost='aa', imagesList=null}] 
+0

'cartxml' zeigen Sie Ihre Layout-Datei –

+0

i XML-Datei des Adapters und Aktivität hinzugefügt haben auch –

Antwort

2

Sie benötigen cartxml Höhe zu android:layout_height="wrap_content"

wie unten Code

<?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"> 

EDIT

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(). 
+0

ich war das bro ich lass es mich wieder versuchen –

+0

zeigt immer noch einen Datensatz –

+0

@BakhtiyarHussain check updated ans Bruder –

1

druckt Ihre Liste Größe in getCount(), um sicher zu sein, wenn die Liste nach innen gesendet wird. Überprüfen Sie die Layout_Höhe Ihres Elements, es sollte wrap_content sein.

+0

überprüfen Sie bitte, wie Sie dies tun? –

+0

Protokollieren Sie die Array-Listengröße - 'Log.d (' TAG ', "size -" + list.size())' in der Methode getCount(). Die erste Ansicht in Ihrem Layout 'R.layout.cartxml' sollte die Höhe 'wrap_content' haben. – NateshR

1

Weil Sie Adapter jedes Element in Schleife einstellen.

so dauert es letzte pid von der Iteration und holt Produktdetails, die Sie im Kartenadapter einstellen.

Teil falsch ist: Sie anrufen setAdapter() bei jeder Iteration in der Schleife

Lösung: Sammeln Sie die Liste der Datensätze in einer Schleife, können Sie halten Produktdetails in der Array-Liste hinzufügen und Legen Sie dann die endgültige Liste außerhalb der Schleife im Adapter fest.

+0

Jetzt rufe ich lv.setAdapter (myadapter); außerhalb der Schleife, aber zeigt immer noch nur einen Datensatz –

+0

zeigen Sie mir Ihre PID-Datensätze und Struktur, wäre in der Lage, Ihnen schnell zu helfen! –

+0

Ich habe Konsolenausgabe –

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

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

Sie setzen Adapter in Schleife. Was ist völlig falsch. Setzen Sie diesen Code außerhalb von Loop. Und noch eine Sache Es ist Zeit für ein Upgrade auf RecyclerView. Seine einfache und bessere Implementierung von ViewHolder Pattern.

Gehen Sie so vor, und wenn Ihr Dataset nicht ordnungsgemäß erstellt wird, dann verwenden Sie einen falschen Datensatz für Ihre Liste. Oder wahrscheinlich benötigen Sie eine ExpandableListView. Finde zuerst das DataSet heraus, welches 'ArrayList' ist.

+0

hinzugefügt, wenn ich Adapter in Schleife dann nicht stelle, wie ich mehrere Datensätze erhalten kann, weil PID zwei Datensätze hat –

+0

Jedes Mal, wenn Sie 'setAdapter' auf demselben ListView aufrufen, wird der vorherige überschreiben. Die Lösung besteht darin, die Liste der Datensätze in der Schleife zu erstellen und dann die äußere Schleife des Adapters festzulegen. – ADM

+0

bro ich tat dies Ich habe gerade lv.setAdapter (myadapter) außerhalb der Schleife, aber es zeigt immer noch nur den letzten Datensatz –

1

Überprüfen Sie Ihre cartxml Layout-Datei zu machen. Die Wurzel RelativeLayout Höhe wird wrap_content sein sonst wird es den ganzen verfügbaren Platz für ein einzelnes Element nehmen. Verwenden Sie android:layout_width="wrap_content" für RelativeLayoutin in Ihrem cartxml

+0

erledigt , aber immer noch eine Aufzeichnung –

Verwandte Themen