如何在 RecyclerView 中显示 ArrayList 数据?

huangapple go评论92阅读模式
英文:

How to show arraylist data in recyclerview?

问题

我有一个包含重复数据的ArrayList我想在RecyclerView的第一行中显示前5个项目然后在第二行中显示接下来的5个项目依此类推如何在RecyclerView中实现这一点

> Arraylist: [2013-04-01, 截至2013年3月31日的OB国际更新, 0, 0, 0,
> 2013-09-10082013应付月份的继续7802395412014-03-28
> 032014应付月份的继续7802395412014-02-03012014应付月份的继续7802395412013-07-26072013应付月份的继续780239541]

**DataAdapter.java**

    public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder> {
        private AppCompatActivity activity;
        List<String> data = new ArrayList();
        private LayoutInflater inflater;
    
        class MyViewHolder extends RecyclerView.ViewHolder {
            private final View saparator;
            private final TextView txtLabel;
    
            public MyViewHolder(View itemView) {
                super(itemView);
                this.txtLabel = (TextView) itemView.findViewById(R.id.detail_parameter);
                this.saparator = itemView.findViewById(R.id.detail_value);
            }
        }
    
        public DataAdapter(AppCompatActivity activity, List<String> data) {
            this.activity = activity;
            this.inflater = LayoutInflater.from(activity);
            this.data = data;
        }
    
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(this.inflater.inflate(R.layout.pfpassbook_item, parent, false));
        }
    
        @SuppressLint("WrongConstant")
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.txtLabel.setText((CharSequence) this.data.get(position));
            holder.txtLabel.setTextSize(15.0f);
            holder.txtLabel.setTextColor(this.activity.getResources().getColor(R.color.colorAccent));
    
        }
    
        public int getItemCount() {
            return this.data.size();
        }
    }

**MainActivity.java**

    public class MainActivity extends AppCompatActivity {
        private RecyclerView rcList;
        public String platenum = null;
        RelativeLayout idForSaveView;
        public static ArrayList<String> statement;
    
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView((int) R.layout.pfpassbook);
            statement = (ArrayList<String>) getIntent().getSerializableExtra("statement");
                  setView();
            idForSaveView = (RelativeLayout) findViewById(R.id.relate);
        }
    
        private void setView() {
            this.rcList = (RecyclerView) findViewById(R.id.detail_recyclerview);
            this.rcList.setLayoutManager(new GridLayoutManager(this, 5));
            this.rcList.setHasFixedSize(true);
    
            this.rcList.setAdapter(new DataAdapter(DetailActivity.this, statement));
        }
    
    }

pfpassbook_item.xml

    <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_margin="0dp"
        app:cardElevation="0dp"
        app:cardBackgroundColor="@android:color/transparent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/design_default_color_error"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/detail_parameter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:paddingTop="2dp"
                android:paddingBottom="2dp" />
            
    
        </LinearLayout>
    </androidx.cardview.widget.CardView>
英文:

I have an ArrayList with repeated data, I want to show the first 5 items in the first row of recyclerview then the second 5 items in the second row, and so on. How can I achieve this in Recyclerview?

> Arraylist: [2013-04-01, OB Int. Updated upto 31/03/2013, 0, 0, 0,
> 2013-09-10, Cont. For Due-Month 082013, 780, 239, 541, 2014-03-28,
> Cont. For Due-Month 032014, 780, 239, 541, 2014-02-03, Cont. For
> Due-Month 012014, 780, 239, 541, 2013-07-26, Cont. For Due-Month
> 072013, 780, 239, 541]

