2013-07-18 145 views
0

我是Android编程的新手,我想知道女巫是从DataBase填充ListView最合适的方式。 这里我使用数据库的方法,让我的项目从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; 
     } 
    } 


} 
+0

应该使用SimpleCursorAdapter –

+0

你能告诉我请该怎么做? –

+0

可能的重复http://stackoverflow.com/questions/17674178/how-to-load-listview-contents-from-sqlitedatabase-in-android/17674448#17674448 – DynamicMind

回答

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); 
0

既然你只有在TextView的ListView的你,你可以只简单地使用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(); 


} 
相关问题