2013-03-12 200 views
3

雖然這個問題在這裏已經問了很多次,但我找不到適合我的代碼的正確答案。我意識到這可能是小事,但我似乎無法找到問題,因爲我只是真的很陌生。將SQLite數據庫中的數據顯示在Android的ListView中

這裏是我的代碼getClientNamesDatabaseHelper類:

public Cursor getSitesByClientname(String id) { 
    String[] args={id}; 

Cursor myCursor = db.rawQuery("SELECT client_sitename FROM " + CLIENT_SITES_TABLE + " WHERE client_id=?", args); 
     String results = ""; 
     /*int count = myCursor.getCount(); 
     String[] results = new String[count + 1]; 
     int i = 0;*/ 

     if (myCursor != null) {   
       if(myCursor.getCount() > 0) 
       { 
        for (myCursor.moveToFirst(); !myCursor.isAfterLast(); myCursor.moveToNext()) 
        { 
         results = results + myCursor.getString(myCursor.getColumnIndex("client_sitename")); 
        } 
       } 
      } 
      return results; 
} 

一個問題是,我返回「字符串」到「光標」,但我不知道是什麼最好的解決方法,因爲我認爲我應該返回一個Cursor。

這裏的ClientSites類,我想顯示的數據:

public class ClientSites extends Activity { 

//public final static String ID_EXTRA="com.example.loginfromlocal._ID"; 

     private DBUserAdapter dbHelper = null; 
    private Cursor ourCursor = null; 
    private Adapter adapter=null; 


    @SuppressWarnings("deprecation") 
    @SuppressLint("NewApi") 
    public void onCreate(Bundle savedInstanceState) { 
     try 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.client_sites); 

      Intent i = getIntent(); 
      String uID = String.valueOf(i.getIntExtra("userID", 0)); 

      ListView myListView = (ListView)findViewById(R.id.myListView); 

      dbHelper = new DBUserAdapter(this); 

      //dbHelper.createDatabase(); 

      dbHelper.openDataBase(); 

      ourCursor = dbHelper.getSitesByClientname(uID); 
      Log.e("ALERT", uID.toString()); 

      startManagingCursor(ourCursor); 
      Log.e("ERROR", "After start manage cursor: "); 

      //@SuppressWarnings("deprecation") 
      //SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), R.id.myListView, null, null, null); 
      CursorAdapter adapter = new SimpleCursorAdapter(this, R.id.myListView, null, null, null, 0); 
      adapter = new Adapter(ourCursor); 

      //Toast.makeText(ClientSites.this, "Booo!!!", Toast.LENGTH_LONG).show(); 

      myListView.setAdapter(adapter); 

      myListView.setOnItemClickListener(onListClick); 


     } 
     catch (Exception e) 
     { 
      Log.e("ERROR", "XXERROR IN CODE: " + e.toString()); 
      e.printStackTrace(); 
     } 

    } 

    private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, 
           View view, int position, 
           long id) 
     { 
      Intent i=new Intent(ClientSites.this, InspectionPoints.class); 

      i.putExtra(ID_EXTRA, String.valueOf(id)); 
      startActivity(i); 

     } 
    }; 


    class Adapter extends CursorAdapter { 
     @SuppressWarnings("deprecation") 
     Adapter(Cursor c) { 
      super(ClientSites.this, c); 
     } 

     //@Override 
     public void bindView(View row, Context ctxt, 
          Cursor c) { 
      Holder holder=(Holder)row.getTag(); 
      holder.populateFrom(c, dbHelper); 
     } 
     @Override 
     public View newView(Context ctxt, Cursor c, 
          ViewGroup parent) { 
      LayoutInflater inflater=getLayoutInflater(); 
      View row = inflater.inflate(R.layout.row, parent, false); 
      Holder holder=new Holder(row); 
      row.setTag(holder); 
      return(row); 
     } 
     } 

    static class Holder { 
     private TextView name=null; 

     Holder(View row) { 
      name=(TextView)row.findViewById(R.id.ingredientText); 
     } 

     void populateFrom(Cursor c, DBUserAdapter r) { 
      name.setText(r.getName(c)); 
     } 
    } 
} 

這裏是我現在正在使用嘗試在ListView顯示數據的代碼。我從原來的嘗試中改變了一些,但仍然不確定我做錯了什麼。

