简体   繁体   中英

How to set Recyclview items on TextView in android

在此处输入图像描述

This is my main screen and i want when i click on Select All checkbox then check all checkboxes and set all checkbox's text on below textView(Just text). I successfully select all checkboxes but can't set text on textbox with separate commas, with the help of adapter i get the list of whole data but can't set it on main activity textview layout. plz suggest me any helpful way for done this. Thnakyou.

public class MainActivity extends AppCompatActivity {
CheckBox checkboxAll;
EditText editSearch;
RecyclerView recyclerView;
TextView dataList;
MainAdapter mainAdapter;
DataAdapter dataAdapter;
List<MainModel> list;
List<String> showList = new ArrayList<>();
SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedPreferences = this.getSharedPreferences("prefs", Context.MODE_PRIVATE);
    recyclerView = findViewById(R.id.recyclerView);
    checkboxAll = findViewById(R.id.checkboxAll);
    dataList = findViewById(R.id.dataList);
    editSearch = findViewById(R.id.editSearch);

    editSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            String editValue = String.valueOf(charSequence);
            filter(editValue);

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    checkboxAll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (checkboxAll.isChecked()) {
                mainAdapter.selectAll();
            } else {
                mainAdapter.unselectAll();
            }
        }
    });

    list = new ArrayList<>();
    String[] cityname = getResources().getStringArray(R.array.cityList);

    for (int i = 0; i < cityname.length; i++) {
        MainModel mainModel = new MainModel(false, cityname[i]);
        list.add(mainModel);
    }
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    mainAdapter = new MainAdapter(MainActivity.this, list);
    recyclerView.setAdapter(mainAdapter);
}



private void filter(String searchValue) {
    ArrayList<MainModel> filteredList = new ArrayList<>();
    for (MainModel item : list) {
        if (item.getCityName().toLowerCase().contains(searchValue.toLowerCase())) {
            filteredList.add(item);
        }
    }
    mainAdapter.filterList(filteredList);
}

}

Adapter is:-

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

Context context;
List<MainModel> cityList = new ArrayList<>();
int checkValue = 0;
List<String> allValues;
SharedPreferences sharedPreferences;

public MainAdapter(Context context, List<MainModel> list) {

    sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
    this.context = context;
    this.cityList = list;
}

public void selectAll() {
    checkValue = 1;
    notifyDataSetChanged();
}

public void unselectAll() {
    checkValue = 2;
    notifyDataSetChanged();
}

@NonNull
@Override
public MainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(context).inflate(R.layout.list_items, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int position) {

    holder.itemName.setText(cityList.get(position).getCityName());

    if (checkValue == 1) {
        holder.itemCheckBox.setChecked(true);

    } else if (checkValue == 2) {
        holder.itemCheckBox.setChecked(false);
    }
}


@Override
public int getItemCount() {
    return cityList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    CheckBox itemCheckBox;
    TextView itemName;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        itemCheckBox = itemView.findViewById(R.id.itemCheckBox);
        itemName = itemView.findViewById(R.id.itemName);
    }
}

public void filterList(ArrayList<MainModel> filteredList) {
    cityList = filteredList;
    notifyDataSetChanged();
}

}

You can acheive it using Interface, I am addding below code snipet

class MainActivity extends AppCompatActivity implements MainAdapter.MyRecyclerClickListener {


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    MainAdapter mainAdapter = new MainAdapter(MainActivity.this, list);
    recyclerView.setAdapter(mainAdapter);

    mainAdapter.setClickListener(this);


}

@Override
public void onAllItemSelected(String text) {
    // set on TextView
}

@Override
public void onAllItemUnSelected(String text) {
    // set on TextView toi clear all previous value
}

}

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    private MyRecyclerClickListener myRecyclerClickListener;
    
    public void selectAll() {
        checkValue = 1;
        StringBuilder stringBuilder = new StringBuilder();
        for (MainModel cityModel : cityList) {
            stringBuilder.append(cityModel.getCityName()).append(",");

        }
        myRecyclerClickListener.onAllItemSelected(stringBuilder.toString());
        notifyDataSetChanged();
    }

    public void unselectAll() {
        checkValue = 2;
        myRecyclerClickListener.onAllItemUnSelected("");
        notifyDataSetChanged();
    }
    
    public void  setClickListener(MyRecyclerClickListener myRecyclerClickListener){
        this.myRecyclerClickListener = myRecyclerClickListener
    }


    public  interface MyRecyclerClickListener{
        void onAllItemSelected(String text);
        void onAllItemUnSelected(String text);
    }

}

On the event when all check boxes are selected you update an observable field which is binded to your text view. You post the updated string to that observable and you should see them on the text view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM