ListView简单用法

3 篇文章 0 订阅
1 篇文章 0 订阅
package com.cn.listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;

public class ListView04 extends ListActivity {
private List<Map<String,Object>> mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData=getData();
MyAdapter mAdapter=new MyAdapter(this);
setListAdapter(mAdapter);
}


private List<Map<String,Object>> getData(){
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
Map<String,Object> map=new HashMap<String,Object>();
map.put("title", "title01");
map.put("info", "info01");
map.put("img", R.drawable.icon);
list.add(map);
map=new HashMap<String,Object>();
map.put("title", "title02");
map.put("info", "info02");
map.put("img", R.drawable.icon);
list.add(map);
return list;
}

public void showInfo(){
new AlertDialog.Builder(this)
.setTitle("myListView")
.setMessage("instruction")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {

}
}).show();
}

public final class ViewHolder{
public ImageView img;
public TextView title;
public TextView info;
public Button viewBtn;
}

public class MyAdapter extends BaseAdapter{
private LayoutInflater mflater;
public MyAdapter(Context mContext){
this.mflater=LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}

@Override
public Object getItem(int arg0) {
return null;
}

@Override
public long getItemId(int arg0) {
return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ViewHolder holder=null;
if (arg1==null) {
holder=new ViewHolder();
arg1=mflater.inflate(R.layout.main, null);
holder.img=(ImageView)arg1.findViewById(R.id.img);
holder.title=(TextView)arg1.findViewById(R.id.title);
holder.info=(TextView)arg1.findViewById(R.id.info);
holder.viewBtn=(Button)arg1.findViewById(R.id.btn);
arg1.setTag(holder);
}else{
holder=(ViewHolder)arg1.getTag();
}
holder.img.setBackgroundResource((Integer)mData.get(arg0).get("img"));
holder.title.setText((String)mData.get(arg0).get("title"));
holder.info.setText((String)mData.get(arg0).get("info"));
holder.viewBtn.setOnClickListener(new btn_click());
return arg1;
}

class btn_click implements OnClickListener{

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showInfo();
}

}

}
}
<?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="fill_parent">
<ImageView android:id="@+id/img" android:layout_width="45px"
android:layout_height="45px" />
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:stretchColumns="0">
<TableRow>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="left">
<TextView android:textSize="16px" android:textColor="#ffffff"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/title" />
<TextView android:textSize="13px" android:textColor="#ffffff"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/info" />
</LinearLayout>

<Button android:id="@+id/btn" android:text="@string/viewBtn"
android:layout_width="65px" android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值