1

I am showing data in listview from json using AsynTask.

Code is here.

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

All things are working fine but i want to modify my code by dividing the listview into sections. I want first 4 list items under category 1 the other 4 list items under category 2. I dont want expandable listview. Just want to modify above mentioned code.

2

3 Answers 3

2
  1. onPostExecute is being called on the main ("UI") thread so there is really no need to run its code through runOnUiThread(Runnable).
  2. If you want to display two types of views in the same ListView you will need to modify your Adapter to supply it (see Adapter.getViewTypeCount()), then you will need to sort your data set (List in your example) so it will reflect your requested sort + sections, and finally you will need to handle it in your Adapter (return the appropriate type/view by the given position). Also see Adapter.getItemViewType() and Adapter.getView().
Sign up to request clarification or add additional context in comments.

2 Comments

can i get that after modifying my above code in post execute.
a. the postExecute() thingy doesn't really related to your core issue b. you can add sections related objects to your List (i.e. add(TitleA), add(DataA1), add(DataA2) ..., add(TitleB), add(DataB1), add(DataB2) ... -> just one of many ways to differentiate your Adapter data-set's inner types)
1

There are several options for you to chose from. Have a look at the link in one of the comments to your question, or take a look at the SectionedAdapter that I wrote a while back.

What you basically want to do is to use a custom adapter, most likely derived from BaseAdapter. You will need to override getViewTypeCount() and return the number of different kinds of list items that you have in the list. In your case it's 2 since you have normal list items and categories.

You will also have to override getItemViewType(position) and return either a 0 if the item at the specified position is a normal list item or a 1 if it's a category.

Finally you'll also have to override getView() and return a list item of the appropriate type (category or normal list item), based on getItemViewType().

Comments

1

Both britzl and avimak gave good answers but there is another approach that may be simpler and adequate for some use cases.

First specify a list item layout like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/section_header" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_below="@id/section_header" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- your layout here ... --> </RelativeLayout> </RelativeLayout> 

Then, in your adapter, decide if you want to show the section header or not.

@Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); bindSectionHeader(position, view); return view; } private void bindSectionHeader(int position, View view) { TextView sectionView = (TextView) view.findViewById(R.id.section_header); if (isBeginningOfSection(position)) { sectionView.setText(getSectionTitle(position)); sectionView.setVisibility(View.VISIBLE); } else { sectionView.setVisibility(View.GONE); } } private boolean isBeginningOfSection(int position) { // ... } private String getSectionTitle(int position) { // ... } 

An AlphabetIndexer may help with implementing the two methods.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.