2015-01-06 118 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

其显示这样的错误方法添加(类别)是未定义的类型类别 –