Listview一遍又一遍只显示最后一个项目

时间:2019-02-07 11:37:44

标签: java android arraylist android-listview custom-arrayadapter

我是android开发的新手。我注册了Udacity课程,然后我到达必须使用服装arrayAdapter来显示数据(文本)的listView的位置,但是我钉住了大部分数据,但是显示的数据存在问题,即:当我启动活动listView时显示最后一个项目在may arraylist上 我已经尝试了几乎所有的东西,但它们对我没有用

我的数据

/**
 * 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;
    }


}


我的ArrayList:

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;

    }
}

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您的tamazightTranslationenglishTranslation属性在Numbers类上是静态的,这意味着它们是类属性。从变量和getter中删除static,应该没问题。在https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

中查看类属性和实例属性之间的区别

答案 2 :(得分:0)

我更改了您的模型类和适配器类,请按照此步骤操作

private static void FillMePlease<T>(T genericThingy)
{
    foreach (PropertyInfo propertyInfo in genericThingy.GetType().GetProperties())
    {
        if (propertyInfo.PropertyType == typeof(string))
        {
            propertyInfo.SetValue(genericThingy, "Test", null);
        }
        else
        {
            //var list = Activator.CreateInstance(propertyInfo.PropertyType);
            //Now I'm stumped.
        }
    }
}


适配器类

 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;
    }
}

答案 3 :(得分:0)

如果它在列表视图中显示最后一项,则可能有多种原因。

  1. 您需要在ArrayAdapter中覆盖getCount()才能具有列表的大小。可能可以解决问题。

  2. 请确保未在XML布局的scrollview内声明列表视图。如果是这样,请使用nestedscrollview或其他方法正确处理它。

如果它在列表视图的所有行中显示相同的数据,则可能是

  1. 对于您而言,您需要删除属性前面的static关键字,static意味着属性或方法属于Number类,而不属于Number类对象的实例。基本上,这一定是原因。