DataAdapter.java

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder> {
private AppCompatActivity activity;
List<String> data = new ArrayList();
private LayoutInflater inflater;
class MyViewHolder extends RecyclerView.ViewHolder {
private final View saparator;
private final TextView txtLabel;
public MyViewHolder(View itemView) {
super(itemView);
this.txtLabel = (TextView) itemView.findViewById(R.id.detail_parameter);
this.saparator = itemView.findViewById(R.id.detail_value);
}
}
public DataAdapter(AppCompatActivity activity, List<String> data) {
this.activity = activity;
this.inflater = LayoutInflater.from(activity);
this.data = data;
}
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(this.inflater.inflate(R.layout.pfpassbook_item, parent, false));
}
@SuppressLint("WrongConstant")
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.txtLabel.setText((CharSequence) this.data.get(position));
holder.txtLabel.setTextSize(15.0f);
holder.txtLabel.setTextColor(this.activity.getResources().getColor(R.color.colorAccent));
}
public int getItemCount() {
return this.data.size();
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
private RecyclerView rcList;
public String platenum = null;
RelativeLayout idForSaveView;
public static ArrayList<String> statement;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView((int) R.layout.pfpassbook);
statement = (ArrayList<String>) getIntent().getSerializableExtra("statement");
setView();
idForSaveView = (RelativeLayout) findViewById(R.id.relate);
}
private void setView() {
this.rcList = (RecyclerView) findViewById(R.id.detail_recyclerview);
this.rcList.setLayoutManager(new GridLayoutManager(this, 5));
this.rcList.setHasFixedSize(true);
this.rcList.setAdapter(new DataAdapter(DetailActivity.this, statement));
}
}

pfpassbook_item.xml

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="0dp"
app:cardElevation="0dp"
app:cardBackgroundColor="@android:color/transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/design_default_color_error"
android:orientation="horizontal">
<TextView
android:id="@+id/detail_parameter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>

答案1

得分: 2

首先,您不应该处于这种情况下。

您可以通过将这些数据转换为某些包含5个值的对象的ArrayList来修复它。

这只是为了让您入门并提供思路。

import java.util.ArrayList;

public class HelloWorld {

    public static class Model {
        String value1;
        String value2;
        String value3;
        String value4;
        String value5;

        public String getValue1() {
            return value1;
        }

        public void setValue1(String value1) {
            this.value1 = value1;
        }

        public String getValue2() {
            return value2;
        }

        public void setValue2(String value2) {
            this.value2 = value2;
        }

        public String getValue3() {
            return value3;
        }

        public void setValue3(String value3) {
            this.value3 = value3;
        }

        public String getValue4() {
            return value4;
        }

        public void setValue4(String value4) {
            this.value4 = value4;
        }

        public String getValue5() {
            return value5;
        }

        public void setValue5(String value5) {
            this.value5 = value5;
        }

