您好我开发了一个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);
}
在这里,我面临category.add(categoryName)显示错误的问题 怎么解决这个问题..?
答案 0 :(得分: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();
}
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);
}
答案 1 :(得分:0)
问题是您使用List
初始化了Category
,如此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
}