我正在創建一個android應用程序。我有一個陣列如下所示:如何在arraylist中以不同textview顯示數組的每個元素
Array: {item1, item2, item3, item4, item5, item6, item7, item8, item9, item10.........};
我想在列表視圖中顯示這些項目。我的列表視圖的結構應如下所示:
row1: item1 item2 item3 item4 item5
row2: item6 item7 item8 item9 item10
row3: item11 .... .... ..... ...... etc
我已嘗試使用陣列適配器和基座適配器。每一個項目我收到列表視圖,如下圖所示:
item1
item2
item3
item4
item5
item6
item7
item8
....
,我已經試過如下圖所示的代碼:
使用數組適配器:
字符串數組是searchData
ArrayAdapter arrayAdapter = new ArrayAdapter(getApplicationContext(), R.layout.custom_search_row, R.id.textView3, searchData);
mListView.setAdapter(arrayAdapter);
使用基本適配器:
class SingleRow {
String mFetchedData1;
// String mFetchedData2;
// String mFetchedData3;
// String mFetchedData4;
// String mFetchedData5;
SingleRow(String mFetchedData1){
this.mFetchedData1 = mFetchedData1;
// this.mFetchedData2 = mFetchedData2;
// this.mFetchedData3 = mFetchedData3;
// this.mFetchedData4 = mFetchedData4;
// this.mFetchedData5 = mFetchedData5;
}
}
class MyAdapter extends BaseAdapter {
ArrayList<SingleRow> list;
Context context;
MyAdapter(Context c){
context = c;
list = new ArrayList<SingleRow>();
String[] mFetchedData = searchData;
for (int i=0; i<4; i++){
list.add(new SingleRow(mFetchedData[i]));
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_search_row, parent, false);
TextView title = (TextView) row.findViewById(R.id.textView3);
SingleRow temp = list.get(position);
title.setText(temp.mFetchedData1);
return row;
}
}
mListView.setAdapter(new MyAdapter(getApplicationContext()));
我的列表視圖的單行如下圖所示:
custom_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:id="@+id/textView4"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:id="@+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:id="@+id/textView6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:id="@+id/textView7"
/>
</LinearLayout>
</HorizontalScrollView>
如果任何人有任何想法,請幫助我。所有建議都歡迎。如果您需要任何信息,請讓我知道。
要麼你可以去網格佈局由@Muhammad在答覆中提到,或者你去自定義適配器並查看列表/回收視圖一起。事實上,在回收站視圖中,您也可以實現網格 –