我有一个扩展listview的类。我需要在listview上有onitemclicklistener。以下代码无效。我的意思是点击监听器不起作用。怎么解决这个问题?
listview.xml
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="47dip"
android:layout_marginTop="47dip" >
</ListView>
Elements.xml的
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="46dp"
android:gravity="center" >
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="ToggleButton"
android:textOff="Inactive"
android:textOn="Active" />
<TextView
android:id="@+id/label_note"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginRight="38dp"
android:layout_marginLeft="2dip"
android:layout_toRightOf="@+id/check_note"
android:text="@+id/label"
android:textSize="25px" />
<CheckBox
android:id="@+id/check_note"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:visibility="gone"
android:button="@drawable/customdrawablecheckbox" />
</RelativeLayout>
List.java
public class List extends ListActivity{
....
public void onCreate(Bundle icicle) {
this.setContentView(R.layout.listview);
super.onCreate(icicle);
}
ArrayAdapter<ListModel> adapter = new InteractiveArrayAdapter(
this, getModel(), false);
setListAdapter(adapter);
final ListView list1 = getListView();
list1.setItemsCanFocus(true);
list1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
// TODO Auto-generated method stub
// HashMap<String, String> o = (HashMap<String, String>) list1.getItemAtPosition(pos);
Toast.makeText(ContactsNoteList.this, "ID '" +list.get(pos).getName()+" "+list.get(pos).getId().toString() + "' was clicked.", Toast.LENGTH_LONG).show();
}
});
..}
private List<ListModel> getModel() {
for (int i=0;i<3;i++)
list.add(get("Elem"+i.toString(),i);
}
}
return list;
}
private ListModel get(String s1,int Id) {
return new ListModel(s1,Id);
}
InteractiveArrayAdapter.java
InteractiveArrayAdapter extends
ArrayAdapter<ListModel> {
private final List<ListModel> list;
private final Activity context;
private boolean visibility;
public InteractiveArrayAdapter(Activity context, List<ListModel> list,boolean visibility) {
super(context, R.layout.elements, list);
this.context = context;
this.list = list;
this.visibility=visibility;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.elements, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label_note);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check_note);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
ListModel element = (ListModel) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
if (visibility==true)
holder.checkbox.setVisibility(View.VISIBLE);
else
holder.checkbox.setVisibility(View.INVISIBLE);
return view;
}
}
需要帮助。谢谢。欣赏
答案 0 :(得分:2)
您必须覆盖onListItemClick()
类
ListActivity
方法
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//Your code goes here..
}
答案 1 :(得分:2)
使用:
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {...
Toast.makeText(context,"you clicked item: "+position,Toast.Lenght_LONG).show();
//code you want to execute on click of list item...
}
});
return view;
在适配器类中。
编辑更改了示例代码...
答案 2 :(得分:-1)
将其放入onCreate()
..
ListView mListView = getListView();
mListView .setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
}
});