简体   繁体   English

如何在 android 中的 TextView 上设置 Recyclview 项目

[英]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).这是我的主屏幕,当我点击 Select All 复选框然后选中所有复选框并将所有复选框的文本设置在 textView 下方(仅文本)时,我想要。 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.我成功 select 所有复选框,但无法在文本框上使用单独的逗号设置文本,在适配器的帮助下,我获得了整个数据的列表,但无法在主要活动 textview 布局上设置它。 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.您将更新后的字符串发布到该 observable,您应该在文本视图中看到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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