AsynTask를 사용하여 json의 listview에 데이터를 표시하고 있습니다.목록보기의 "구분 기호"
코드는 여기에 있습니다.
public class MenuTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
List<NameValuePair> params = new ArrayList<NameValuePair>();
// Getting JSON String from URL..............
JSONObject jsonObject = jParser.makeHttpRequest(
"http://smartaway.dk/json/submenu.php?resid=" + res_id,
"POST", params);
try {
bestdeal = jsonObject.getJSONArray(TAG_MENU);
///LOOping through AllEvents........
for (int i = 0; i < bestdeal.length(); i++) {
JSONObject e = bestdeal.getJSONObject(i);
String resname = e.getString(TAG_MENUNAME);
String city_state = e.getString(TAG_PRICE);
// Creating New HAsh Map.........
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
// map.put(TAG_ID, id);
map.put(TAG_MENUNAME, resname);
map.put(TAG_PRICE, city_state);
/*
* map.put(TAG_STREET, street); map.put(TAG_COUSINE,
* cousine); map.put(TAG_RES_LOGO, reslogo);
*/
// adding HashList to ArrayList
bestdeal_list.add(map);
}
// }
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
/*
* if(bestdeal_list.isEmpty()){ AlertDialog alertDialog=new
* AlertDialog.Builder(getParent()).create();
* alertDialog.setTitle("No Best Deal Found");
* alertDialog.setButton("Ok", new DialogInterface.OnClickListener()
* {
*
* @Override public void onClick(DialogInterface dialog, int which)
* {
*
*
* } }); alertDialog.show(); } else{
*/
/*
* if (bestdeal_list.isEmpty()) {
* Toast.makeText(getApplicationContext(), "Empty Menu",
* Toast.LENGTH_LONG).show(); } else{
*/
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
RestaurantDetails.this, bestdeal_list,
R.layout.menu_list, new String[] { TAG_MENUNAME,
TAG_PRICE }, new int[] { R.id.textView1,
R.id.textView3 });
list.setAdapter(adapter);
}
});
}
// }
}
모든 것이 잘 작동하지만 목록보기를 섹션으로 나누어 코드를 수정하고 싶습니다. 카테고리 1에서 처음 4 개의 목록 항목을 카테고리 2에서 다른 4 개의 목록 항목으로 원합니다. 확장 가능한 목록보기를 원하지 않습니다. 위에서 언급 한 코드를 수정하기 만하면됩니다. runOnUiThread(Runnable)
를 통해 코드를 실행할 필요가 정말 없다, 그래서
사용자 지정 어댑터를 구현하여이 작업을 수행 할 수 있습니다. http://stackoverflow.com/a/1606642/2410326 – ooops
http://stackoverflow.com/questions/2394514/how-to-generate-a-listview-with-headers-above-some-sections –