1

我有一个扩展列表视图的类。我需要在该列表视图上有 onitemclicklistener。以下代码不起作用。我的意思是点击监听器不起作用。如何解决这个问题?

列表视图.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>

元素.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>

列表.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;
    }
}

需要帮忙。谢谢。欣赏

4

3 回答 3

2

你必须重写类 onListItemClick()的方法ListActivity

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//Your code goes here..     
}
于 2012-04-27T07:15:22.053 回答
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;

在您的适配器类中。

编辑更改示例代码...

于 2012-04-27T07:16:03.630 回答
-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

    }

});
于 2012-04-27T07:15:25.880 回答