2017-11-30 13 views
-2

こんにちは、私はアンドロイド開発の問題の1つを持っています。私はまだアンドロイドアプリを開発しています。私はアンドロイドリストビューの問題に直面しています。2つのデータ項目を1つのリストビューに表示する方法

私の問題は、私は1つのリストビューに2つのデータ項目を表示することです。

Recipt No :Date 
001  :2017/11/30 

だから今は、負荷1つのデータ要素のためのArrayadapterを使用します。このような 。 しかし、私はリストビューで2つのデータをロードしたい

以下私はリストビューにロードする1つのデータのJavaコードを入れます。

public void showList() 
{ 
    String[] ides = new String[de1.size()];  

    for (int i = 0; i < de1.size(); i++) { 

     ides[i] = de1.get(i).getId().toString(); 



    } 
    ArrayAdapter adapter = new ArrayAdapter<String>(depositDetails.this,R.layout.depositlists,R.id.b,ides); 


     listViewlist.setAdapter(adapter); 



} 

XMLコードの一部

<LinearLayout 
     android:layout_width="500dp" 
     android:layout_height="67dp" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="8dp" 
     android:orientation="horizontal" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_bias="0.060000002"> 


     <android.support.constraint.ConstraintLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 




      <TextView 

       android:id="@+id/b" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="8dp" 
       android:layout_marginEnd="8dp" 
       android:layout_marginLeft="8dp" 
       android:layout_marginRight="8dp" 
       android:layout_marginStart="8dp" 
       android:layout_marginTop="8dp" 
       android:ellipsize="end" 
       android:gravity="center" 
       android:maxLength="15" 
       android:maxLines="2" 
       android:text="Deposit Index" 
       android:textColor="#c60a0a" 
       android:textSize="18sp" 
       android:textStyle="bold" 
       app:layout_constraintBottom_toBottomOf="parent" 
       app:layout_constraintHorizontal_bias="0.037" 
       app:layout_constraintLeft_toRightOf="@+id/imageViewProduct" 
       app:layout_constraintRight_toRightOf="parent" 
       app:layout_constraintTop_toTopOf="parent" 
       app:layout_constraintVertical_bias="0.0" /> 



      <TextView 
       android:id="@+id/Status" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="8dp" 
       android:layout_marginLeft="8dp" 
       android:layout_marginRight="8dp" 
       android:layout_marginTop="0dp" 
       android:text="TextView" 
       android:textColor="@color/ButtonCol" 
       android:textStyle="bold" 
       app:layout_constraintBottom_toBottomOf="parent" 
       app:layout_constraintHorizontal_bias="0.03" 
       app:layout_constraintLeft_toRightOf="@+id/imageViewProduct" 
       app:layout_constraintRight_toRightOf="parent" 
       app:layout_constraintTop_toBottomOf="@+id/b" 
       app:layout_constraintVertical_bias="0.555" /> 
     </android.support.constraint.ConstraintLayout> 

    </LinearLayout> 
+1

..あなたはこれを参照することができます代わりに 'ArrayAdapter'のアダプタのクラスを作成し、その内の必要なプロパティを定義します** [ここの例](http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown_example)** –

+2

アイテムにカスタムレイアウトとアダプターを使用する必要があります。詳細については、**の[画像とテキストを使用したAndroidカスタムリストビュー](https://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/)* * –

答えて

2

使用カスタム・アダプタ

public class CustomAdapter extends ArrayAdapter<User> { 

Context context; 
ArrayList<User> userArrayList; 
public CustomAdapter(Context context,ArrayList<User> userArrayList) { 
    super(context, R.layout.depositlists, userArrayList); 
    this.context=context; 
    this.userArrayList=userArrayList; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 


    if (convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.depositlists, parent, false); 
    } 

    User user = getItem(position); 


    TextView b = (TextView) convertView.findViewById(R.id.b); 
    TextView Status = (TextView) convertView.findViewById(R.id.Status); 

    b.setText(user.getReciptNo()); 
    Status.setText(user.getDate()); 

    return convertView; 
} 
} 


Replace showList method 

public void showList() 
{ 
    ArrayList<User> users = new ArrayList<>(); 

    for (int i = 0; i < de1.size(); i++) { 

     users.add(new User(i,de1.get(i).getId().toString())); 

    } 
    CustomAdapter adapter = new CustomAdapter(depositDetails.this,users); 

    listViewlist.setAdapter(adapter); 
    } 


Add User Class 


public class User { 
String ReciptNo; 
String Date; 

public User(String reciptNo, String date) { 
    ReciptNo = reciptNo; 
    Date = date; 
} 

public String getReciptNo() { 
    return ReciptNo; 
} 

public void setReciptNo(String reciptNo) { 
    ReciptNo = reciptNo; 
} 

public String getDate() { 
    return Date; 
} 

public void setDate(String date) { 
    Date = date; 
} 
} 
関連する問題