英文:
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-10,082013应付月份的继续,780,239,541,2014-03-28,
> 032014应付月份的继续,780,239,541,2014-02-03,012014应付月份的继续,780,239,541,2013-07-26,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>
英文:
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 "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));
}
}
}
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<DataAdapter.MyViewHolder> {
private AppCompatActivity activity;
ArrayList<Model> 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<Model> 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 = {"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);
}
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 "Model{" +
"value1='" + value1 + '\'' +
", value2='" + value2 + '\'' +
", value3='" + value3 + '\'' +
", value4='" + value4 + '\'' +
", value5='" + value5 + '\'' +
'}';
}
}
pfpassbook_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/textView5"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
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="5"
答案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
<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" />
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论