android的ListView显示

本文介绍了一个基于Android平台的收支管理应用程序的设计与实现细节。重点展示了如何通过ListView展示收入信息,并利用自定义方法根据不同的管理类型显示相应的数据。此外,还介绍了如何通过Intent在不同活动之间传递数据。
package com.xiaoke.accountsoft.activity;

import java.util.List;

import com.xiaoke.accountsoft.dao.InaccountDAO;
import com.xiaoke.accountsoft.model.Tb_inaccount;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Inaccountinfo extends Activity {
	public static final String FLAG = "id";// 定义一个常量,用来作为请求码
	ListView lvinfo;// 创建ListView对象
	String strType = "";// 创建字符串,记录管理类型

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inaccountinfo);// 设置布局文件
		lvinfo = (ListView) findViewById(R.id.lvinaccountinfo);// 获取布局文件中的ListView组件

		ShowInfo(R.id.btnininfo);// 调用自定义方法显示收入信息

		lvinfo.setOnItemClickListener(new OnItemClickListener()// 为ListView添加项单击事件
		{
			// 覆写onItemClick方法
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String strInfo = String.valueOf(((TextView) view).getText());// 记录收入信息
				String strid = strInfo.substring(0, strInfo.indexOf('|'));// 从收入信息中截取收入编号
				Intent intent = new Intent(Inaccountinfo.this, InfoManage.class);// 创建Intent对象
				intent.putExtra(FLAG, new String[] { strid, strType });// 设置传递数据
				startActivity(intent);// 执行Intent操作
			}
		});
	}///onCreate

	private void ShowInfo(int intType) {// 用来根据传入的管理类型,显示相应的信息
		String[] strInfos = null;// 定义字符串数组,用来存储收入信息
		ArrayAdapter<String> arrayAdapter = null;// 创建ArrayAdapter对象
		strType = "btnininfo";// 为strType变量赋值
		InaccountDAO inaccountinfo = new InaccountDAO(Inaccountinfo.this);// 创建InaccountDAO对象
		// 获取所有收入信息,并存储到List泛型集合中
		List<Tb_inaccount> listinfos = inaccountinfo.getScrollData(0,
				(int) inaccountinfo.getCount());
		strInfos = new String[listinfos.size()];// 设置字符串数组的长度
		int m = 0;// 定义一个开始标识
		for (Tb_inaccount tb_inaccount : listinfos) {// 遍历List泛型集合
			// 将收入相关信息组合成一个字符串,存储到字符串数组的相应位置
			strInfos[m] = tb_inaccount.get_id() + "|" + tb_inaccount.getType()
					+ " " + String.valueOf(tb_inaccount.getMoney()) + "元     "
					+ tb_inaccount.getTime();
			m++;// 标识加1
		}
		// 使用字符串数组初始化ArrayAdapter对象
		arrayAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, strInfos);
		lvinfo.setAdapter(arrayAdapter);// 为ListView列表设置数据源
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();// 实现基类中的方法
		ShowInfo(R.id.btnininfo);// 显示收入信息
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二十六画生的博客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值