public void onCreate(Bundle savedInstanceState) { 
     try 
     { 
      super.onCreate(savedInstanceState); 
      //setContentView(R.layout.client_sites); 

      Intent i = getIntent(); 
      String uID = String.valueOf(i.getIntExtra("userID", 0)); 
      //int uID = i.getIntExtra("userID", 0); 

      //ListView myListView = (ListView)findViewById(R.id.myListView); 

      dbHelper = new DBUserAdapter(this); 

      dbHelper.createDatabase(); 

      dbHelper.openDataBase(); 

      String[] results = dbHelper.getSitesByClientname(uID); 

      //setListAdapter(new ArrayAdapter<String>(ClientSites.this, R.id.myListView, results)); 
      //adapter = new ArrayAdapter<String>(ClientSites.this, R.id.myListView, results); 
      setListAdapter(new ArrayAdapter<String>(ClientSites.this, R.layout.client_sites, results)); 

      //ListView myListView = (ListView)findViewById(R.id.myListView); 
      ListView listView = getListView(); 
      listView.setTextFilterEnabled(true); 

      //ourCursor = dbHelper.getSitesByClientname(uID); 
      //Log.e("ALERT", uID.toString()); 

      //startManagingCursor(ourCursor); 
      //Log.e("ERROR", "After start manage cursor: "); 

      //@SuppressWarnings("deprecation") 
      //SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), R.id.myListView, null, null, null); // LOOK AT THIS IN THE MORNING!!!!!!!!!!! 
      //CursorAdapter adapter = new SimpleCursorAdapter(this, R.id.myListView, null, null, null, 0); 
      //adapter = new Adapter(ourCursor); 

      //Toast.makeText(ClientSites.this, "Booo!!!", Toast.LENGTH_LONG).show(); 

      //myListView.setAdapter(adapter); 

      //myListView.setOnItemClickListener(onListClick); 
+0

你爲什麼不只是光標返回? – njzk2 2013-03-12 14:17:17

+0

是的,我會給出一個去,只是相當新的,所以不太確定正確的語法。謝謝你的幫助。 – Confucius 2013-03-12 21:04:56

回答

0

如果你想返回從dbHelper光標,你可以做這樣的事情......

public Cursor getSitesByClientname(String id) { 
    String[] args={id}; 

return db.rawQuery("SELECT client_sitename FROM " + CLIENT_SITES_TABLE + " WHERE client_id=?", args); 
} 

我還需要一些時間來閱讀listview tutorial

+0

感謝您的幫助,我看了一遍教程,真的很有幫助,歡呼! – Confucius 2013-03-12 21:15:35

+0

沒問題。如果你感到高興,隨時將其標記爲答案。 – MrChaz 2013-03-13 09:20:24

2

我創建了一個來自數據庫的Listview。這裏是我用於我的應用程序的代碼

這是數據庫處理程序的一部分。它會爲我的列表視圖返回一個類別列表。如果這是你需要的,它可以返回一個字符串列表。

public List<Category> getAllCategorys() { 
     ArrayList<Category> categoryList = new ArrayList<Category>(); 
     // Select All Query 
     String selectQuery = "SELECT * FROM " + TABLE_CATEGORY; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     // looping through all rows and adding to list 
     try{ 
      if (cursor.moveToFirst()) { 
       do { 
        Category category = new Category(); 
        category.setID(Integer.parseInt(cursor.getString(0))); 
        category.setCategory(cursor.getString(1)); 
        // Adding category to list 
        categoryList.add(category); 
       } while (cursor.moveToNext()); 
      } 
     }finally{ 
      cursor.close(); 
     } 
     db.close(); 
     // return category list 
     return categoryList; 

以下是我如何通過調用數據庫來填充ListView。

int size = db.getCategoryCount(); 
List<Category> categoryList = db.getAllCategorys(); 

category_data = new String[size-1]; 
int i=0; 
for(Category cn : categoryList) 
{ 
    category_data[i] = cn.getCategory(); // get the name of the category and add it to array 
    i++; 
} 

listAdapter = new ArrayAdapter<String>(this, R.layout.categoryrow, category_data); 
listViw.setAdapter(listAdapter); 

編輯:這裏是應該爲你工作

public List<String> getSitesByClientname(String id) { 
    String[] args={id}; 
    ArrayList<String> result = new ArrayList<String>(); 
    SQLiteDatabase db = this.getWritableDatabase(); 

    Cursor myCursor = db.rawQuery("SELECT client_sitename FROM " + CLIENT_SITES_TABLE +  " WHERE client_id=?", args); 

    try{ 
     if (myCursor.moveToFirst()){ 
      do{ 
       result.add(myCursor.getString(myCusor.getString(myCursor.getColumnIndex("client_sitename")); 
      }while(myCursor.moveToNext()); 
     } 
    }finally{ 
     myCursor.close(); 
    } 
    db.close(); 
    return result; 
} 

使用方法如下

List<String> sites_data = dbHelper.getSitesByClientname(uID); 

result_data = new String[sites_data.size()]; 
int i=0; 
for(String s : sites_data) 
{ 
    result_data[i] = s; // get the name of the category and add it to array 
    i++; 
} 

listAdapter = new ArrayAdapter<String>(this, R.layout.client_sites, result_data); 
listViw.setAdapter(listAdapter); 
+0

謝謝Zabador,感謝您的幫助。我現在用我的值返回'string'數組,但不幸的是我仍然無法在我的ListView中顯示任何東西。這裏是我正在使用的代碼來嘗試這樣做。 – Confucius 2013-03-13 21:45:12

+0

我已經改變了我的代碼來顯示數據,這與你在這裏的內容一致。我想在這裏展示它,但似乎沒有能力,所以編輯了我的原始問題並將其添加到了底部。如果任何人可以看看,並提供任何意見,我在做什麼錯我會很感激。 – Confucius 2013-03-13 23:04:01

+0

您可以顯示您爲您製作的新代碼DBUserAdapter類嗎? – Zabador 2013-03-14 15:55:30

相關問題