我遇到很多麻烦,我可以通过搜索找到与我的问题相关的内容,但它们似乎无法解决我的问题,而且我已尽力而为。我似乎无法为我的生活解决这个问题。
所以我有一个应用程序来跟踪大学课程。其中,我有一个按钮,可以将自定义视图持有者添加到RecyclerView。此自定义视图持有者包含两个EditText字段以及CheckBox。现在,当我点击我的按钮“添加课程”时,它将添加这三件事。这很好用。但是,当我在EditText字段中输入任何文本,然后添加另一个课程时,它会删除之前输入EditText字段的所有内容。我试图保存这些内容,非常永久,或者至少在用户删除该视图之前(点击我的“全部删除”按钮或向左滑动)。我虽然遇到了极大的困难。我一直在尝试为每个视图添加一个“TextChangedListener”,但我似乎无法让它工作。我已经尝试制作一个自定义文本监听器并将其添加到我的“onCreateViewHolder”方法中,然后在“onBindViewHolder”方法中设置文本,但这似乎并没有解决我的问题。
我认为我的另一个问题可能与那个问题有关,当我在RecyclerView中有足够的视图向下滚动时,我会在EditText字段中输入文本。然后,当我向下滚动时,我输入的文本将出现在其他EditText字段中。或者如果我检查我的CheckBox,然后向下滚动,那么也将检查其他复选框。这是一些视觉图片。
这是我添加新课程的时候,文字消失了:
这是我的滚动问题:
这也是我的文件。
首先,我的MainActivity.java:
import android.content.ClipData;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ArrayList<ListItems> itemsList = new ArrayList<ListItems>();
private RecyclerView mRecyclerView;
private MyRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// For the recycler view.
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rl);
// For the addCourse button.
final Button addCourse = (Button) findViewById(R.id.addCourse);
addCourse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListItems.createListItem(itemsList);
adapter = new MyRecyclerAdapter(MainActivity.this, itemsList);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(linearLayoutManager);
}
});
final Button clearAll = (Button) findViewById(R.id.clearAll);
clearAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// In order to clear the list.
adapter.clearAdapter();
}
});
// For the drag and drop/swipe to dismiss.
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(
new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
ItemTouchHelper.LEFT) {
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
final int fromPos = viewHolder.getAdapterPosition();
final int toPos = target.getAdapterPosition();
Collections.swap(itemsList, fromPos, toPos);
adapter.notifyItemMoved(fromPos, toPos);
return true;
}
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
adapter.onItemDismiss(viewHolder.getAdapterPosition());
}
});
itemTouchHelper.attachToRecyclerView(mRecyclerView);
} // End of onCreate
} // End of MainActivity
现在,我的适配器:
import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MyRecyclerAdapter extends RecyclerView.Adapter<CustomRowViewHolder> {
private ArrayList<ListItems> itemsList;
private Context mContext;
private int focuseditem = 0;
String afterTextChanged = "";
public MyRecyclerAdapter(Context context, ArrayList<ListItems> itemsList){
this.itemsList = itemsList;
this.mContext = context;
}
@Override
public CustomRowViewHolder onCreateViewHolder(final ViewGroup viewGroup, int position) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.new_course_row, null);
final CustomRowViewHolder holder = new CustomRowViewHolder(v, new CustomTextListener());
holder.creditsText.setInputType(InputType.TYPE_CLASS_NUMBER);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(holder.checkBox.isChecked()) {
holder.courseText.setEnabled(false);
holder.courseText.setFocusable(false);
holder.courseText.setInputType(InputType.TYPE_NULL);
holder.creditsText.setEnabled(false);
holder.creditsText.setFocusable(false);
holder.creditsText.setInputType(InputType.TYPE_NULL);
} else {
holder.courseText.setEnabled(true);
holder.courseText.setFocusable(true);
holder.courseText.setFocusableInTouchMode(true);
holder.courseText.setInputType(InputType.TYPE_CLASS_TEXT);
holder.creditsText.setEnabled(true);
holder.creditsText.setFocusable(true);
holder.creditsText.setFocusableInTouchMode(true);
holder.creditsText.setInputType(InputType.TYPE_CLASS_NUMBER);
} // End if else
}
});
return holder;
} // End of onCreateViewHolder
@Override
public void onBindViewHolder(final CustomRowViewHolder customRowViewHolder, int position) {
ListItems listItem = itemsList.get(position);
customRowViewHolder.itemView.setSelected(focuseditem == position);
customRowViewHolder.getLayoutPosition();
customRowViewHolder.customTextListener.updatePosition(position, customRowViewHolder);
customRowViewHolder.courseText.setText(itemsList.get(position).getCourse());
} // End of onBindViewHolder
public void clearAdapter() {
itemsList.clear();
notifyDataSetChanged();
} // End of clearAdapter
public int getItemCount() {
return(null != itemsList ? itemsList.size() : 0);
} // End of getItemCount
public void onItemDismiss(int position) {
itemsList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, itemsList.size());
} // End of onItemDismiss
public void onItemMove(List<ListItems> itemsList, int fromPosition, int toPosition) {
Collections.swap(itemsList, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
} // End of onItemMove
} // End of MyRecyclerAdapter
/**
* Created by Thomas on 6/3/2016.
*/
class CustomTextListener implements TextWatcher {
private int position;
private CustomRowViewHolder customRowViewHolder;
public void updatePosition(int position, CustomRowViewHolder customRowViewHolder) {
this.position = position;
this.customRowViewHolder = customRowViewHolder;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
customRowViewHolder.courseText.setText(s.toString());
}
}
我的“CustomRowViewHolder.java”:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class CustomRowViewHolder extends RecyclerView.ViewHolder{
protected EditText courseText;
protected EditText creditsText;
protected CheckBox checkBox;
protected RelativeLayout relativeLayout;
protected CustomTextListener customTextListener;
public CustomRowViewHolder(View view, CustomTextListener customTextListener) {
super(view);
this.customTextListener = customTextListener;
this.courseText = (EditText) view.findViewById(R.id.course);
this.creditsText = (EditText) view.findViewById(R.id.credits);
this.checkBox = (CheckBox) view.findViewById(R.id.complete);
this.relativeLayout = (RelativeLayout) view.findViewById(R.id.relLayout);
view.setClickable(true);
}
}
最后,我的“ListItems.java”:
import java.util.ArrayList;
public class ListItems {
private String course;
private String credits;
private Boolean complete;
public ListItems(String mCourse, String mCredits, Boolean mComplete) {
course = mCourse;
credits = mCredits;
complete = mComplete;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getCredits() {
return credits;
}
public void setCredits(String credits) {
this.credits = credits;
}
public Boolean getIsComplete() {
return complete;
}
public void setIsComplete(Boolean complete) {
this.complete = complete;
}
public static ArrayList<ListItems> createListItem(ArrayList<ListItems> itemsList) {
itemsList.add(new ListItems(null, null, false));
return itemsList;
} // End of createListItem
public static ArrayList<ListItems> removeListItem(ArrayList<ListItems> itemsList) {
if (itemsList.size() == 0) {
return itemsList;
} else {
itemsList.remove(0);
} // End if
return itemsList;
} // End of removeListItem
} // End of ListItems
提前感谢您的任何帮助!非常感谢,因为我长期以来一直坚持这个问题!
答案 0 :(得分:0)
我遇到了类似的问题......您可以尝试使用ediText中的文本在ListItems中添加一个字段(如果需要,可以使用其他列表)。重要的是要引用先前插入的文本和listItem。让我们说,检查是否完成,获取文本并将其设置为ListItem中的该字段。
然后,在适配器中,使用该字段的值将setText设置为editText。
答案 1 :(得分:0)
向您的回收商视图添加方法以插入新项目。或删除它们。 像那样
public void addItem(ListItems object)
{
itemsList.add(object);
int pos = itemsList.indexOf(object);
notifyItemInserted(pos);
}
然后在添加课程按钮中单击事件调用添加项目方法:
// For the recycler view.
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rl);
List<ListItems> itemsList = new ArrayList<>();
adapter = new MyRecyclerAdapter(MainActivity.this, itemsList);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(linearLayoutManager);
// For the addCourse button.
final Button addCourse = (Button) findViewById(R.id.addCourse);
addCourse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecyclerView.getAdapter().addItem(new ListItems(null, null, false));
}
});
对于你的第二个问题,只需保存相关ListItem中的复选框状态,然后将其设置为onBindViewHolder方法中的复选框:像这样:
@Override
public void onBindViewHolder(final CustomRowViewHolder customRowViewHolder, int position) {
ListItems listItem = itemsList.get(position);
customRowViewHolder.itemView.setSelected(focuseditem == position);
customRowViewHolder.getLayoutPosition();
customRowViewHolder.customTextListener.updatePosition(position, customRowViewHolder);
customRowViewHolder.courseText.setText(itemsList.get(position).getCourse());
// just set listener to check box.
customRowViewHolder.checkBox.setChecked( itemsList.get(position).getIsComplete());
initEditText(customRowViewHolder,itemsList.get(position).getIsComplete());
customRowViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
itemsList.get(position).setIsComplete(b);
// for retrieve eidtTexts state
initEditText(customRowViewHolder,b);
});
//for retrieve text
customRowViewHolder.courseText.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) {
}
@Override
public void afterTextChanged(Editable editable) {
itemsList.get(position).setCourse(editable.toString());
}
});
customRowViewHolder.creditsText.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) {
}
@Override
public void afterTextChanged(Editable editable) {
itemsList.get(position).setCredits(editable.toString());
}
});
customRowViewHolder.courseText.setText( itemsList.get(position).getCourse());
customRowViewHolder.creditsText.setText( itemsList.get(position).getCredits());
} // End of onBindViewHolder
private void initEditText(CustomRowViewHolder customRowViewHolder,boolean b) {
if(b) {
customRowViewHolder.courseText.setEnabled(false);
customRowViewHolder.courseText.setFocusable(false);
customRowViewHolder.courseText.setInputType(InputType.TYPE_NULL);
customRowViewHolder.creditsText.setEnabled(false);
customRowViewHolder.creditsText.setFocusable(false);
customRowViewHolder.creditsText.setInputType(InputType.TYPE_NULL);
} else {
customRowViewHolder.courseText.setEnabled(true);
customRowViewHolder.courseText.setFocusable(true);
customRowViewHolder.courseText.setFocusableInTouchMode(true);
customRowViewHolder.courseText.setInputType(InputType.TYPE_CLASS_TEXT);
customRowViewHolder.creditsText.setEnabled(true);
customRowViewHolder.creditsText.setFocusable(true);
customRowViewHolder.creditsText.setFocusableInTouchMode(true);
customRowViewHolder.creditsText.setInputType(InputType.TYPE_CLASS_NUMBER);
} // End if else
}