首页 > 代码库 > 为listview中的控件添加点击事件

为listview中的控件添加点击事件

刚上班,作为新手,一直在学习。有一点小心得。是关于为listview的item中的控件添加点击事件。listview当然是自定义的。而这个笔记也只是给listview的item中的控件单独做个点击。和listview的onItemClickListener差不多。区别就是可以为item中的多个控件设置点击,比如item中的button,textview,imageview,通过点击可以跳转不同的activity。我要实现的终极目标是点击itme(只有一个textview),每个item跳转的activity都不同。感觉还有好长一段距离。完整代码如下:

activity代码:

public class UseUrl extends Activity{
private Button source;
private ListView datalist;
private ArrayList<Params> list = new ArrayList<Params>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_url);
source = (Button) findViewById(R.id.btn_open);
source.setOnClickListener(new OnClick());
datalist = (ListView) findViewById(R.id.list_data);
// datalist.setOnItemClickListener(new OnList());

}
private class OnClick implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
InputStream is = getAssets().open("json.xml");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
String datahttp://www.mamicode.com/= "";
while((data = http://www.mamicode.com/br.readLine()) != null){
builder.append(data);
}
JSONArray array = new JSONObject(builder.toString()).getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject object = (JSONObject) array.opt(i);
Params params = new Params();
params.setBtnType(object.getString("button_type"));
params.setBtnKey(object.getString("button_key"));
params.setBtnName(object.getString("button_name"));
list.add(params);
MyAdapter adapter = new MyAdapter(UseUrl.this, list);
datalist.setAdapter(adapter);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// private class OnList implements OnItemClickListener{
//
// @Override
// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
// long arg3) {
// // TODO Auto-generated method stub
// Intent intent = new Intent(UseUrl.this, ClickList.class);
// startActivity(intent);
// }
//
// }
}

属性类:

public class Params {

private String btnType;
private String btnKey;
private String btnName;

public String getBtnType() {
return btnType;
}
public void setBtnType(String btnType) {
this.btnType = btnType;
}
public String getBtnKey() {
return btnKey;
}
public void setBtnKey(String btnKey) {
this.btnKey = btnKey;
}
public String getBtnName() {
return btnName;
}
public void setBtnName(String btnName) {
this.btnName = btnName;
}
}

 

自定义adapter类:

public class MyAdapter extends BaseAdapter{

private Context context;
private ArrayList<Params> list = new ArrayList<Params>();

public MyAdapter(Context context,ArrayList<Params> list){
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = LayoutInflater.from(context).inflate(R.layout.adapter, null);
TextView type = (TextView) convertView.findViewById(R.id.text_type);
TextView key = (TextView) convertView.findViewById(R.id.text_key);
TextView name = (TextView) convertView.findViewById(R.id.text_name);
type.setText(list.get(position).getBtnType());
key.setText(list.get(position).getBtnKey());
name.setText(list.get(position).getBtnName());

name.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, ClickList.class);
context.startActivity(intent);
Log.e("context", "button"+position);
}
});
return convertView;
}
}

跳转类:

就一个布局。可以随便写。

主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btn_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_open" />
<ListView
android:id="@+id/list_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:fastScrollEnabled="true"
android:divider="#97F709"
android:dividerHeight="5dip" />

</LinearLayout>

adapter布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
android:id="@+id/text_type"
android:layout_marginRight="15dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:id="@+id/text_key"
android:layout_marginRight="15dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />

</LinearLayout>

 

为listview中的控件添加点击事件