mardi 6 janvier 2015

How to add items to the custom array list in android

Hi I have developed an android application where I'm retrieving the categories from mysql and setting them to the custom array list and displaying them as buttons.


Now I wanted to bring the same categories from local sqlite database and display them as buttons. But in First situation I have created a custom list array and I'm assigning the response and adding the items to that list and displaying as buttons.


But when I'm trying to retrieve them from local sqlite database I'm not getting how to add the values to the array list


This is how I'm retrieving from 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();

}
}


This is my Uiupdate() method



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);
}

}


This is my local sqlite method



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"));
categories.add(categoryName);
// Toast.makeText(getApplicationContext(), ""+categoryName, 5000).show();

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

}


}


Here I'm facing the problem with categories.add(categoryName) its showing error How to solve this..?


Aucun commentaire:

Enregistrer un commentaire