我使用布尔概念来交换回收物品中的文本字段,
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我使用布尔概念来交换回收物品中的文本字段,相关的知识,希望对你有一定的参考价值。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ContactViewHolder> {
static final String myTag = "DocsUpload";
ArrayList<RecyclerItem> listItems = new ArrayList<>();
RecyclerView.ViewHolder holder;
int clickcount=0;
private Context mContext;
List list = new ArrayList();
Boolean signUpModeActive = true;
public MyAdapter(ArrayList<RecyclerItem> listItems, Context mContext) {
this.listItems = listItems;
this.mContext = mContext;
}
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
ContactViewHolder contactViewHolder = new ContactViewHolder(view, mContext, listItems);
return contactViewHolder;
}
@Override
public void onBindViewHolder(final ContactViewHolder holder, final int position) {
RecyclerItem itemList = listItems.get(position);
holder.txtTitle.setText(itemList.getTitle());
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtTitle;
public CheckBox checkBox;
public TextView txtDescription;
public ImageView txtOptionDigit;
ArrayList<RecyclerItem> listitems = new ArrayList<RecyclerItem>();
Context ctx;
public ContactViewHolder(View view, Context ctx, ArrayList<RecyclerItem> listitems) {
super(view);
this.listitems = listitems;
this.ctx = ctx;
view.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBoxxml);
}
boolean m=false;
@Override
public void onClick(View v) {
clickcount=clickcount+1;
Log.e("count", String.valueOf(clickcount));
int position = getAdapterPosition();
final RecyclerItem listitems = this.listitems.get(position);
final String getname = listitems.getTitle();
if (signUpModeActive=true) {
signUpModeActive = false;
listItems.get(position).setTitle("");
listItems.get(position).setTitle(getname + " " +"absent");
} else {
signUpModeActive = true;
listItems.get(position).setTitle("");
listItems.get(position).setTitle(getname + " " +"present");
}
notifyDataSetChanged();
}
}
public void getsetadapt(String pos) {
list.add(pos);
Log.e("listdata", String.valueOf(list));
for (int i=0;i < list.size();i++)
{
Log.i("Value "+i, String.valueOf(list.get(i)));
i++;
}
}
当我点击任何列表项时,它想要添加缺少现有文本,如果我再次点击它想要替换缺席现在..可以任何人知道我哪里出错...如果在我的模拟器上运行此代码它工作和它正在添加不替换的文本。例如,如果我点击学生oviya它添加文本缺席oviya附近但是如果我再次点击它不是替换它正在一次又一次地添加礼物
只需将适配器更改为以下适配器,它就可以正常工作
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ContactViewHolder> {
static final String myTag = "DocsUpload";
ArrayList<RecyclerItem> listItems = new ArrayList<>();
RecyclerView.ViewHolder holder;
int clickcount = 0;
private Context mContext;
List list = new ArrayList();
Boolean signUpModeActive = true;
public MyAdapter(ArrayList<RecyclerItem> listItems, Context mContext) {
this.listItems = listItems;
this.mContext = mContext;
}
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_2, parent, false);
ContactViewHolder contactViewHolder = new ContactViewHolder(view, mContext, listItems);
return contactViewHolder;
}
@Override
public void onBindViewHolder(final ContactViewHolder holder, final int position) {
RecyclerItem itemList = listItems.get(position);
holder.txtTitle.setText(itemList.getTitle());
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtTitle;
public CheckBox checkBox;
public TextView txtDescription;
public ImageView txtOptionDigit;
ArrayList<RecyclerItem> listitems = new ArrayList<RecyclerItem>();
Context ctx;
public ContactViewHolder(View view, Context ctx, ArrayList<RecyclerItem> listitems) {
super(view);
this.listitems = listitems;
this.ctx = ctx;
view.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBoxxml);
}
boolean m = false;
@Override
public void onClick(View v) {
clickcount = clickcount + 1;
Log.e("count", String.valueOf(clickcount));
int position = getAdapterPosition();
final RecyclerItem listitems = this.listitems.get(position);
final String getname = listitems.getTitle().replace(" absent" , "").replace(" present" , "");
if (signUpModeActive ) {
signUpModeActive = false;
listItems.get(position).setTitle(getname + " " + "absent");
} else {
signUpModeActive = true;
listItems.get(position).setTitle(getname + " " + "present");
}
notifyDataSetChanged();
}
}
public void getsetadapt(String pos) {
list.add(pos);
Log.e("listdata", String.valueOf(list));
for (int i = 0; i < list.size(); i++) {
Log.i("Value " + i, String.valueOf(list.get(i)));
i++;
}
}
}
实际上问题是排队的
final RecyclerItem listitems = this.listitems.get(position);
final String getname = listitems.getTitle();
你把文字设置为
listItems.get(position).setTitle(getname + " " + "absent");
要么
listItems.get(position).setTitle(getname + " " + "present");
这将导致名称缺席或名称作为结果出现,并将添加新的缺席或呈现给它
您的代码中存在不同的问题。
1首先,您的变量未正确命名,因此非常混乱。例如,你使用几乎完全相同的listitems
和listItems
。加上listitems
两次使用两个不同的变量,在相同的范围内使用完全相同的情况!
变量应描述其内容(您应该了解读取名称时的内容,例如学生列表不会被命名为'list'而是'student'或'studentsList')并且您应该避免使用2个变量同一范围内的完全相同的名称(如果你必须开始做类似this.myvariable = myvariable
的事情,那就意味着你的实现中出了问题)。
2以不同方式管理标题
您想要的是名称+状态取决于是否存在。
所以当你这样做时:
listItems.get(position).setTitle("");
你只是将名称松散为listItems,listitems是完全相同的列表。
所以我建议在getName()
上使用RecyclerItem
方法来返回名称。删除set/getTitle
。
然后用布尔值管理当前/缺席。基本上,就像那样:
if (signUpModeActive = true) {
signUpModeActive = false;
item.setIsPresent(false);
} else {
signUpModeActive = true;
item.setIsPresent(true);
}
这意味着你需要在setIsPresent
中添加RecyclerItem
方法。
然后你,在onBindViewHolder
你做:
@Override
public void onBindViewHolder(final ContactViewHolder holder, final int position) {
RecyclerItem item = mListItems.get(position);
String name = item.getName();
String status = item.isPresent() ? "present" : "absent";
String title = name + " " + status;
holder.txtTitle.setText(title);
}
使用带占位符的字符串资源来管理状态文本会更好。
以上是关于我使用布尔概念来交换回收物品中的文本字段,的主要内容,如果未能解决你的问题,请参考以下文章