2013-05-28 49 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可能有助于实施这两种方法。