问题
GridView contains Categories, and when I do tap on any of the GridView item (i.e - any Category) i would like to show items those belongs to that particular Category only in ViewPager.
For example, I have two categories
first - Electronics and second - Appliances, still whenever i do tap on Appliances
category - it showing all
the items
(from Electronics 1 to Appliances 2
) whereas it has to show only items
those are under Appliances
category
ISSUE
Still showing all the items in a ViewPager, no matter which category has been tapped by user.... How may i show those Items that belongs to tapped Category only ?
public class MainActivity extends Activity {
ArrayList<Items> itemsArrayList;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemsArrayList = new ArrayList<Items>();
...
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
String categoryName = menusArrayList.get(position).getName().toString();
Log.d("categoryName:", categoryName);
menus = menusArrayList.get(position);
itemsArrayList = menus.getItemsArrayList();
adapter = new ViewPagerAdapter(MenusActivity.this, R.layout.adapter_viewpager, itemsArrayList);
viewPager.setAdapter(adapter);
}
});
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... urls) {
try {
.....
JSONArray jsonArray = jsonObject.getJSONArray("category");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jObject = jsonArray.getJSONObject(i);
menus = new Menus();
menus.setName(jObject.getString("name"));
menus.setImage(jObject.getString("image"));
JSONArray imagesArray = jObject.getJSONArray("items");
for(int j=0; j<imagesArray.length(); j++)
{
JSONObject imagesObject = imagesArray.getJSONObject(j);
items = new Items();
items.setTitle(imagesObject.getString("title"));
items.setImage(imagesObject.getString("image"));
itemsArrayList.add(items);
}
menus.setItemsArrayList(itemsArrayList);
menusArrayList.add(menus);
}
return true;
}
...
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
...
}
}
}
回答1:
Your itemArrayList is global and on parsing you're adding items to that list each time you read a category. For that reason your itemListArray include all items. Since it is reference to a list when you add an item to list, you're also adding item to previous categories since they reference to same list. Try changing your inner loop like this.
ArrayList<Items> itemsArrayList = new ArrayList<Items>();
for(int j=0; j<imagesArray.length(); j++)
{
JSONObject imagesObject = imagesArray.getJSONObject(j);
items = new Items();
items.setTitle(imagesObject.getString("title"));
items.setImage(imagesObject.getString("image"));
itemsArrayList.add(items);
}
来源:https://stackoverflow.com/questions/30047959/how-to-pass-json-array-using-json-object