android ListView本行控件操作本行其它控件的重要方法(绝对原创,本人研究2天成果)
这是java代码
package org.lee.android;
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.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
* @author allin
*
*/
public class MyListView4 extends ListActivity {
private List<Map<String, Object>> mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData = getData();
MyAdapter adapter = new MyAdapter(this);
setListAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 1; i < 8; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "G1");
map.put("bei", "52");
map.put("zhui", "true");
map.put("info", "google 1");
map.put("img", R.drawable.i1);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G2");
map.put("info", "google 2");
map.put("bei", "52");
map.put("zhui", "true");
map.put("img", R.drawable.i2);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G3");
map.put("info", "google 3");
map.put("bei", "52");
map.put("zhui", "true");
map.put("img", R.drawable.i3);
list.add(map);
}
return list;
}
// ListView 中某项被选中后的逻辑
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.v("MyListView4-click", (String) mData.get(position).get("title"));
}
/**
* listview中点击按键弹出对话框
*/
public void showInfo() {
new AlertDialog.Builder(this).setTitle("我的listview")
.setMessage("介绍...")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
public final class ViewHolder {
public ImageView img;
public TextView title;
public TextView info;
public Button viewBtn;
private CheckBox isZuiHaoCbox;
private EditText beiShuEtxt;
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
String[] list;
ViewHolder holder = null;
TextView[] textViewsCanche;
CheckBox[] iszuihaoTxtCanche;
EditText[] beishuEditTxtCanche;
public MyAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
textViewsCanche = new TextView[mData.size()];//创建数组,主要目的为了保存每个item中的控件
iszuihaoTxtCanche = new CheckBox[mData.size()];
beishuEditTxtCanche = new EditText[mData.size()];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int positionTemp = position;
// if (convertView == null||textViewsCanche[positionTemp]==null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.vlist2, null);
holder.img = (ImageView) convertView.findViewById(R.id.img);
holder.title = (TextView) convertView.findViewById(R.id.title);
textViewsCanche[positionTemp] = holder.title;//在这里保存控件的信息
holder.isZuiHaoCbox = (CheckBox) convertView.findViewById(R.id.check);
iszuihaoTxtCanche[positionTemp] = holder.isZuiHaoCbox;//在这里保存控件的信息
holder.beiShuEtxt = (EditText) convertView.findViewById(R.id.text);
beishuEditTxtCanche[positionTemp] = holder.beiShuEtxt;//在这里保存控件的信息
holder.info = (TextView) convertView.findViewById(R.id.info);
holder.viewBtn = (Button) convertView.findViewById(R.id.view_btn);
convertView.setTag(holder);
// } else {
// holder = (ViewHolder) convertView.getTag();
// }
holder.img.setBackgroundResource((Integer) mData.get(position).get(
"img"));
holder.title.setText((String) mData.get(position).get("title"));
holder.info.setText((String) mData.get(position).get("info"));
holder.isZuiHaoCbox.setChecked(Boolean.valueOf((String) mData.get(position).get("zhui")));
holder.beiShuEtxt.setText((String) mData.get(position).get("bei"));
holder.viewBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
holder.title = textViewsCanche[positionTemp];
holder.title.setText(String.valueOf(positionTemp));
holder.beiShuEtxt = beishuEditTxtCanche[positionTemp];
holder.beiShuEtxt.setText(String.valueOf(positionTemp));
}
});
holder.isZuiHaoCbox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked == true) {
holder.title = textViewsCanche[positionTemp];
holder.title.setText("你好");
} else {
holder.beiShuEtxt = beishuEditTxtCanche[positionTemp];
holder.beiShuEtxt.setText(String.valueOf(positionTemp));
Map<String, Object> map=mData.get(positionTemp);
map.put("title", "帅哥");
map.put("bei", "同学");
map.put("zhui", "false");
map.put("info", "新港");
map.put("img", R.drawable.icon);
notifyDataSetChanged();//更新list
}
}
});
//holder.beiShuEtxt=beishuEditTxtCanche[position];
holder.beiShuEtxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
/*++这里显示出输入的字符串++*/
holder.title = textViewsCanche[positionTemp];
holder.title.setText(s);
}
});
return convertView;
}
}
}