【问题标题】:show listview for Arraylist from another Activity显示来自另一个活动的 Arraylist 的列表视图
【发布时间】:2015-07-05 17:41:56
【问题描述】:

我想制作本地 HighScore 表并在主 Activity 中显示它可以说。 我建立了一个ListViewArrayAdapter 等等。 当我打开包含列表视图的意图时,我看不到列表视图,我在应用程序代码中以文本视图的形式获得了一些位置。(参见图片)

代码: Person 类具有Arraylist

public class Person extends Activity {
    EditText name;
    Button ok;
    TextView enter;
    Integer Score;
    String Name;
    Person person;
    ArrayList<Person> TopTen = new ArrayList<Person>(10);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);

        Intent intent = getIntent();
        Score = intent.getIntExtra("Score", 0);//defualt value if thers no score
        name = (EditText) findViewById(R.id.namep);
        ok = (Button) findViewById(R.id.send);
        enter = (TextView) findViewById(R.id.entername);
        Toast toast = Toast.makeText(getApplicationContext(), "Lets see if you are can be on TopTen", Toast.LENGTH_SHORT);
        person = new Person();
        person.Name = name.getText().toString();
        person.Score = Score;
        ok.setOnClickListener(SendActionListener);
    }

    private OnClickListener SendActionListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // error is here
            /*Person lastEntry = TopTen.get(TopTen.size()-1);//Last element of the arraylist
            Log.d("TopTen", lastEntry.Score.toString());
            if(lastEntry.Score < Score){//checking score
                lastEntry.Score = Score;//put score aka Value
                lastEntry.Name=name.getText().toString();//put name aka Key
                Toast toast = Toast.makeText(getApplicationContext(), "You are now on the TopTen List", Toast.LENGTH_SHORT);
            }*/
            /*Iterator<Person> iterator = TopTen.iterator();
            while(iterator.hasNext()){
                if(!iterator.hasNext()){//last value of the list

                }

            }*/
            int size = TopTen.size() - 1;
            Log.d("size", String.valueOf(size));
            if (size == -1) {//No Values in the array
                TopTen.add(person);
            } else {
                size = TopTen.size() - 1;
                if (person.Score > 0) {
                    if (TopTen.get(size).Score < person.Score) {
                        TopTen.remove(size);
                        TopTen.add(person);
                        Toast toast = Toast.makeText(getApplicationContext(), "You are in topten", Toast.LENGTH_SHORT);
                        toast.show();
                        Intent intent = new Intent(Person.this, MainActivity.class);
                        //intent.putExtra("ArrayList", TopTen);
                        startActivity(intent);
                    } else {
                        Toast toast = Toast.makeText(getApplicationContext(), "You are not in topten", Toast.LENGTH_SHORT);
                        toast.show();
                        Intent intent = new Intent(Person.this, MainActivity.class);
                        //intent.putExtra("ArrayList", TopTen);
                        startActivity(intent);
                    }
                }
            }
            //Collections.sort(TopTen, new ScoreComparator());
        }
    };

    public Integer getScore() {
        return Score;
    }

    class ScoreComparator implements Comparator<Person> {
        @Override
        public int compare(Person person1, Person person2) {
            // TODO Auto-generated method stub
            return person1.getScore().compareTo(person2.getScore());
        }

    }

}//end of Person

TopTen 类,包含 list view

public class TopTen extends ListActivity {
    Person p;
    Person a;
    Person b;
    Person c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topten);
        p = new Person();
        /*a = new Person();
        b = new Person();
        c = new Person();

        a.Name="Daniel";
        a.Score=20;
        b.Name="Zohar";
        b.Score=15;
        c.Name="Yakir";
        c.Score=10;

        p.TopTen.add(a);
        p. TopTen.add(b);
        p. TopTen.add(c);*/
        Log.d("inside", "TopTen OnCreat");
        populateListView();
    }

    private void populateListView() {
        Log.d("inside", "populateListView");
        ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this, R.layout.activity_topten, R.id.text1, p.TopTen);
        ListView list = (ListView) findViewById(android.R.id.list);
        list.setAdapter(adapter);
    }
}

Topten_activity xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_1"
android:orientation="vertical" >

<TextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_button_orange"
    android:gravity="center"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="6dip"
    android:text="@string/topten"
    android:textColor="@android:color/background_light"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

【问题讨论】:

  • 从不引用适配器中的活动。您必须以另一种方式传递您的 TopTen ArrayList。
  • 很明显,您应该做一个 Android 教程来修复此错误,该教程向您解释 Android 的一般工作原理,例如这个人的视频:youtube.com/watch?v=Z149x12sXsw,一旦您知道如何不手动实例化活动,并且有专门的类来存储您显示的数据,而不是将您的“人”(数据)与活动(数据的显示)合并,最重要的是当你知道自己在做什么时,你应该清理你写的东西并重新做。
  • 会尽快重新做

标签: java android listview arraylist


【解决方案1】:

将您的列表数组作为可解析的包传递,或者只是将对象列表从一个活动传递到第二个活动

【讨论】:

  • 虽然这是真的,但这里真正的问题是他的“列表数组”由活动组成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 2014-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多