如何将项目添加到 android 中的自定义数组列表

How to add items to the custom array list in android

您好,我开发了一个 android 应用程序,我从 mysql 中检索类别并将它们设置为自定义数组列表并将它们显示为按钮。

现在我想从本地 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) 显示错误的问题 如何解决这个..?

那样做
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();

}

categoryName 是字符串类型,您要将其添加到属于

类别的列表中
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);
}

问题是你用 Category 初始化了 List,就像这样 List<Category> list = new ArrayList<Category>();。但是您尝试添加 String 而不是 Category。您需要做的是将初始化更改为 List<String> list = new ArrayList<String>(); 或添加 Category 而不是 String.

编辑 作为对您 comment.No 的回应,您没有添加类别。在您的 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
}