首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >listview中的"Separator“

listview中的"Separator“
EN

Stack Overflow用户
提问于 2013-05-28 17:28:58
回答 3查看 355关注 0票数 1

我使用AsynTask在json的列表视图中显示数据。

代码在这里。

代码语言:javascript
复制
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);

            }
        });
    }
    // }
}

一切都很好,但我想通过将listview划分为几个部分来修改我的代码。我希望前4个列表项在类别1下,其他4个列表项在类别2下。我不想要可扩展的列表视图。只想修改上面提到的代码。

EN

回答 3

Stack Overflow用户

发布于 2013-05-28 17:39:04

  1. onPostExecute是在主("UI")线程上被调用的,所以实际上没有必要通过runOnUiThread(Runnable).
  2. If运行它的代码你想要在同一个ListView中显示两种类型的视图你需要修改你的Adapter来提供它(参见Adapter.getViewTypeCount()),然后你需要对你的数据集(在你的例子中是List)进行排序,这样它将反映你所请求的sort + sections,最后你需要在你的Adapter中处理它(根据给定的位置返回适当的类型/视图)。另请参见Adapter.getItemViewType()Adapter.getView().
票数 2
EN

Stack Overflow用户

发布于 2013-05-28 17:41:59

有几个选项可供您选择。看看你的问题评论中的链接,或者看看我前段时间写的SectionedAdapter

您基本上想要做的是使用一个自定义适配器,它很可能是从BaseAdapter派生的。您需要覆盖getViewTypeCount()并返回列表中不同种类的列表项的数量。在你的例子中是2,因为你有正常的列表项和类别。

您还必须重写getItemViewType(position),如果指定位置的项是普通列表项,则返回0;如果是类别,则返回1。

最后,您还必须重写getView(),并根据getItemViewType()返回适当类型(类别或普通列表项)的列表项。

票数 1
EN

Stack Overflow用户

发布于 2013-05-28 18:36:12

britzl和avimak都给出了很好的答案,但有另一种方法可能更简单,更适合某些用例。

首先指定一个列表项布局,如下所示:

代码语言:javascript
复制
<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>

然后,在您的适配器中,决定是否要显示节标题。

代码语言:javascript
复制
@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可能有助于实现这两个方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16788520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档