gpt4 book ai didi

java - Android - 在单个 RecyclerView 中存储两个数组

转载 作者:行者123 更新时间:2023-12-01 21:20:27 25 4
gpt4 key购买 nike

我有两个数组:

private void InitData(){
list = new ArrayList<String>();
list.add("element 1");
list.add("element 2");
list.add("element 3");
list.add("element 4");
list.add("element 5");
}
private void InitData2(){
list2 = new ArrayList<String>();
list.add("item 1");
list.add("item 2");
list.add("item 3");
list.add("item 4");
list.add("item 5");
}

我想要实现的是将两个列表存储在单个 RecyclerView 中,其中包含元素的列表显示在屏幕左侧,项目列表显示在右侧(如下图所示) )我现在使用两个 ListView 并愿意转换为单个 recyclerView附言。最好保持数组的结构

编辑/这是我要使用的教程 RecyclerView Tutorial

enter image description here

最佳答案

您可以使用 Pojo 类来实现此目的

public class DataHolder {

String item1,item2;

public DataHolder(){

}

String getItem1(){
retrun item1;
}

String getItem2(){
retrun item2;
}

String setItem1(String item1){
this.item1 = item1;
}

String setItem2(String item2){
this.item2 = item2;
}
}

然后创建DataHolder列表。

ArrayList<DataHolder> bothItemDataList =new ArrayList<DataHolder>();

添加这样的项目。

DataHolder item = new DataHolder ();

item.setItem1("element 1");
item.setItem2("element 1");

bothItemDataList.add(item);

更新部分:

enter image description here

主要 Activity :

public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
public EditText search;
// private List<String> list = new ArrayList<String>();
private List<DataHolder> list = new ArrayList<DataHolder>();
SimpleAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = (EditText) findViewById(R.id.search);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

countryList(); // in this method, Create a list of items.

// call the adapter with argument list of items and context.
mAdapter = new SimpleAdapter(list, this);
mRecyclerView.setAdapter(mAdapter);

addTextListener();

}

// this method is used to create list of items.
public void countryList() {

DataHolder item = new DataHolder();
item.setName("Afghanistan");
item.setTranslation("demo trans1");

list.add(item);

DataHolder item1 = new DataHolder();
item1.setName("Paksitan");
item1.setTranslation("my pakistan");

list.add(item1);
// list.add("Afghanistan");
// list.add("Albania");
// list.add("Algeria");
// list.add("Bangladesh");
// list.add("Belarus");
// list.add("Canada");
// list.add("Cape Verde");
// list.add("Central African Republic");
// list.add("Denmark");
// list.add("Dominican Republic");
// list.add("Egypt");
// list.add("France");
// list.add("Germany");
// list.add("Hong Kong");
// list.add("India");
// list.add("Iceland");

}


public void addTextListener() {

search.addTextChangedListener(new TextWatcher() {

public void afterTextChanged(Editable s) {
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

public void onTextChanged(CharSequence query, int start, int before, int count) {

query = query.toString().toLowerCase();

final List<DataHolder> filteredList = new ArrayList<DataHolder>();

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

final String text = list.get(i).getName().toLowerCase();
if (text.contains(query)) {

filteredList.add(list.get(i));
}
}

mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
mAdapter = new SimpleAdapter(filteredList, MainActivity.this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged(); // data set changed
}
});
}
}

SimpleAdapter:

public class SimpleAdapter extends
RecyclerView.Adapter<SimpleAdapter.MyViewHolder> {

private List<DataHolder> list_item;
public Context mcontext;


public SimpleAdapter(List<DataHolder> list, Context context) {

list_item = list;
mcontext = context;
}

// Called when RecyclerView needs a new RecyclerView.ViewHolder of the given type to represent an item.
@Override
public SimpleAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a layout
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_item, null);

MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}

// Called by RecyclerView to display the data at the specified position.
@Override
public void onBindViewHolder(final MyViewHolder viewHolder, final int position) {


viewHolder.country_name.setText(list_item.get(position).getName());
viewHolder.country_translation.setText(list_item.get(position).getTranslation());

viewHolder.country_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Toast.makeText(mcontext, list_item.get(position).getName(),
Toast.LENGTH_LONG).show();
}
});

}

// initializes some private fields to be used by RecyclerView.
public static class MyViewHolder extends RecyclerView.ViewHolder {

public TextView country_name, country_translation;

public MyViewHolder(View itemLayoutView) {
super(itemLayoutView);

country_name = (TextView) itemLayoutView.findViewById(R.id.country_name);
country_translation = (TextView) itemLayoutView.findViewById(R.id.country_translation);

}
}

//Returns the total number of items in the data set hold by the adapter.
@Override
public int getItemCount() {
return list_item.size();
}

}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.numetriclabz.androidsearch.MainActivity">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:id="@+id/country_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Sohail"
android:textColor="#0b0080"
android:textSize="16dp" />

<TextView
android:id="@+id/country_translation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toEndOf="@+id/country_name"
android:layout_toRightOf="@+id/country_name"
android:text="New Text"
android:textColor="#0b0080" />

</RelativeLayout>

</RelativeLayout>

数据持有者:

public class DataHolder {
String name, translation;


public DataHolder() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getTranslation() {
return translation;
}

public void setTranslation(String translation) {
this.translation = translation;
}
}

更新:

 public void countryList() {

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

DataHolder item = new DataHolder();
item.setName(i + "name");
item.setTranslation(i + "translation");

list.add(item);
}
}

关于java - Android - 在单个 RecyclerView 中存储两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39202596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com