Thorvald
Thorvald

Reputation: 3563

Android - storing two arrays in a single RecyclerView

I have two arrays :

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");
}

what I want to achieve is storing both lists in a singler RecyclerView in a way that the list with elements are shown on the left side of the screen and the items list are shown in the right side (like shown in the picture below) I am now using two listviews and willing to convert to a single recyclerView PS. preferably maintain the structure of the arrays

EDIT / THIS IS THE TUTORIAL I AM GOING TO USE RecyclerView Tutorial

enter image description here

Upvotes: 0

Views: 2400

Answers (1)

Sohail Zahid
Sohail Zahid

Reputation: 8149

You can use Pojo class for this

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;
   }
}

then create DataHolder list.

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

Add item like this.

DataHolder item = new DataHolder ();

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

bothItemDataList.add(item);

Update Section:

enter image description here

MainActivity:

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>

DataHolder:

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;
    }
}

Update:

 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);
        }
    }

Upvotes: 3

Related Questions