Java 显示来自其他活动的Arraylist的listview
我想制作本地高分表,并在主要活动中展示。 我构建了一个Java 显示来自其他活动的Arraylist的listview,java,android,listview,arraylist,Java,Android,Listview,Arraylist,我想制作本地高分表,并在主要活动中展示。 我构建了一个ListView和ArrayAdapter等等。 当我打开包含列表视图的意图时,我看不到列表视图,我在应用程序代码中得到一些位置作为文本视图。(见图) 代码: 具有Arraylist的Person类 public class Person extends Activity { EditText name; Button ok; TextView enter; Integer Score; String
ListView
和ArrayAdapter
等等。
当我打开包含列表视图的意图时,我看不到列表视图,我在应用程序代码中得到一些位置作为文本视图。(见图)
代码:
具有Arraylist的Person
类
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
热门活动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>
将列表数组作为可解析包传递,或者只将对象列表从一个活动传递到第二个活动切勿在适配器中引用活动。你必须以另一种方式传递你的前十名ArrayList。显然,你应该做的是做一个Android教程,向你解释Android的一般工作原理,比如这家伙的视频:一旦你知道如何不手动实例化活动,并且有专门的类来存储你显示的数据,而不是合并你的数据“个人”(数据)和活动(数据显示),最重要的是,当你知道你在做什么时,你应该清理你写的东西并重新做。我会尽快重新做。这是真的,这里真正的问题是他的“列表数组”由活动组成。
<?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>