先说说我的测试机器:android 虚拟机,sdk 2.3.3。
直接上代码:
package com.listViewTest2; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.LinearLayout; import android.widget.Toast; public class ListViewTest2Activity extends Activity { /** Called when the activity is first created. */ private Button btn; private TextView tv; private ListView lv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv=(ListView)findViewById(R.id.listview); List<Map<String,String>> list=new ArrayList<Map<String,String>>(); for(int i=0;i<3;i++){ Map<String,String> map=new HashMap<String,String>(); map.put("button", "button:"+i); map.put("textview","textview:"+i); list.add(map); } SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.list_item, new String[] {"button","textview"}, new int[]{R.id.button,R.id.textView}); lv.setAdapter(adapter); } public void btnClick(View v){ LinearLayout layout=(LinearLayout)v.getParent();//获得button所在的view,然后通过view找到button String text=((Button)layout.findViewById(R.id.button)).getText().toString(); Toast.makeText(this, text, Toast.LENGTH_LONG).show(); TextView tv=(TextView)layout.findViewById(R.id.textView); tv.setText("I haves changed"); } }
button添加onclick方法有三种
第一种:
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
第二种:
class btn_clickListner implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case value:
break;default:
break;
}
}
}第三种就是上面所说的
附上demo:androidlistview(2)