2013-05-28 45 views
1

我使用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個列表項目。我不想要可擴展列表視圖。只是想修改上面提到的代碼。

+0

您可以通過實現您的自定義適配器來實現。 http://stackoverflow.com/a/1606642/2410326 – ooops

+0

http://stackoverflow.com/questions/2394514/how-to-generate-a-listview-with-headers-above-some-sections –

回答

2
  1. onPostExecute被稱爲主(「UI」)線程所以實在沒有必要通過runOnUiThread(Runnable)運行其代碼。
  2. 如果你想在同一ListView顯示兩種觀點,您將需要修改Adapter供應(見Adapter.getViewTypeCount()),那麼你需要將你的數據集(List在你的例子)進行排序,以便它將反映你所要求的排序+部分,最後你需要在你的適配器中處理它(返回給定位置的適當類型/視圖)。 另見Adapter.getItemViewType()Adapter.getView()
+0

我可以得到在後執行修改我的上述代碼後。 –

+0

a。 'postExecute()'thingy並不真正與您的核心問題 b有關。你可以添加相關對象到你的List中(即添加(TitleA),添加(DataA1),添加(DataA2)...,添加(TitleB),添加(DataB1),添加(DataB2)... - >只是區分Adapter數據集內部類型的衆多方法之一) – avimak

1

有幾種選擇供您選擇。看看其中一條評論對你的問題的鏈接,或者看看我後面寫的SectionedAdapter

你基本上想要做的是使用自定義適配器,最有可能來自BaseAdapter。您將需要覆蓋getViewTypeCount()並返回列表中不同種類的列表項的數量。在你的情況下,它是2,因爲你有正常的列表項和類別。

您還必須覆蓋getItemViewType(position),如果指定位置的項目是普通列表項目,則返回0,如果是類別,則返回1。

最後,您還必須重寫getView(),並根據getItemViewType()返回適當類型(類別或普通列表項)的列表項。

1

britzl和avimak都給出了很好的答案,但還有另一種方法對於某些用例可能更簡單和充分。

首先指定列表項的佈局是這樣的:

<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> 

然後,在你的適配器,決定是否要顯示節頭或沒有。

@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) { 
    // ... 
} 

AlphabetIndexer可能有助於實施這兩種方法。