从另一个活动

时间:2015-04-27 10:12:24

标签: java android listview arraylist

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

enter image description here

代码: 具有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
包含list view

TopTen

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>

1 个答案:

答案 0 :(得分:0)

将列表数组作为可解析的包或简单地将对象列表从一个活动传递到第二个