        @Override
        public String toString() {
            return "Model{" +
                    "value1='" + value1 + '\'' +
                    ", value2='" + value2 + '\'' +
                    ", value3='" + value3 + '\'' +
                    ", value4='" + value4 + '\'' +
                    ", value5='" + value5 + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        System.out.println("Hello World");
        String a[] = {"2013-04-01", "OBInt.Updatedupto31/03/2013", "0", "0", "0", "2013-09-10", "Cont.ForDue-Month082013", "780", "239", "541", "2014-03-28", "Cont.ForDue-Month032014", "780", "239", "541", "2014-02-03", "Cont.ForDue-Month012014", "780", "239", "541", "2013-07-26", "Cont.ForDue-Month072013", "780", "239", "541"};

        ArrayList<Model> arrayList = new ArrayList<>();

        for (int i = 0; i < a.length; i = i + 5) {
            Model m = new Model();

            m.setValue1(a[i]);
            m.setValue2(a[i + 1]);
            m.setValue3(a[i + 2]);
            m.setValue4(a[i + 3]);
            m.setValue5(a[i + 4]);
            arrayList.add(m);
        }

        System.out.println(arrayList.size() + "");

        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
    }
}

在提供的数组大小将是5的倍数的情况下,您可以将数据清理并设置到RecyclerView中,这是一种很好的做法,因为您可以在将其设置到RecyclerView之前捕获错误。通过这样做,您可以在CustomAdapter中使用它,为您的项目创建所需的视图。

另外,请注意您提供的关于DataAdapter、setViewMethod等部分的信息,这些部分不需要翻译,因为它们是代码的组成部分,不属于需要翻译的文本范围。

英文:

First of all you shouldn't be in this kind of situation.
You can fix it BY converting this data to some ArrayList of Objects where object will hold 5 values .
This is just to get you started and give you idea.

import java.util.ArrayList;
public class HelloWorld {
public static class Model {
String value1;
String value2;
String value3;
String value4;
String value5;
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
public String getValue3() {
return value3;
}
public void setValue3(String value3) {
this.value3 = value3;
}
public String getValue4() {
return value4;
}
public void setValue4(String value4) {
this.value4 = value4;
}
public String getValue5() {
return value5;
}
public void setValue5(String value5) {
this.value5 = value5;
}
@Override
public String toString() {
return &quot;Model{&quot; +
&quot;value1=&#39;&quot; + value1 + &#39;\&#39;&#39; +
&quot;, value2=&#39;&quot; + value2 + &#39;\&#39;&#39; +
&quot;, value3=&#39;&quot; + value3 + &#39;\&#39;&#39; +
&quot;, value4=&#39;&quot; + value4 + &#39;\&#39;&#39; +
&quot;, value5=&#39;&quot; + value5 + &#39;\&#39;&#39; +
&#39;}&#39;;
}
}
public static void main(String[] args) {
System.out.println(&quot;Hello World&quot;);
String a[] = {&quot;2013-04-01&quot;, &quot;OBInt.Updatedupto31/03/2013&quot;, &quot;0&quot;, &quot;0&quot;, &quot;0&quot;, &quot;2013-09-10&quot;, &quot;Cont.ForDue-Month082013&quot;, &quot;780&quot;, &quot;239&quot;, &quot;541&quot;, &quot;2014-03-28&quot;, &quot;Cont.ForDue-Month032014&quot;, &quot;780&quot;, &quot;239&quot;, &quot;541&quot;, &quot;2014-02-03&quot;, &quot;Cont.ForDue-Month012014&quot;, &quot;780&quot;, &quot;239&quot;, &quot;541&quot;, &quot;2013-07-26&quot;, &quot;Cont.ForDue-Month072013&quot;, &quot;780&quot;, &quot;239&quot;, &quot;541&quot;};
ArrayList&lt;Model&gt; arrayList = new ArrayList&lt;&gt;();
for (int i = 0; i &lt; a.length ; i = i + 5) {
Model m = new Model();
m.setValue1(a[i]);
m.setValue2(a[i + 1]);
m.setValue3(a[i + 2]);
m.setValue4(a[i + 3]);
m.setValue5(a[i + 4]);
arrayList.add(m);
}
System.out.println(arrayList.size() + &quot;&quot;);
for(int i =0 ;i&lt;arrayList.size();i++){
System.out.println(arrayList.get(i));
}
}
}

Provided that the size of the array will be in multiple of 5.
For me cleaning the data before setting to recyclerView is a good way to go as you can catch the errors before setting it on recyclerView.
Also by doing this you can use it in CustomAdapter to make desired View for your Items.

UPDATE 1:

DataAdapter

public class DataAdapter extends RecyclerView.Adapter&lt;DataAdapter.MyViewHolder&gt; {
private AppCompatActivity activity;
ArrayList&lt;Model&gt; data = new ArrayList();
private LayoutInflater inflater;
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private TextView textView2;
private TextView textView3;
private TextView textView4;
private TextView textView5;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById( R.id.textView );
textView2 = (TextView)itemView.findViewById( R.id.textView2 );
textView3 = (TextView)itemView.findViewById( R.id.textView3 );
textView4 = (TextView)itemView.findViewById( R.id.textView4 );
textView5 = (TextView)itemView.findViewById( R.id.textView5 );
}
}
public DataAdapter(AppCompatActivity activity, ArrayList&lt;Model&gt; data) {
this.activity = activity;
this.inflater = LayoutInflater.from(activity);
this.data = data;
}
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(this.inflater.inflate(R.layout.pfpassbook_item, parent, false));
}
public void onBindViewHolder(MyViewHolder holder, int position) {
Model m = data.get(position);
holder.textView.setText(m.getValue1());
holder.textView2.setText(m.getValue2());
holder.textView3.setText(m.getValue3());
holder.textView4.setText(m.getValue4());
holder.textView5.setText(m.getValue5());
}
public int getItemCount() {
return this.data.size();
}
}

setViewMethod:

