`

android开发综合应用之打分应用

 
阅读更多

该程序是一个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>

 运行结果:

 

  • 大小: 41.9 KB
  • 大小: 46 KB
分享到:
评论

相关推荐

    Android应用开发综合设计验收评分细则1

    2. 团队分工及组织(20%),团队成员分工是否明确,如何体现团队成员各自工作量 3. 项目功能实现(30%),需求分析中的功能是否实现,流程是否清晰 4. 交

    android开发入门与实战(下)

    4.3 Android开发活动及特色应用 4.3.1 开发应用的领域 4.3.2 AndroidMarket特色应用一览 4.4 你也可以做东家——申请Market账号 4.4.1 卖东西要先入伙——准备工作 4.4.2 入伙过程——申请 4.5 开张了——在Market上...

    《Google Android开发入门与实战》.pdf

     本书内容上涵盖了用android开发的大部分场景,从android基础介绍、环境搭建、sdk介绍、market使用,到应用剖析、组件介绍、实例演示等方面。从技术实现上,讲解了5个android平台下的完整综合实例及源代码分析,...

    android开发入门与实战(上)

    4.3 Android开发活动及特色应用 4.3.1 开发应用的领域 4.3.2 AndroidMarket特色应用一览 4.4 你也可以做东家——申请Market账号 4.4.1 卖东西要先入伙——准备工作 4.4.2 入伙过程——申请 4.5 开张了——在Market上...

    android开发实例大全_王东华

    本书以Android应用程序的开发为主题,并结合真实的案例向读者详细介绍了Android的基本组件的使用及应用程序开发的整个流程。本书的讲述由浅入深,实例全面并典型,几乎囊括了所有和Android应用相关的项目。全书分为...

    Google.Android开发入门与实战

    内容简介  《Android开发入门与实战》内容上涵盖了用Android开发的大部分场景,从Android基础介绍、... 最后,祝广大开发者的技术日益精进,早日开始Android开发之旅,赶上移动互联网的第一班车,共赢中国3G未来!

    Android入门到精通源代码.

    第2章 搭建Android开发环境 2.1 Android开发环境要求 2.2 JDK的安装和配置 2.2.1 安装JDK 2.2.2 配置JDK 2.3 Android SDK的下载和安装 2.3.1 下载Android SDK 2.3.2 安装Android SDK 2.3.3 创建Android虚拟设备 2.4 ...

    Google Android开发入门与实战的代码

    4.3 Android开发活动及特色应用 37 4.3.1 开发应用的领域 37 4.3.2 Android Market特色应用一览 38 4.4 你也可以做东家——申请Market账号 43 4.4.1 卖东西要先入伙——准备工作 43 4.4.2 入伙过程...

    安卓手机移动开发android studio 课程设计 星座APP

    缘:这个界面主要是进行星座配对分析,男生女生可以选择星座然后来进行配对分析,配对详情包含:配对评分、星座比重、解析、注意事项等。 运势:这个界面蕴含了十二个星座的图片,点开每个图片,会对这个星座的今年...

    微信公众平台应用开发:方法、技巧与案例.(机械工业.柳峰)

    他还是一位资深的Java软件开发工程师和Android/iOS移动应用开发工程师,活跃于CocoaChina、开源中国、CSDN等社区,CSDN博客专家,在CSDN博客撰写了系列微信公众平台二次开发的教程,深受欢迎并被广泛传播,也因此...

    android手机安全卫士

    1.整体项目采用MVC框架,是对android知识点的综合应用,用到的技术有 (activity,service,broadcast,content provider,Notification , 数据库,自定义title,自定义控件,自定义toast,widget,aidl进程间通讯, javascript和...

    实验1-Android环境搭建与调试.doc

    申明人(签名): 实验报告评语与评分: 评阅老师签名: "实验题目 "实验1 Android开发环境搭建与调试 " "实验地点及组别 " U508 1人/组 "实验时间 "2015年9月7日 " "实验目的 " "Windows平台下进行Android应用程序...

    OTTAAProject:加入我们,为残障儿童创建第一个增强型交流平台!

    项目信息网页资源图书馆使用的库是: -网络请求应用评分动态动画图形报告-加载图像 Java 7 lamba实现自然语言处理-Google的支持库 所需工具官方Android开发人员工具 Android Studio 资料库 比特桶象形图 阿拉萨克...

    MomoURU为Chrome「MomoURU para Chrome」-crx插件

    它对用户有很好的视觉帮助,对原始信息的基本改进是综合平均评分。 - 时间表:获取当前时间表。它有一个很好和干净的演讲,只关注学生上课的日子。 - 帐户:获取学生的帐户对帐单。侧重于所需的基本信息。 在...

Global site tag (gtag.js) - Google Analytics