我收到了responseJson
,而我正在尝试确定要在网格视图中加载的数据。我的屏幕上有2个textview,一旦按下1 textview,就会加载属于SubCategoryID =1
的项目。按下文本视图2时,应加载属于SubCategoryID = 2
的项目。
我已经给出了以下代码,
@Override
public void onTaskCompleted(JSONArray responseJson) {
try {
final List<String> descriptions = new ArrayList<String>();
final List<String> imageUrls = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
final JSONObject object = responseJson.getJSONObject(i);
mQuickReturnViewTop
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if ((object.getString("MainCategoryID"))
.equals("1")
&& (object
.getString("SubCategoryID"))
.equals("1")) {
Log.i("ImageURL ",
object.getString("ImageURL"));
imageUrls.add(object
.getString("ImageURL"));
Log.i("Description ",
object.getString("Description"));
descriptions.add(object
.getString("Description"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CustomGridPizza adapter = new CustomGridPizza(
getActivity(), descriptions, imageUrls);
grid.setAdapter(adapter);
}
});
mQuickReturnViewIta
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if ((object.getString("MainCategoryID"))
.equals("1")
&& (object
.getString("SubCategoryID"))
.equals("2")) {
Log.i("ImageURL ",
object.getString("ImageURL"));
imageUrls.add(object
.getString("ImageURL"));
Log.i("Description ",
object.getString("Description"));
descriptions.add(object
.getString("Description"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CustomGridPizza adapter = new CustomGridPizza(
getActivity(), descriptions, imageUrls);
grid.setAdapter(adapter);
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
使用此代码时,它只加载1个项目,当按住文本视图时,它会逐个添加相同的项目。
我需要帮助才能立即将项目加载到网格中。任何帮助将不胜感激。
答案 0 :(得分:0)
对于每个JSON对象,您正在创建一个新的OnClickListener并通过调用setOnClickListener覆盖最后一个,所以基本上您总是添加最后一个JSON对象。 您需要创建/设置for循环的OnClickListener和适配器外部。
try {
final List<String> descriptions1 = new ArrayList<String>();
final List<String> imageUrls1 = new ArrayList<String>();
final List<String> descriptions2 = new ArrayList<String>();
final List<String> imageUrls2 = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
final JSONObject object = responseJson.getJSONObject(i);
try {
if ((object.getString("MainCategoryID"))
.equals("1")
&& (object
.getString("SubCategoryID"))
.equals("1")) {
Log.i("ImageURL ",
object.getString("ImageURL"));
imageUrls1.add(object
.getString("ImageURL"));
Log.i("Description ",
object.getString("Description"));
descriptions1.add(object
.getString("Description"));
}else if ((object.getString("MainCategoryID"))
.equals("1")
&& (object
.getString("SubCategoryID"))
.equals("2")) {
Log.i("ImageURL ",
object.getString("ImageURL"));
imageUrls2.add(object
.getString("ImageURL"));
Log.i("Description ",
object.getString("Description"));
descriptions2.add(object
.getString("Description"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mQuickReturnViewTop
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomGridPizza adapter = new CustomGridPizza(
getActivity(), descriptions1, imageUrls1);
grid.setAdapter(adapter);
}
});
mQuickReturnViewIta
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomGridPizza adapter = new CustomGridPizza(
getActivity(), descriptions2, imageUrls2);
grid.setAdapter(adapter);
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}