0

我是 Android 编程新手,我想知道女巫是从数据库中填充 ListView 的最合适方法。这是我在数据库中用来获取我的项目的方法

// Getting All stats
public List<StatParcours> getAllSats() {
       List<StatParcours> statList = new ArrayList<StatParcours>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_STATS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                StatParcours stat = new StatParcours();
                stat.setID(Integer.parseInt(cursor.getString(0)));
                stat.setDate(cursor.getString(1));
                stat.setDuration(cursor.getString(2));
                stat.setDistance(Double.parseDouble(cursor.getString(3)));
                stat.setSpeed(Double.parseDouble(cursor.getString(4)));
                stat.setCondition(cursor.getString(5));

                // Adding contact to list
                statList.add(stat);
            } while (cursor.moveToNext());
        }

        // return contact list
        return statList;
}

在主要活动中,我正在使用它。我知道 populateMyStatsList 方法有问题,但我仍然不知道如何修复它。

public class History extends Activity {
public DatabaseHandler db;
private List<StatParcours> MyStats = new ArrayList<StatParcours>();     
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i("oncreate", "ok");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.history);
    populateMyStatsList ();
    populateListView();
    registerClickCallback();


}
    private void populateMyStatsList (){
        MyStats = db.getAllSats();

    }
    private void populateListView() {
        ArrayAdapter<StatParcours> adapter = new MyListAdapter();
        ListView list = (ListView) findViewById(R.id.HistListView);
        list.setAdapter(adapter);
        Log.i("Populate", "ok");
    }
    private void registerClickCallback() {
        ListView list = (ListView) findViewById(R.id.HistListView);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked,
                    int position, long id) {

                StatParcours clickedCar = MyStats.get(position);
                String message = "You clicked position " + position
                                + " Which is car make " + clickedCar.getDate();
                Toast.makeText(History.this, message, Toast.LENGTH_LONG).show();
            }
        });
    }
    private class MyListAdapter extends ArrayAdapter<StatParcours> {
        public MyListAdapter() {
            super(History.this, R.layout.item_view, MyStats);
            Log.i("MyListAdapter", "ok");
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View itemView = convertView;
            if (itemView == null) {
                Log.i("Make sure", "ok");
                itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
            }
            Log.i("getview", "ok");

            StatParcours currentStat = MyStats.get(position);           

            TextView makeText = (TextView) itemView.findViewById(R.id.item_txtMake);
            makeText.setText(currentStat.getDate());


            TextView yearText = (TextView) itemView.findViewById(R.id.item_txtYear);
            yearText.setText("" + currentStat.getDistance());


            TextView condionText = (TextView) itemView.findViewById(R.id.item_txtCondition);
            condionText.setText(currentStat.getCondition());

            return itemView;
        }   
    }


}
4

2 回答 2

0

您需要使用 SimpleCursor 适配器。我无法访问开发人员站点以获取文档,但这是您上面代码的示例。

编辑:这是 android 开发者网站的链接。

http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

第二次编辑:这将进入 populateListView()

// Select All Query
String selectQuery = "SELECT  * FROM " + TABLE_STATS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// Set your layout to int[]
int[] to = new int[]{R.id.item_txtMake,R.id.item_txtYear,R.id.item_txtCondition};

//set your columns to string[]; fill in your actual column names
string[] from = new string[]{"make","year","condition"};

//set up adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),R.layout.item_view, cursor, from,to,null);

//set adapter to listview
ListView list = (ListView) findViewById(R.id.HistListView);
    list.setAdapter(adapter);
于 2013-07-18T01:09:30.073 回答
0

由于您的 ListView 中只有 TextView,您可以简单地使用 SimpleCursorAdapter。

// Getting All stats
public Cursor getAllSats() {
   List<StatParcours> statList = new ArrayList<StatParcours>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_STATS;

    SQLiteDatabase db = this.getWritableDatabase();
    return db.rawQuery(selectQuery, null);
}

在你的历史课上

public class History extends Activity {
public DatabaseHandler db;
private List<StatParcours> MyStats = new ArrayList<StatParcours>();     
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("oncreate", "ok");
super.onCreate(savedInstanceState);
setContentView(R.layout.history);

Cursor c = getAllSats();
String[] fromColumns = {Your database column name for date, 
                            Your database column name for distance, 
                            Your database column name for condition};
    int[] toViews = {R.id.item_txtMake, 
                     R.id.item_txtYear, 
                     R.id.item_txtCondition};
    adapter = new SimpleCursorAdapter(this, R.layout.item_view, 
                                        c, fromColumns, toViews, 0);
    ListView list = (ListView) findViewById(R.id.HistListView);
    list.setAdapter(adapter);
registerClickCallback();


}
于 2013-07-18T01:16:07.107 回答