【问题标题】:Multiple List Views?多个列表视图?
【发布时间】:2012-05-21 16:54:33
【问题描述】:

我正在尝试显示我正在创建的游戏的高分,有两列,一列是他们的名字,另一列是他们完成游戏所花费的移动量。

目前它全部存储在 SQLiteDatabase 中并显示在列表视图中,在该格式中显示为一列

名字,动作

但我想在屏幕的对面进行移动,这需要多个列表视图还是需要编辑一个列表视图或其适配器?

目前使用的代码是:

        datasource = new HighScoreDataSource(this);
    datasource.open(); //Open the connection

    List<HighScore> values = datasource.getAllHighScores(); //Retrieve all the data

    //Using a simple cursor adapted to show the elements
    ArrayAdapter<HighScore> adapter = new ArrayAdapter<HighScore>(this, android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);

【问题讨论】:

    标签: android sql sqlite android-listview


    【解决方案1】:

    使用您想要的方式放置两个TextViews 进行行布局,并实现一个简单的自定义ArrayAdapter

    public class CustomAdapter extends ArrayAdapter<HighScore> {
    
        private LayoutInflater inflater;
    
        public CustomAdapter(Context context, int textViewResourceId,
                List<HighScore> objects) {
            super(context, textViewResourceId, objects);
            inflater = LayoutInflater.from(context);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.new_row_layout, parent, false);
            }
            HighScore item = getItem(position); 
            TextView name = (TextView) findViewById(R.id.name_id_textview);
            name.setText(/*get the name from the item HighScore object*/);
            TextView moves = (TextView) findViewById(R.id.moves_id_textview);
            moves.setText(/*get the moves from the item HighScore object*/);
            return convertView;
        }       
    
    }
    

    另一种选择是将您的List&lt;HighScore&gt; values 拆分为HashMaps 列表(包含两个条目,一个用于名称,一个用于移动)并使用SimpleAdapter(使用上面的行布局)。

    【讨论】:

      猜你喜欢
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多