private void setView() {
this.rcList = (RecyclerView) findViewById(R.id.detail_recyclerview);
this.rcList.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL,false));
this.rcList.setHasFixedSize(true);
String[] a = {&quot;2013-04-01&quot;, &quot;OBInt.Updatedupto31/03/2013&quot;, &quot;0&quot;, &quot;0&quot;, &quot;0&quot;, &quot;2013-09-10&quot;, &quot;Cont.ForDue-Month082013&quot;, &quot;780&quot;, &quot;239&quot;, &quot;541&quot;, &quot;2014-03-28&quot;, &quot;Cont.ForDue-Month032014&quot;, &quot;780&quot;, &quot;239&quot;, &quot;541&quot;, &quot;2014-02-03&quot;, &quot;Cont.ForDue-Month012014&quot;, &quot;780&quot;, &quot;239&quot;, &quot;541&quot;, &quot;2013-07-26&quot;, &quot;Cont.ForDue-Month072013&quot;, &quot;780&quot;, &quot;239&quot;, &quot;541&quot;};
ArrayList&lt;Model&gt; arrayList = new ArrayList&lt;&gt;();
for (int i = 0; i &lt; a.length ; i = i + 5) {
Model m = new Model();
m.setValue1(a[i]);
m.setValue2(a[i + 1]);
m.setValue3(a[i + 2]);
m.setValue4(a[i + 3]);
m.setValue5(a[i + 4]);
arrayList.add(m);
}
this.rcList.setAdapter(new DataAdapter(this, arrayList));
}

Model Class :

public  class Model {
String value1;
String value2;
String value3;
String value4;
String value5;
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
public String getValue3() {
return value3;
}
public void setValue3(String value3) {
this.value3 = value3;
}
public String getValue4() {
return value4;
}
public void setValue4(String value4) {
this.value4 = value4;
}
public String getValue5() {
return value5;
}
public void setValue5(String value5) {
this.value5 = value5;
}
@Override
public String toString() {
return &quot;Model{&quot; +
&quot;value1=&#39;&quot; + value1 + &#39;\&#39;&#39; +
&quot;, value2=&#39;&quot; + value2 + &#39;\&#39;&#39; +
&quot;, value3=&#39;&quot; + value3 + &#39;\&#39;&#39; +
&quot;, value4=&#39;&quot; + value4 + &#39;\&#39;&#39; +
&quot;, value5=&#39;&quot; + value5 + &#39;\&#39;&#39; +
&#39;}&#39;;
}
}

pfpassbook_item.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:orientation=&quot;horizontal&quot; android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;TextView
android:id=&quot;@+id/textView&quot;
android:layout_width=&quot;0dp&quot;
android:layout_weight=&quot;1&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView2&quot;
android:layout_width=&quot;0dp&quot;
android:layout_weight=&quot;1&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView3&quot;
android:layout_width=&quot;0dp&quot;
android:layout_weight=&quot;1&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView4&quot;
android:layout_width=&quot;0dp&quot;
android:layout_weight=&quot;1&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot; /&gt;
&lt;TextView
android:layout_width=&quot;0dp&quot;
android:layout_weight=&quot;1&quot;
android:id=&quot;@+id/textView5&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot; /&gt;
&lt;/LinearLayout&gt;

You can design pfpassbook_item any way you want. always bind your data in Array of objects instead of adding one by one in the array.

答案2

得分: 0

你可以使用 spanCount 来显示每行中所需的项目数量,就像这样:

layoutManager!!.spanCount = 5

在 XML 中的 RecyclerView 中,可以像这样设置:

app:spanCount="5"
英文:

You can use spanCount for showing desired quantity of items in a row like this

      layoutManager!!.spanCount = 5

And in XML inside your recyclerview like this

            app:spanCount=&quot;5&quot;

答案3

得分: 0

你可以将StaggeredGridLayoutManager用作RecyclerView的布局。

XML

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="androidx.recyclerview.widget.StaggeredGridLayoutManager"
    app:spanCount="5" />

这里的spanCount定义了每行要显示的项目数。

如果你想使用类进行定义:

val layoutManager = StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL)
recyclerView.layoutManager = layoutManager
英文:

You can use StaggeredGridLayoutManager as the layout of recyclerview

> XML

 &lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;+@id/recyclerView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
app:layoutManager=&quot;androidx.recyclerview.widget.StaggeredGridLayoutManager&quot;
app:spanCount=&quot;5&quot; /&gt;

Here spanCount defines the number of items you want to display in a row

If you want to define using class

val layoutManager = StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL)
recyclerView.layoutManager = layoutManager

huangapple
  • 本文由 发表于 2020年10月21日 12:44:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64456826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定