Listview一遍又一遍地只显示最后一项
Posted
技术标签:
【中文标题】Listview一遍又一遍地只显示最后一项【英文标题】:Listview show only last item over and over 【发布时间】:2019-07-01 12:29:23 【问题描述】:我是安卓开发新手。我注册了 Udacity 课程,我到了必须使用服装 arrayAdapter 来显示数据(文本)的列表视图的地方,我掌握了大部分内容,但显示的数据有问题:当我启动我的活动时,listView 显示最后一项五月数组列表 我几乎尝试了所有方法,但它们对我没有任何作用
我的数据
/**
* Displays text to the user.
*/
public class Numbers
//First we Set the Stat of our Class :
// English Translation
public static String englishTranslation;
//Tamazight Translation
public static String tamazightTranslation;
/**
* Create a new Numbers Object :
*
* @param englishTranslation is for ENGLISH NUMBERS
* @param tamazightTranslation is for TAMAZIGHT NUMBERS
*/
//Constructor
public Numbers(String englishTranslation, String tamazightTranslation)
this.englishTranslation = englishTranslation;
this.tamazightTranslation = tamazightTranslation;
//Getter
public static String getEnglishTranslation()
return englishTranslation;
public static String getTamazightTranslation()
return tamazightTranslation;
我的数组列表:
public class ActivityNumbers extends AppCompatActivity
//Add English numbers to The ARRAYList
ArrayList<Numbers> englishNumbers = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
englishNumbers.add(new Numbers("One", "Yan"));
englishNumbers.add(new Numbers("Two", "Sin"));
englishNumbers.add(new Numbers("Three", "Krad"));
englishNumbers.add(new Numbers("Four", "Koz"));
englishNumbers.add(new Numbers("Five", "Smos"));
englishNumbers.add(new Numbers("Six", "Sdis"));
englishNumbers.add(new Numbers("Seven", "Sa"));
englishNumbers.add(new Numbers("Eight", "Tam"));
englishNumbers.add(new Numbers("Nine", "Tza"));
englishNumbers.add(new Numbers("Ten", "Mraw"));
//EnglishNumbers.remove(0);
//EnglishNumbers.size();
// EnglishNumbers.get(1);
//Create a NEW TV object
/**Creating an ArrayAdapter (DATA HOLDER)
@param Context / the ACTIVITY concerned
@param android.R.layout : an XML layout file contains TextView predefined by Android
@param Items to display */
NumbersAdapter itemsAdapter = new NumbersAdapter (this, englishNumbers);
// Linking the ListView object to a Variable
ListView numbersListView = (ListView) findViewById(R.id.list);
//Calling setAdapter Method on numbersListView with "itemsAdapter
numbersListView.setAdapter(itemsAdapter);
我的适配器:
public class NumbersAdapter extends ArrayAdapter<Numbers>
public NumbersAdapter(Context context, ArrayList<Numbers> englishNumbers)
super(context, 0, englishNumbers);
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
View listItemView = convertView;
if (listItemView == null)
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.listitem, parent, false);
Numbers numbers = getItem(position);
System.out.println(position);
TextView tamazight_item = (TextView) listItemView.findViewById(R.id.tamazight_item);
TextView english_item = (TextView) listItemView.findViewById(R.id.english_item);
tamazight_item.setText(numbers.getEnglishTranslation());
System.out.println(numbers.getEnglishTranslation());
english_item.setText(numbers.getTamazightTranslation());
System.out.println(numbers.getTamazightTranslation());
return listItemView;
【问题讨论】:
这是您的数组适配器类的完整代码吗? 请正确格式化您的代码。 【参考方案1】:试试这个方法
Context context;
ArrayList<Numbers> englishNumbers;
public NumbersAdapter(Context context, ArrayList<Numbers> englishNumbers)
this.context=context;
this.englishNumbers=englishNumbers;
@Override
public View getView(int position, View convertView, ViewGroup parent)
convertView= LayoutInflater.from(context).inflate(R.layout.listitem,parent,false);
TextView tamazight_item = (TextView) listItemView.findViewById(R.id.tamazight_item);
TextView english_item = (TextView) listItemView.findViewById(R.id.english_item);
tamazight_item.setText(englishNumbers.get(position).getEnglishTranslation());
english_item.setText(englishNumbers.get(position).getTamazightTranslation());
return convertView;
别忘了让 NumbersAdapter 扩展 BaseAdapter
【讨论】:
感谢您的帮助 :)【参考方案2】:您的 tamazightTranslation
和 englishTranslation
属性在 Numbers
类上是静态的,这意味着它们是类属性。从变量和吸气剂中删除static
,你应该没问题。在这里查看类属性和实例属性的区别https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
【讨论】:
谢谢蒂亚戈,我已经为此苦苦挣扎了很长时间..我已经尝试过没有静态但没有工作..但现在它像魔术一样工作:)非常感谢 【参考方案3】:我已经改变了你的模型类和适配器类,请遵循这个它会工作
public class Numbers
//First we Set the Stat of our Class :
// English Translation
private String englishTranslation;
//Tamazight Translation
private String tamazightTranslation;
public String getEnglishTranslation()
return englishTranslation;
public void setEnglishTranslation(String englishTranslation)
this.englishTranslation = englishTranslation;
public String getTamazightTranslation()
return tamazightTranslation;
public void setTamazightTranslation(String tamazightTranslation)
this.tamazightTranslation = tamazightTranslation;
适配器类
public class NumbersAdapter extends ArrayAdapter<Numbers>
private Context mContext;
private ArrayList<Numbers>list;
public NumbersAdapter(Context context, ArrayList<Numbers> englishNumbers)
super(context, 0, englishNumbers);
this.mContext=context;
this.list = englishNumbers;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
View listItemView = convertView;
if (listItemView == null)
listItemView = LayoutInflater.from(mContext).inflate(R.layout.listitem, parent, false);
Numbers numbers = list.get(position);
System.out.println(position);
TextView tamazight_item = (TextView) listItemView.findViewById(R.id.tamazight_item);
TextView english_item = (TextView) listItemView.findViewById(R.id.english_item);
tamazight_item.setText(numbers.getEnglishTranslation());
System.out.println(numbers.getEnglishTranslation());
english_item.setText(numbers.getTamazightTranslation());
System.out.println(numbers.getTamazightTranslation());
return listItemView;
【讨论】:
谢谢你,我会试试这个代码:) 好的,如果对你有帮助,别忘了接受这个答案【参考方案4】:如果它在您的列表视图中显示最后一项,可能有几个原因。
您需要在 ArrayAdapter 中覆盖 getCount()
以获得列表的大小。它可能会解决问题。
确保列表视图未在您的 xml 布局中的滚动视图内声明。如果是,请使用nestedscrollview 或其他方式正确处理。
如果它在列表视图的所有行中显示相同的数据,那么它可能是
-
在你的情况下,你需要删除属性前面的
static
关键字,static
表示属性或方法属于Number类,而不是你的Number类对象的实例。基本上,一定是这个原因。
【讨论】:
以上是关于Listview一遍又一遍地只显示最后一项的主要内容,如果未能解决你的问题,请参考以下文章