为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
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("l");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
String data = "";
while((data = br.readLine()) != null){
builder.append(data);
}
JSONArray array = new String()).getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject object = (JSONObject) array.opt(i);
Params params = new Params();
params.String("button_type"));
params.String("button_key"));
params.String("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
listview控件在哪里
// 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.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(_type); TextView key = (TextView) convertView.findViewById(_key); TextView name = (TextView) convertView.findViewById(_name); type.(position).getBtnType());
key.(position).getBtnKey());
name.(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="schemas.android/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="schemas.android/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>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。