onItemClickListener不适用于customListadapter

时间:2012-03-31 13:18:04

标签: android list listview onitemclicklistener

onItemClickListener不适用于customListadapter ..有人请帮助我。这是完整的代码。我已经在listclick上设置了listview。但它没有用。

在这里输入代码`B.BuddiesActivity.class

public class BuddiesActivity extends Activity {
    /**
     * list view
     */
    ListView listViewFriendsDetails;
    /**
     * adapter for list out the friendsDetails
     */
    ListViewCustomeAdapter detailsAdapter;

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        MenuInflater inflaterObject = getMenuInflater();
        inflaterObject.inflate(R.menu.buddiesmenu, menu);
        /*
         * to change the colour of menu background
         */
        getLayoutInflater().setFactory(new Factory() {
            @Override
            public View onCreateView(String name, Context context,
                    AttributeSet attrs) {
                if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
                    try {
                        LayoutInflater f = getLayoutInflater();
                        final View view = f.createView(name, null, attrs);
                        new Handler().post(new Runnable() {
                            public void run() {
                                // set the background drawable
                                view.setBackgroundColor(Color.GRAY);

                                // set the text color
                                ((TextView) view).setTextColor(Color.DKGRAY);

                            }
                        });
                        return view;
                    } catch (InflateException e) {
                    } catch (ClassNotFoundException e) {
                    } finally {
                        Log.i(Constants.DETAILS_WORKSHEET_ACTIVITY,
                                "menu colour changed");
                    }
                }
                return null;
            }
        });
        return super.onCreatePanelMenu(featureId, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.birthday:

            detailsAdapter = new ListViewCustomeAdapter(this,
                    DatabaseOperation.friendsBirthdayDetailsArrayListObject
                            .size(),
                    DatabaseOperation.friendsBirthdayDetailsArrayListObject,
                    true);

            listViewFriendsDetails.setAdapter(detailsAdapter);
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

    // Intent detailsIntent = new Intent(BuddiesActivity.this,
    // DetailsActivity.class);
    // detailsIntent
    // .setAction(DatabaseOperation.friendsDetailsArrayListObject
    // .get(position).getEmailId());
    // startActivity(detailsIntent);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        /*
         * Listing the songs and its details
         */
        listViewFriendsDetails = (ListView) findViewById(R.id.listView);

        detailsAdapter = new ListViewCustomeAdapter(this,
                DatabaseOperation.friendsDetailsArrayListObject.size(),
                DatabaseOperation.friendsDetailsArrayListObject, false);
        listViewFriendsDetails.setAdapter(detailsAdapter);
        registerForContextMenu(listViewFriendsDetails);

        listViewFriendsDetails
                .setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        System.out
                                .println("buttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt");
                    }
                });

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, 0, 0, "call");
        menu.add(0, 0, 0, "Message");
        menu.add(0, 0, 0, "Find him/her");
        menu.add(0, 0, 0, "Find his/her home");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info;
        try {
            info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        } catch (ClassCastException e) {
            Log.e("", "bad menuInfo", e);
            return false;
        }

        Toast.makeText(
                this,
                "id = "
                        + DatabaseOperation.friendsDetailsArrayListObject.get(
                                info.position).getFirstName(),
                Toast.LENGTH_SHORT).show();
        return true;
    }
}


This my adapter to list the items
ListViewCustomAdapter.class


public class ListViewCustomeAdapter extends BaseAdapter implements
        View.OnCreateContextMenuListener {
    /**
     * normal selection or birthday selection
     */
    Boolean IS_BIRTHDAY;
    /**
     * Information of the friendsdetails from the database
     */
    ArrayList<FriendsDetails> friendsDetailsInList = new ArrayList<FriendsDetails>();
    public Context context;
    public Context con;
    public LayoutInflater inflater;

    public ListViewCustomeAdapter(Context context, int count,
            ArrayList<FriendsDetails> friendsDetailsArryListObject,
            Boolean IS_BIRTHDAY) {
        super();
        this.context = context;
        this.IS_BIRTHDAY = IS_BIRTHDAY;
        friendsDetailsInList = friendsDetailsArryListObject;
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return friendsDetailsInList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 
     * 
     * @author Anitha
     * 
     */
    public static class ViewHolder {

        /**
         * Email Id
         */
        TextView emaiIdView;
        /**
         * First name
         */
        TextView txtViewTitle;
        /**
         * Status
         */
        TextView statusTextView;
        /**
         * The phone icon
         */
        ImageView phoneIcon;
        /**
         * The home icon
         */
        ImageView homeIcon;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        FriendsDetails particularFriendsInfo = friendsDetailsInList
                .get(position);

        if (convertView == null) {

            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.listitemrow, null);
            holder.txtViewTitle = (TextView) convertView
                    .findViewById(R.id.txtViewTitle);
            holder.statusTextView = (TextView) convertView
                    .findViewById(R.id.status);
            holder.emaiIdView = (TextView) convertView
                    .findViewById(R.id.emailId);
            holder.phoneIcon = (ImageView) convertView
                    .findViewById(R.id.mobileAvailable);
            holder.homeIcon = (ImageView) convertView
                    .findViewById(R.id.addressAvailable);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /*
         * If the person has no mobile number and address
         */
        if (particularFriendsInfo.getAddress() == null) {
            holder.homeIcon.setImageResource(R.drawable.mobileiconrestricted);
        }
        if (particularFriendsInfo.getMobileNumber() == null) {
            holder.phoneIcon.setImageResource(R.drawable.homerestricted);
        }
        holder.txtViewTitle.setText(particularFriendsInfo.getFirstName());
        holder.emaiIdView.setText(particularFriendsInfo.getEmailId());
        if (!IS_BIRTHDAY)
            holder.statusTextView.setText(particularFriendsInfo.getStatus());
        else
            holder.statusTextView.setText(particularFriendsInfo.getBirthday());
        convertView.setOnCreateContextMenuListener(this);
        // }
        return convertView;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub

    }
}

This is my xml where it it contains the view of each row in listview.
listitemrow.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@layout/itemselector"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="5dip">

    <ImageView 
    android:focusable="false"
        android:layout_width="50dip" 
        android:layout_height="50dip" 
        android:id="@+id/imgViewLogo" 
        android:src="@drawable/icon" 
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:scaleType="center">
    </ImageView>

    <TextView 
    android:focusable="false"
        android:layout_height="wrap_content" 
        android:text="TextView"
        android:textStyle="bold"
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:id="@+id/txtViewTitle" 
        android:layout_toRightOf="@+id/imgViewLogo"
        android:layout_marginLeft="2dip">
    </TextView>
    <ImageView
    android:focusable="false"
    android:src="@drawable/mobileicon"
    android:layout_toRightOf="@id/txtViewTitle"
    android:layout_marginLeft="50dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/mobileAvailable" 
    />
    <ImageView
    android:focusable="false"
    android:src="@drawable/home"
    android:layout_toRightOf="@id/mobileAvailable"
    android:layout_marginLeft="2dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/addressAvailable" 
    />
    <TextView 
    android:focusable="false"
        android:layout_height="wrap_content" 
        android:text="TextView" 
        android:textStyle="bold"
        android:textColor="#606060"
        android:layout_width="wrap_content" 
        android:id="@+id/emailId" 
        android:layout_toRightOf="@id/imgViewLogo"
        android:layout_below="@id/txtViewTitle"
        android:layout_marginRight="10dip"
        android:layout_marginLeft="2dip">
    </TextView>  
    <TextView
    android:focusable="false"
    android:layout_toRightOf="@id/emailId" 
    android:layout_below="@id/txtViewTitle"
    android:layout_width="wrap_content" 
    android:id="@+id/status"
    android:layout_height="wrap_content" 
    android:textStyle="bold"
    android:layout_marginLeft="10dip"
        android:textColor="#606060"

    />

</RelativeLayout>


listview.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/relativeLayout1"
   android:layout_width="fill_parent"
        android:listSelector="@layout/itemselector"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

       <ListView

        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/listView"

        android:layout_alignParentLeft="true">
    </ListView>
    </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

以编程方式将setfocus设置为false到您的imageview,textview等...如

                Button demo=(Button)row.findViewById(R.id.demo);
                demo.setFocusable(false);

这是用于按钮视图设置为imageview和其他

答案 1 :(得分:0)

这是我在类似情况下必须做的事情。和你一样,我试过只是 android:focusable="false"但它仍然没有用。

那么,我在xml中为行布局中的每个imageview和textview做了什么:

        android:focusable="false"
        android:focusableInTouchMode="false"

这确保键盘或触摸屏都不会导致焦点转到行中的项而不是行,并且应该使侦听器能够正常工作。