我的列表视图存在问题我正在使用。列表视图中的每个项目都有两个按钮,用户可以使用这两个按钮来添加或取消每个参赛者的积分。
问题是当我向列表视图添加新项目或滚动时(在添加至少一个项目之后),代码随机地从一个或多个项目上的按钮中取出文本。
任何人都可以告诉我A)是什么原因导致的?和B)如何解决?
我已经包含了我认为相关的代码。如果您认为我需要在这里增加一些内容,请告诉我。
我的适配器类:
public class ContestantListAdapter extends BaseAdapter {
private ArrayList<ContestantItem> contestantList;
private LayoutInflater layoutInflater;
public ContestantListAdapter(Context context, ArrayList<ContestantItem> contestantList){
this.contestantList = contestantList;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount(){
return contestantList.size();
}
public View getView(int position, View convertView, ViewGroup parentView){
ViewHolder viewHolder;
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.contestant_row_layout,null);//null might need to be activity_main
viewHolder = new ViewHolder();
viewHolder.nameView = (TextView) convertView.findViewById(R.id.nameView);
viewHolder.pointsView = (TextView) convertView.findViewById(R.id.pointsView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.nameView.setText((contestantList.get(position).getName()));
viewHolder.pointsView.setText(Integer.toString(contestantList.get(position).getPoints()));
//get button position
Button button =(Button) convertView.findViewById(R.id.Minus);
button.setOnClickListener(minusListener);
Button buttonPlus = (Button) convertView.findViewById(R.id.Plus);
buttonPlus.setOnClickListener(addListener);
return convertView;
}
private View.OnClickListener minusListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
ContestantItem tmpContestant = (ContestantItem) contestantList.get(position);
int tmpPoints = tmpContestant.getPoints()-1;
if(tmpPoints<0){tmpPoints=0;}
tmpContestant.setPoints(tmpPoints);
notifyDataSetChanged();
}
};
private View.OnClickListener addListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
ContestantItem tmpContestant = (ContestantItem) contestantList.get(position);
int tmpPoints = tmpContestant.getPoints()+1;
tmpContestant.setPoints(tmpPoints);
notifyDataSetChanged();
}
};
static class ViewHolder {
TextView nameView;
TextView pointsView;
}
@Override
public Object getItem(int index){
return contestantList.get(index);
}
@Override
public long getItemId(int index){
return index;
}
//this method should add the contestent to the listView. must know what position to add it in.
public void addItem(String name, int index){
ContestantItem tempContestant = new ContestantItem();
tempContestant.setName(name);
tempContestant.setPoints(1);
contestantList.add(index,tempContestant);
notifyDataSetChanged();
}
. . .
ContestantRowLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.15"
android:id="@+id/Minus"
android:text="@string/minus"
/>
<TextView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.5"
android:id="@+id/nameView"
/>
<TextView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.2"
android:id="@+id/pointsView"
/>
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.15"
android:id="@+id/Plus"
android:text="@string/plus"
/>
</LinearLayout>
主要活动:
public class MainActivity extends AppCompatActivity implements ContestantsFragment.OnFragmentInteractionListener{
ListView listView1;
ContestantListAdapter contestantAdapter;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ContestantItem> rowDetails = getListData();
listView1 = (ListView) findViewById(R.id.contactsListView);
listView1.setAdapter(new ContestantListAdapter(this,rowDetails));
contestantAdapter = (ContestantListAdapter) listView1.getAdapter();
}
private ArrayList getListData(){
ArrayList<ContestantItem> contestantList = new ArrayList<ContestantItem>();
ContestantItem cItem;
for(int i=0;i<10;i++){
cItem = new ContestantItem();
cItem.setName("Danny Anderson");
cItem.setPoints(5);
contestantList.add(cItem);
}
return contestantList;
}
@Override
public void onFragmentInteraction(Uri uri) {
}
//if the name is found returns the index position in the list. If not found -99
public void findAlphabetical(View view){
//TODO implement Binary search algorithm.
hideKeyboard();
EditText tmpEditTxt = (EditText) findViewById(R.id.searchField);
String inputTxt = tmpEditTxt.getText().toString();
int findAtIndex =contestantAdapter.searchAlphabetical(inputTxt)-1;
if(findAtIndex <0){findAtIndex=0;}
listView1.smoothScrollToPosition(findAtIndex);
}
public void addInAlpabetOrder(View view){
//TODO add the name in alphabetical order
hideKeyboard();
//ListView tmpListView = (ListView) contestantsFragment.getView().findViewById(R.id.contactsListView);
EditText tmpEditTxt = (EditText) findViewById(R.id.searchField);
String inputTxt = tmpEditTxt.getText().toString();
int addAtIndex =contestantAdapter.searchAlphabetical(inputTxt);
contestantAdapter.addItem(inputTxt, addAtIndex);
listView1.smoothScrollToPosition(addAtIndex);
}
public void clearContestantList(View view){
//TODO clear out all contestants from the list
contestantAdapter.clearAllItems();
}
. . .
答案 0 :(得分:0)
我提到了@Suhas Bachewar所说的内容,并将一些内容与您拥有的代码放在一起,因为您应该知道您正在使用回收器来使您的视图填充列表中的不同对象,为什么不在内部使用单击事件 getView 就像你不必去寻找哪个特定按钮视图与一个必须增加其值的对象相关联,
public View getView(int position, View convertView, ViewGroup parentView){
ViewHolder viewHolder;
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.contestant_row_layout,null);//null might need to be activity_main
viewHolder = new ViewHolder();
viewHolder.nameView = (TextView) convertView.findViewById(R.id.nameView);
viewHolder.buttonMinus= (Button) convertView.findViewById(R.id.Minus);
viewHolder.buttonPlus= (Button) convertView.findViewById(R.id.Plus);
viewHolder.pointsView = (TextView) convertView.findViewById(R.id.pointsView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.nameView.setText((contestantList.get(position).getName()));
viewHolder.pointsView.setText(Integer.toString(contestantList.get(position).getPoints()));
//get button position
buttonMinus.setOnClickListener(minusListener);
buttonPlus.setOnClickListener(addListener);
return convertView;
}
private View.OnClickListener minusListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
ContestantItem tmpContestant = (ContestantItem) contestantList.get(position);
int tmpPoints = tmpContestant.getPoints()-1;
if(tmpPoints<0){tmpPoints=0;}
tmpContestant.setPoints(tmpPoints);
notifyDataSetChanged();
}
};
private View.OnClickListener addListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
ContestantItem tmpContestant = (ContestantItem) contestantList.get(position);
int tmpPoints = tmpContestant.getPoints()+1;
tmpContestant.setPoints(tmpPoints);
notifyDataSetChanged();
}
};
static class ViewHolder {
TextView nameView;
TextView pointsView;
Button buttonMinus;
Button buttonPlus;
}