我正在创建一个内容应用程序,其中有一个页面显示餐馆列表及其详细信息,其中我使用了可扩展listView.So在子列表中我想显示两个项目,即菜肴和评级。我被困扰了评分部分。我在下面发布我的代码。请帮助我。
restaurant_pg.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@android:color/background_dark"
android:src="@drawable/eatery" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView1"
android:text="Restaurants"
android:background="@android:color/background_dark"
android:textSize="25sp"
android:textColor="@android:color/background_light"
android:gravity="center" />
<ExpandableListView
android:id="@+id/restaurant_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1" >
</ExpandableListView>
</RelativeLayout>
group_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/restaurant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/map"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/map"
android:gravity="center"
android:text=""
android:textSize="12sp" />
<ImageView
android:id="@+id/map"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/mapicon" />
child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/restaurant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/textView1"
android:gravity="center"
android:textSize="18sp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/textView3"
android:layout_marginRight="76dp"
android:src="@drawable/staricon" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignBottom="@+id/textView3"
android:layout_toLeftOf="@+id/imageView2"
android:src="@drawable/staricon" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignBottom="@+id/imageView2"
android:layout_alignLeft="@+id/imageView2"
android:layout_marginLeft="32dp"
android:src="@drawable/staricon" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignTop="@+id/imageView1"
android:layout_toLeftOf="@+id/imageView1"
android:src="@drawable/staricon" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_toLeftOf="@+id/imageView4"
android:text="Star rating"
android:textSize="18sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Cuisine -"
android:textColor="@android:color/background_light"
android:background="@android:color/background_dark"
android:textSize="18sp" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignTop="@+id/imageView1"
android:layout_toRightOf="@+id/imageView3"
android:src="@drawable/staricon" />
Restaurant_pg.java
package com.example.myagraguide;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.example.myagraguide.ExpandableListAdapter.*;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.os.Build;
public class Restaurant_pg extends Activity
{
List<String> groupList;
List<String> childList;
Map<String, List<String>> list_restaurants;
ExpandableListView expListView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.restaurant_pg);
createGroupList();
createCollection();
expListView = (ExpandableListView) findViewById(R.id.restaurant_list);
final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
this,list_restaurants, groupList );
expListView.setAdapter(expListAdapter);
}
private void createGroupList()
{
groupList = new ArrayList<String>();
groupList.add("Esphehan the Oberoi Amarvilas");
groupList.add("Panchhi Petha Store");
groupList.add("Pinch of Spice");
groupList.add("Shankara Vegis Restaurant");
groupList.add("Khandelaa");
groupList.add("Mughal Room");
groupList.add("Tandoori Nights");
groupList.add("Taj Mahal Restaurant");
}
/* private OnGroupClickListener myListGroupClicked = new OnGroupClickListener()
{
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
//get the group header
HeaderInfo headerInfo = deptList.get(groupPosition); */
private void createCollection() {
// preparing restaurant details(child)
String[] esphehan_detail = {"Indian" };
String[] panchhi_detail={"Indian"};
String[] pinch_of_spice={"Multi-cuisine "};
String[] shankara={"Indian"};
String[] khandela={"Mughalai"};
String[] mughal_room={"Chinese"};
String[] tandoori={"Tandoori"};
String[] taj_mahal={"Multi-Cuisine "};
list_restaurants=new LinkedHashMap<String,List<String>>();
for(String restaurant:groupList)
{
if(restaurant.equals("Esphehan the Oberoi Amarvilas"))
{
loadChild(esphehan_detail);
list_restaurants.put(restaurant,childList);
}
if(restaurant.equals("Panchhi Petha Store"))
{
loadChild(panchhi_detail);
list_restaurants.put(restaurant,childList);
}
if(restaurant.equals("Pinch of Spice"))
{
loadChild(pinch_of_spice);
list_restaurants.put(restaurant,childList);
}
if(restaurant.equals("Shankara Vegis Restaurant"))
{
loadChild(shankara);
list_restaurants.put(restaurant,childList);
}
if(restaurant.equals("Khandelaa"))
{
loadChild(khandela);
list_restaurants.put(restaurant,childList);
}
if(restaurant.equals("Mughal Room"))
{
loadChild(mughal_room);
list_restaurants.put(restaurant,childList);
}
if(restaurant.equals("Tandoori Nights"))
{
loadChild(tandoori);
list_restaurants.put(restaurant,childList);
}
if(restaurant.equals("Taj Mahal Restaurant"))
{
loadChild( taj_mahal);
list_restaurants.put(restaurant,childList);
}
}
}
private void loadChild(String[] rest_detail)
{
childList = new ArrayList<String>();
for (String options : rest_detail)
childList.add(options);
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ExpandableListAdapter
package com.example.myagraguide;
import java.util.List;
import java.util.Map;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter
{
private Activity context;
private Map<String, List<String>> list_restaurants;
private List<String> restaurants;
public ExpandableListAdapter(Activity context,Map<String, List<String>> list_restaurants,List<String> restaurants)
{
this.context=context;
this.list_restaurants=list_restaurants;
this.restaurants=restaurants;
}
public Object getChild(int groupPosition,int childPosition)
{
return list_restaurants.get(restaurants.get(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition,int childPosition)
{
return childPosition;
}
public View getChildView(final int groupPosition,final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
final String restaurants = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.child_item, null);
}
TextView tv=(TextView)convertView.findViewById(R.id.restaurant) ;
// return tv;
tv.setText(restaurants);
return convertView;
}
public int getChildrenCount(int groupPosition)
{
return list_restaurants.get(restaurants.get(groupPosition)).size();
}
public Object getGroup(int groupPosition)
{
return restaurants.get(groupPosition);
}
public int getGroupCount()
{
return restaurants.size();
}
public long getGroupId(int groupPosition)
{
return groupPosition;
}
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
String restaurantName = (String) getGroup(groupPosition);
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_item,
null);
}
TextView tv = (TextView) convertView.findViewById(R.id.restaurant);
tv.setTypeface(null, Typeface.BOLD);
/* ImageView contact=(ImageView)convertView.findViewById(R.id.contact);
contact.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
final Dialog dialog=new Dialog(context);
dialog.setContentView(R.layout.contact_tajmahal);
ImageView iv=(ImageView)dialog.findViewById(R.id.exitButton);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
dialog.dismiss();
// TODO Auto-generated method stub
}
});
dialog.show();
// TODO Auto-generated method stub
}
}); */
/*{
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to remove?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
List<String> child =
list_restaurants.get(restaurants.get(groupPosition));
// child.remove(childPosition);
notifyDataSetChanged();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}); */
tv.setText(restaurantName);
return convertView;
}
public boolean hasStableIds()
{
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}