Android &引用;分离器;在listview中
我使用AsynTask在listview中显示来自json的数据 代码在这里Android &引用;分离器;在listview中,android,android-layout,listview,android-listview,Android,Android Layout,Listview,Android Listview,我使用AsynTask在listview中显示来自json的数据 代码在这里 public class MenuTask extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... arg0) { // TODO Auto-generated method stub List<NameValuePa
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);
}
});
}
// }
}
公共类菜单任务扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
List params=new ArrayList();
//正在从URL获取JSON字符串。。。。。。。。。。。。。。
JSONObject JSONObject=jParser.makeHttpRequest(
"http://smartaway.dk/json/submenu.php?resid=“+res_id,
“POST”,params);
试一试{
bestdeal=jsonObject.getJSONArray(标记菜单);
///通过通道循环。。。。。。。。
for(int i=0;ivalue
//地图放置(标签标识,标识);
地图放置(标记名称,重新命名);
地图放置(标签价格,城市/州);
/*
*地图放置(TAG_STREET,STREET);地图放置(TAG_COUSINE,
*cosine);map.put(TAG_RES_LOGO,reslogo);
*/
//将哈希列表添加到ArrayList
最佳交易清单。添加(地图);
}
// }
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
@抑制警告(“弃用”)
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
/*
*如果(bestdeal_list.isEmpty()){AlertDialog AlertDialog=new
*AlertDialog.Builder(getParent()).create();
*alertDialog.setTitle(“未找到最佳交易”);
*alertDialog.setButton(“确定”,新的DialogInterface.OnClickListener()
* {
*
*@Override public void onClick(DialogInterface dialog,int which)
* {
*
*
*}});alertDialog.show();}else{
*/
/*
*if(bestdeal\u list.isEmpty()){
*Toast.makeText(getApplicationContext(),“空菜单”,
*Toast.LENGTH_LONG).show();}其他{
*/
runOnUiThread(新的Runnable(){
公开募捐{
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
餐厅详情。这是最佳交易清单,
R.layout.menu列表,新字符串[]{TAG\u MENUNAME,
TAG_PRICE},新int[]{R.id.textView1,
R.id.textView3});
list.setAdapter(适配器);
}
});
}
// }
}
所有的事情都很好,但是我想通过将listview划分为多个部分来修改我的代码。我想要第一类下的前4个列表项,第二类下的其他4个列表项。我不想要可扩展的列表视图。只想修改上面提到的代码
onPostExecute
,因此实际上不需要通过runOnUiThread(Runnable)
运行其代码ListView
中显示两种类型的视图,则需要修改适配器来提供它(请参见Adapter.getViewTypeCount(),最后,您需要在适配器中处理它(按给定位置返回适当的类型/视图)。
另请参见Adapter.getItemViewType()
和Adapter.getView()
onPostExecute
,因此实际上不需要通过runOnUiThread(Runnable)
运行其代码ListView
中显示两种类型的视图,则需要修改适配器来提供它(请参见Adapter.getViewTypeCount(),最后,您需要在适配器中处理它(按给定位置返回适当的类型/视图)。
另请参见Adapter.getItemViewType()
和Adapter.getView()
有几个选项供您选择。看一看你的问题评论中的链接,或者看一看我不久前写的 您基本上想要做的是使用一个自定义适配器,它很可能来自。您需要覆盖并返回列表中不同种类的列表项的数量。在您的情况下,它是2,因为您有正常的列表项和类别 如果指定位置的项是普通列表项,则必须重写并返回0;如果是类别,则必须重写并返回1
最后,您还必须根据getItemViewType()重写并返回相应类型的列表项(类别或普通列表项)。有几个选项供您选择。看一看你的问题评论中的链接,或者看一看我不久前写的 您基本上想要做的是使用一个自定义适配器,它很可能来自。您需要覆盖并返回列表中不同种类的列表项的数量。在您的情况下,它是2,因为您有正常的列表项和类别 如果指定位置的项是普通列表项,则必须重写并返回0;如果是类别,则必须重写并返回1 最后,您还必须重写并返回适当类型(categ)的列表项
<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) {
// ...
}