2015-01-06 117 views
0

中的自定義數組列表嗨我開發了一個android應用程序,我從mysql中檢索類別並將它們設置爲自定義數組列表並將它們顯示爲按鈕。如何將項目添加到android

現在我想從本地sqlite數據庫中帶來相同的類別,並將它們顯示爲按鈕。 但在第一種情況下,我已經創建了一個自定義列表數組,並且正在分配響應並將這些項目添加到該列表並顯示爲按鈕。

但是,當我試圖從本地SQLite數據庫中獲取它們,我沒有得到如何將值添加到數組列表

這是怎麼了,我從MySQL檢索

@Override 
    protected void onPostExecute(String resp) { 
     progressDialog.dismiss(); 
     if(resp ==null) { 
      if (categoriesResp != null) { 
       //Toast.makeText(getApplicationContext(), ""+categoriesResp, 5000).show(); 
       updateUi(categoriesResp.response); 
      } else 
       Toast.makeText(Categories.this, "Something went wrong", Toast.LENGTH_LONG).show(); 
     }else 

      if(!Utils.isNetConnected(Categories.this)) 
       Utils.showDialog(Categories.this); 
      else 
      Toast.makeText(Categories.this, resp, Toast.LENGTH_LONG).show(); 

    } 
} 

這是我Uiupdate()方法

private void Uiupdate(List<Category> response) { 
    int i = 0; 
    for(Category category:response){ 
     LayoutInflater inflater = getLayoutInflater(); 
     View row = inflater.inflate(R.layout.my_category, null, false); 
     Button button = (Button) row.findViewById(R.id.buttonCategory); 
     button.setText(category.CategoryName); 
     button.setTag(category.CategoryID); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(Utils.isNetConnected(Categories.this)) { 
        Intent intent = new Intent(Categories.this, MainActivity.class); 
        intent.putExtra("categoryId", (String) v.getTag()); 
        intent.putExtra("categoryName", ((Button) v).getText()); 

        startActivity(intent); 
       }else 
        Utils.showDialog(Categories.this); 
      } 
     }); 
     if(i%2 == 0) 
      button.setBackgroundResource(R.drawable.label_with_arrow); 
     else 
      button.setBackgroundResource(R.drawable.field_with_arrow); 
     i++; 
     categoriesContainer.addView(row); 
    } 

} 

這是我的本地SQLite方法

public void loadCategories() 
{ 
    Toast.makeText(getApplicationContext(), "Loaded", 5000).show(); 
    db = this.openOrCreateDatabase("tafrider", MODE_PRIVATE, null); 
    c = db.rawQuery("SELECT CategoryID,CategoryName FROM categories WHERE EventID= 0 and status= 1 ",null); 
    List<Category> categories = new ArrayList<Category>(); 
    while(c.moveToNext()){ 
     categoryName = c.getString(c.getColumnIndex("CategoryName")); 

     Category c1 = new Category(); 
     c1.CategoryName= categoryName; 
     Category.add(c1); 
     // Toast.makeText(getApplicationContext(), ""+categoryName, 5000).show(); 

    } 
    Toast.makeText(getApplicationContext(), ""+categories.size(), 5000).show(); 
    updateUi(categories); 

} 

在這裏,我面臨與categories.add(categoryName)它顯示錯誤 如何解決這個問題..?

+0

「它顯示錯誤」是什麼錯誤? – codeMagic

+0

@codeMagic類型列表中的方法add(類別)不適用於參數(String)這是錯誤 –

回答

0

做到像

List<Categories> categories = new ArrayList<Categories>(); 
while(c.moveToNext()){ 
    categoryName = c.getString(c.getColumnIndex("CategoryName")); 
Categories categoryObject=new Categories(); 
categoryObject.CategoryName=categoryName; 

    categories.add(categoryObject); 
    // Toast.makeText(getApplicationContext(), ""+categoryName, 5000).show(); 

} 

類別名稱是字符串類型,你將它添加到列表中的類

for(Category category:response){ 
    LayoutInflater inflater = getLayoutInflater(); 
    View row = inflater.inflate(R.layout.my_category, null, false); 
    Button button = (Button) row.findViewById(R.id.buttonCategory); 
    button.setText(category.CategoryName); 
    button.setTag(category.CategoryID); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(Utils.isNetConnected(Categories.this)) { 
       Intent intent = new Intent(Categories.this, MainActivity.class); 
       intent.putExtra("categoryId", (String) v.getTag()); 
       intent.putExtra("categoryName", ((Button) v).getText()); 

       startActivity(intent); 
      }else 
       Utils.showDialog(Categories.this); 
     } 
    }); 
    if(i%2 == 0) 
     button.setBackgroundResource(R.drawable.label_with_arrow); 
    else 
     button.setBackgroundResource(R.drawable.field_with_arrow); 
    i++; 
    categoriesContainer.addView(row); 
} 
+0

我試過這個,它顯示錯誤 –

+1

它顯示的錯誤是什麼? –

+0

在類型分類的方法Uiupdate(名單)不適用的參數(列表) –

0

問題是你有Category初始化的List這樣List<Category> list = new ArrayList<Category>();。但是您嘗試添加String而不是Category。您需要做的是將初始化更改爲List<String> list = new ArrayList<String>();或添加Category而不是String

編輯 迴應你的評論。你沒有添加分類。在您的loadCategories()方法我看到這個代碼

while(c.moveToNext()){ 
    categoryName = c.getString(c.getColumnIndex("CategoryName"));// This is String not a category. You need to change here 
    categories.add(categoryName); // Adding as String 
} 

更改爲

while(c.moveToNext()){ 
    categoryName = c.getString(c.getColumnIndex("CategoryName")) 
    // Init Category 
    Categories caObj=new Categories(); 
    caObj.yourCategoryName=categoryName; 
    categories.add(caObj); // Now you adding as Cateogry 
} 
+0

我已添加類別,但它顯示錯誤 –

+0

檢查我更新的答案。希望它會很明確 – GoCrazy

+0

其顯示這樣的錯誤方法添加(類別)是未定義的類型類別 –