尝试实现高分ListView。我想在我的ListView中添加一个适配器。我用3个TextView元素构建了自己的Adapter。我的ListView甚至没有显示在我的活动中。
private class HighscoreAdapter extends ArrayAdapter<String> {
private class ViewHolder {
private TextView name;
private TextView highscore;
private TextView date;
}
private final Context context;
private final String[] values;
private final int textViewResourceId;
public HighscoreAdapter(Context context, int textViewResourceId, String[] values) {
super(context, textViewResourceId);
this.textViewResourceId = textViewResourceId;
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = getLayoutInflater().inflate(textViewResourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.highscore_name);
viewHolder.highscore = (TextView) convertView.findViewById(R.id.highscore_points);
viewHolder.date = (TextView) convertView.findViewById(R.id.highscore_date);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText("xman");
viewHolder.highscore.setText("2000");
viewHolder.date.setText("today");
return convertView;
}
}
这是我在与HighscoreAdapter相同的类中的onCreate方法。 R.layout.list_line包含所有三个TextView元素:
public class HighScore extends Activity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore_activity);
final ListView listview = (ListView) findViewById(android.R.id.list);
String[] values = new String[] { "one", "two", "three"};
final HighscoreAdapter highscoreAdapter = new HighscoreAdapter(this, R.layout.list_line, values);
listview.setAdapter(highscoreAdapter);
}
...
private class HighscoreAdapter extends ...
}
这是我的活动布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
....
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginBottom="70dp"
android:id="@+id/highscore_listview"
android:layout_gravity="center_horizontal" />
</RelativeLayout>
这是我的list_line.xml布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<TextView
android:id="@+id/icon"
android:layout_width="30dip"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:textSize="40sp"
android:text="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_toRightOf="@+id/icon"
android:text="Large Text"
android:id="@+id/highscore_name"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20.000"
android:id="@+id/highscore_points"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:textSize="23dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30 Dec 2015"
android:id="@+id/highscore_date"
android:layout_below="@id/highscore_points"
android:layout_alignParentEnd="true"
android:textSize="15dp" />
</RelativeLayout>
答案 0 :(得分:2)
更改您的适配器,如下所示:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class HighScoreAdapter extends BaseAdapter {
private ArrayList<String> items;
private Context context;
public HighScoreAdapter(Context context, ArrayList<String> items){
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_line, null);
}
TextView name, highscore, date;
name = (TextView) convertView.findViewById(R.id.highscore_name);
highscore = (TextView) convertView.findViewById(R.id.highscore_points);
date = (TextView) convertView.findViewById(R.id.highscore_date);
name.setText(items.get(position));
highscore.setText(items.get(position));
date.setText(items.get(position));
return convertView;
}
}
而不是像下面那样使用它:
String[] values = new String[] { "one", "two", "three"};
ArrayList<String> items = new ArrayList<>();
for (String item : values){
items.add(item);
}
final HighScoreAdapter highscoreAdapter = new HighScoreAdapter(this, items);
listview.setAdapter(highscoreAdapter);
答案 1 :(得分:1)
首先改变这一行:
ListView listview = (ListView) findViewById(android.R.id.list);
为:
ListView listview = (ListView) findViewById(R.id.highscore_listview);
**你应该使用你的包R类来查找id,你应该设置正确的id:D
一些提示:
为什么你为两个TextView使用相同的id?为每个视图使用uniqe id! 我觉得final关键字在这里没用!为什么你使用final作为listview?
答案 2 :(得分:0)
这是问题
final ListView listview = (ListView) findViewById(android.R.id.list);
您在此处使用了错误的ListView
ID(android.R.id.list
)。
将其更改为R.id.my_list
,格式为R.layout.highscore_activity
<强>更新强>
更改
final ListView listview = (ListView) findViewById(android.R.id.list);
到
final ListView listview = (ListView) findViewById(R.id.highscore_listview);
答案 3 :(得分:0)
此
final ListView listview = (ListView) findViewById(android.R.id.list);
应该是
final ListView listview = (ListView) findViewById(android.R.id.highscore_listview);
答案 4 :(得分:0)
您的ListView应如下所示:
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginBottom="70dp"
android:id="@android:id/list"
android:layout_gravity="center_horizontal" />