滚动或添加列表视图中的项目时按钮文本消失,Android
button text disappears when scrolling or adding and item in listview, Android
我在使用列表视图时遇到问题。列表视图中的每个项目都有两个按钮,用户可以使用这些按钮为每位参赛者增加或减少分数。
问题是当我向列表视图添加新项目或滚动时(至少添加了一个项目后)代码随机从一个或多个项目的按钮中取出文本。
谁能告诉我 A) 是什么原因造成的? B) 如何解决?
我已经包含了我认为是相关的代码。如果您认为我需要在此处添加更多内容,请告诉我。
我的适配器class:
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();
}
. . .
参赛者行布局:
<?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>
主要Activity:
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();
}
. . .
我提到了@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;
}
我在使用列表视图时遇到问题。列表视图中的每个项目都有两个按钮,用户可以使用这些按钮为每位参赛者增加或减少分数。
问题是当我向列表视图添加新项目或滚动时(至少添加了一个项目后)代码随机从一个或多个项目的按钮中取出文本。
谁能告诉我 A) 是什么原因造成的? B) 如何解决?
我已经包含了我认为是相关的代码。如果您认为我需要在此处添加更多内容,请告诉我。
我的适配器class:
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();
}
. . .
参赛者行布局:
<?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>
主要Activity:
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();
}
. . .
我提到了@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;
}