如何在arraylist中的不同textview中显示数组的每个元素

时间:2015-09-30 17:13:08

标签: android arrays listview android-custom-view baseadapter

我正在创建一个Android应用程序。我有一个如下所示的数组:

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

我尝试过的代码如下所示:

使用阵列适配器:

String Array是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>

如果有任何人有任何想法,请帮助我。欢迎所有建议。如果您需要任何信息,请告诉我。

2 个答案:

答案 0 :(得分:0)

尝试将GridLayout与自定义列选项一起使用。将ListView和Adapter更改为GridView。

答案 1 :(得分:0)

使用自定义布局和CustomAdapter

public class CustomAdapter extends BaseAdapter {

    Context context;
    String[] values;
    public static startposition=0;
    public CustomAdapter(Context context,String[] values) {
        this.context=context;
        this.values=values
    }



    @Override
    public View getView(int index, View view, final ViewGroup parent) {

        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.custom_layout,parent, false);
        }

        TextView textView3= (TextView)view.findViewById(R.id.textView3);
        TextView textView4= (TextView)view.findViewById(R.id.textView4);
        TextView textView5 = (TextView) view.findViewById(R.id.textView5);
        TextView textView6= (TextView)view.findViewById(R.id.textView7);
        TextView textView7= (TextView)view.findViewById(R.id.textView7);
        if(position<values.length-5)
        {
        int i=position+startposition;
        startposition+=5;
        textView3.setText(values[i]);
         textView4.setText(values[i+1]);
          textView5.setText(values[i+2]);
         textView6.setText(values[i+3]);
         textView7.setText(values[i+4]);      
        }

        return view;
    }
}