该程序是一个listView跟ratingbar综合的应用示例。先上代码如下:
main.java
import java.util.ArrayList; import java.util.List; import android.app.AlertDialog; import android.app.ListActivity; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RatingBar; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.AdapterView.OnItemSelectedListener; public class Main extends ListActivity { private static String[] applicationNames = new String[] { "多功能日历", "eoeMarket客户端", "耐玩的重力消砖块", "白社会", "电池管家" }; private static String[] authors = new String[] { "张三", "eoemobile", "小费", "ApkHome", "lucyfa" }; private static int[] resIds = new int[] { R.drawable.calendar, R.drawable.eoemarket, R.drawable.brick, R.drawable.whitesociety, R.drawable.terminater }; private static float[] applicationRating = new float[] { (float) 5.0, (float) 5.0, (float) 3.5, (float) 5.0, (float) 4.0 }; String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater layoutInflater; private RatingAdapter raAdapter; /** * 自定义适配器, * 因为simpleAdapter只支持textView,imagesView,或是想Checkable接口的类,所以这里不能使用; * @author lyj * */ private class RatingAdapter extends BaseAdapter { private Context context; public RatingAdapter(Context context) { this.context = context; //获取扩展对象,用于操作布局文件 layoutInflater = (LayoutInflater) context .getSystemService(inflater); } @Override public int getCount() { return applicationNames.length; } @Override public Object getItem(int position) { return applicationNames[position]; } @Override public long getItemId(int position) { return position; } public void setRating(int position, float rating) { applicationRating[position] = rating; //更新数据 notifyDataSetChanged(); } /** * 设置列表项值,返回定义视图 */ @Override public View getView(int position, View convertView, ViewGroup parent) { //获取布局对象,进行布局文件里的组件操作 LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate( R.layout.main, null); ImageView ivLogo = (ImageView) linearLayout .findViewById(R.id.ivLogo); TextView tvApplicationName = ((TextView) linearLayout .findViewById(R.id.tvApplicationName)); TextView tvAuthor = (TextView) linearLayout .findViewById(R.id.tvAuthor); TextView tvRating = (TextView) linearLayout .findViewById(R.id.tvRating); RatingBar ratingBar = (RatingBar) linearLayout .findViewById(R.id.ratingbar); ivLogo.setImageResource(resIds[position]); tvApplicationName.setText(applicationNames[position]); tvAuthor.setText(authors[position]); tvRating.setText(String.valueOf(applicationRating[position])); ratingBar.setRating(applicationRating[position]); return linearLayout; } } @Override protected void onListItemClick(ListView l, View view, final int position, long id) { View myView = getLayoutInflater().inflate(R.layout.rating, null); final RatingBar ratingBar = (RatingBar) myView .findViewById(R.id.ratingbar); ratingBar.setRating(applicationRating[position]); new AlertDialog.Builder(this).setTitle(applicationNames[position]) .setMessage("给应用程序打分").setIcon(resIds[position]) .setView(myView).setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { raAdapter.setRating(position, ratingBar.getRating()); } }).setNegativeButton("取消", null).show(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); List<View> viewList = new ArrayList<View>(); viewList.add(getLayoutInflater().inflate(R.layout.main, null)); raAdapter = new RatingAdapter(this); setListAdapter(raAdapter); } }
程序中使用到了自定义的适配器,不熟悉adapter的朋友可以先去查阅下资料。
布局文件:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical"> <ImageView android:id="@+id/ivLogo" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/icon" android:paddingLeft="5dp" /> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:padding="10dp"> <TextView android:id="@+id/tvApplicationName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16dp" /> <TextView android:id="@+id/tvAuthor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tvApplicationName" android:textSize="14dp" /> </RelativeLayout> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:padding="10dp"> <TextView android:id="@+id/tvRating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5.0" /> <RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" style="?android:attr/ratingBarStyleSmall" android:layout_below="@id/tvRating" /> </RelativeLayout> </LinearLayout>
弹出对话框的布局文件:
rating.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical"> <RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5"/> </LinearLayout>
运行结果: