Possible Duplicate:
Android: How to fire onListItemClick in Listactivity with buttons in list?




我已经开发了一个应用程序,其中我制作了ListActivity,其中custome listview将显示自定义项目list.all一切都会好起来,但是在这里我与itemOnClickListner混淆了。如何在listActivity中添加onclick列表器?因为没有任何可初始化的列表视图,我可以通过该列表视图控件设置列表器槽...我从here中找到了它,但它也对我不起作用

:这是代码::

package com.AppFavorits;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.Toast;

import com.FavoritesDB.CommentsDataSource;
import com.SharedDB.SharedCommentsDataSource;

public class Favorites extends ListActivity implements OnClickListener {
    protected static final String TAG = "Favorites";
    CommentsDataSource datasource;
    ListView lstFavrowlistv;

    float[] rate;
    static boolean[] bSelected;
    static ArrayList<Comment> alPackagenm;
    static ArrayList alAppName;
    static String[] strAppnm;
    Drawable[] alIcon;
    ViewHolder holder;
    static int sizeincrement = 1;
    private SharedCommentsDataSource ShrdDatasource;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onResume() {
        super.onResume();
        datasource = new CommentsDataSource(this);
        datasource.open();
        ShrdDatasource = new SharedCommentsDataSource(this);

        alAppName = datasource.getAllComments();
        alPackagenm = datasource.getAllPackage();

        Log.i(TAG, "values >>>" + alAppName);
        Log.i(TAG, "values >>>" + alPackagenm);
        int inc = 0;
        alIcon = new Drawable[200];
        for (int i = 0; i < alPackagenm.size(); i++) {
            Log.i(TAG, "Appname >>>" + GetAllApp.lstpinfo.get(i).pname);
            for (int j = 0; j < GetAllApp.lstpinfo.size(); j++) {
                if (alPackagenm
                        .get(i)
                        .toString()
                        .equalsIgnoreCase(
                                GetAllApp.lstpinfo.get(j).pname.toString())) {
                    alIcon[inc] = GetAllApp.lstpinfo.get(j).icon;
                    Log.i("TAG", "sqlPackagename"
                            + alPackagenm.get(i).toString());
                    Log.i("TAG", "from getAllapp"
                            + GetAllApp.lstpinfo.get(j).pname.toString());
                    inc++;
                }

            }
        }

        ArrayList<RowModel> list = new ArrayList<RowModel>();
        ArrayList<Model> Mlist = new ArrayList<Model>();
        rate = new float[alAppName.size()];
        bSelected = new boolean[alAppName.size()];
        Iterator itr = alAppName.iterator();
        String strVal = null;
        while (itr.hasNext()) {
            strVal += itr.next().toString() + ",";

        }

        int lastIndex = strVal.lastIndexOf(",");
        strVal = strVal.substring(0, lastIndex);
        System.out.println("Output String is : " + strVal);
        String strAr[] = strVal.split(",");
        int Appinc = 0;
        for (String s : strAr) {
            list.add(new RowModel(s));
            Appinc += 1;
        }
        for (String s : strAr) {
            Mlist.add(new Model(s));
        }
        setListAdapter(new RatingAdapter(list, Mlist));

        datasource.close();

    }

    class RowModel {
        String label;
        float rating = 0.0f;

        RowModel(String label) {
            this.label = label;
        }

        public String toString() {
            if (rating >= 3.0) {
                return (label.toUpperCase());
            }

            return (label);
        }
    }

    private RowModel getModel(int position) {
        return (((RatingAdapter) getListAdapter()).getItem(position));

    }

    class RatingAdapter extends ArrayAdapter<RowModel> {
        private ArrayList<Model> mlist;
        boolean[] checkBoxState;

        RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist) {
            super(Favorites.this, R.layout.outbox_list_item,
                    R.id.txvxFavrowiconappname, list);
            checkBoxState = new boolean[list.size()];
            this.mlist = mlist;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View row = super.getView(position, convertView, parent);
            holder = (ViewHolder) row.getTag();
            if (convertView == null) {
                holder = new ViewHolder(row);
                row.setTag(holder);

            } else {
                row = convertView;
                ((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
                        .get(position));
            }

            RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating,
                        boolean fromTouch) {
                    Integer myPosition = (Integer) ratingBar.getTag();
                    RowModel model = getModel(myPosition);

                    model.rating = rating;
                    rate[position] = rating;

                }
            };
            holder.ratingBar1.setOnRatingBarChangeListener(l);
            holder.chkbxFavrowsel
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Model element = (Model) holder.chkbxFavrowsel
                                    .getTag();
                            element.setSelected(buttonView.isChecked());
                            bSelected[position] = isChecked;
                            element.setsizeInc(sizeincrement);
                            // if (holder.chkbxFavrowsel.isChecked() ==
                            // isChecked) {
                            ShrdDatasource.open();
                            ShrdDatasource.createComment(alAppName
                                    .get(position).toString(),
                                    "https://play.google.com/store/apps/details?id="
                                            + alPackagenm.get(position)
                                                    .toString(), String
                                            .valueOf(rate[position]));
                            ShrdDatasource.close();
                            Log.i(TAG, "Check Position is " + position);
                            // }

                        }
                    });
            RowModel model = getModel(position);
            ViewHolder holder = (ViewHolder) row.getTag();
            holder.ratingBar1.setTag(new Integer(position));
            holder.ratingBar1.setRating(model.rating);
            holder.imgvFavrowiconappicon.setImageDrawable(alIcon[position]);
            holder.txvxFavrowiconappname.setText(alAppName.get(position)
                    .toString());
            holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected());
            holder.chkbxFavrowsel.setTag(mlist.get(position));
            return (row);
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        ShrdDatasource.close();
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG)
                .show();
        Log.i(TAG, "Click fire");

    }
}


更新::

package com.AppFavorits;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.FavoritesDB.CommentsDataSource;
import com.SharedDB.SharedCommentsDataSource;

public class Favorites extends ListActivity implements OnClickListener {
    protected static final String TAG = "Favorites";
    CommentsDataSource datasource;
    ListView lstFavrowlistv;

    float[] rate;
    static boolean[] bSelected;
    static ArrayList<Comment> alPackagenm;
    static ArrayList alAppName;
    static String[] strAppnm;
    Drawable[] alIcon;
    ViewHolder holder;
    static int sizeincrement = 1;
    private SharedCommentsDataSource ShrdDatasource;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onResume() {
        super.onResume();
        datasource = new CommentsDataSource(this);
        datasource.open();
        ShrdDatasource = new SharedCommentsDataSource(this);

        alAppName = datasource.getAllComments();
        alPackagenm = datasource.getAllPackage();

        Log.i(TAG, "values >>>" + alAppName);
        Log.i(TAG, "values >>>" + alPackagenm);
        int inc = 0;
        alIcon = new Drawable[200];
        for (int i = 0; i < alPackagenm.size(); i++) {
            Log.i(TAG, "Appname >>>" + GetAllApp.lstpinfo.get(i).pname);
            for (int j = 0; j < GetAllApp.lstpinfo.size(); j++) {
                if (alPackagenm
                        .get(i)
                        .toString()
                        .equalsIgnoreCase(
                                GetAllApp.lstpinfo.get(j).pname.toString())) {
                    alIcon[inc] = GetAllApp.lstpinfo.get(j).icon;
                    Log.i("TAG", "sqlPackagename"
                            + alPackagenm.get(i).toString());
                    Log.i("TAG", "from getAllapp"
                            + GetAllApp.lstpinfo.get(j).pname.toString());
                    inc++;
                }

            }
        }

        ArrayList<RowModel> list = new ArrayList<RowModel>();
        ArrayList<Model> Mlist = new ArrayList<Model>();
        rate = new float[alAppName.size()];
        bSelected = new boolean[alAppName.size()];
        Iterator itr = alAppName.iterator();
        String strVal = null;
        while (itr.hasNext()) {
            strVal += itr.next().toString() + ",";

        }

        int lastIndex = strVal.lastIndexOf(",");
        strVal = strVal.substring(0, lastIndex);
        System.out.println("Output String is : " + strVal);
        String strAr[] = strVal.split(",");
        int Appinc = 0;
        for (String s : strAr) {
            list.add(new RowModel(s));
            Appinc += 1;
        }
        for (String s : strAr) {
            Mlist.add(new Model(s));
        }
        setListAdapter(new RatingAdapter(list, Mlist));

        datasource.close();

    }

    class RowModel {
        String label;
        float rating = 0.0f;

        RowModel(String label) {
            this.label = label;
        }

        public String toString() {
            if (rating >= 3.0) {
                return (label.toUpperCase());
            }

            return (label);
        }
    }

    private RowModel getModel(int position) {
        return (((RatingAdapter) getListAdapter()).getItem(position));

    }

    class RatingAdapter extends ArrayAdapter<RowModel> implements OnClickListener {
        private ArrayList<Model> mlist;
        boolean[] checkBoxState;

        RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist) {
            super(Favorites.this, R.layout.outbox_list_item,
                    R.id.txvxFavrowiconappname, list);
            checkBoxState = new boolean[list.size()];
            this.mlist = mlist;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View row = super.getView(position, convertView, parent);
            holder = (ViewHolder) row.getTag();
            if (convertView == null) {
                holder = new ViewHolder(row);
                row.setTag(holder);

            } else {
                row = convertView;
                ((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
                        .get(position));
            }

            RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating,
                        boolean fromTouch) {
                    Integer myPosition = (Integer) ratingBar.getTag();
                    RowModel model = getModel(myPosition);

                    model.rating = rating;
                    rate[position] = rating;

                }
            };
            holder.ratingBar1.setOnRatingBarChangeListener(l);
            holder.chkbxFavrowsel
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Model element = (Model) holder.chkbxFavrowsel
                                    .getTag();
                            element.setSelected(buttonView.isChecked());
                            bSelected[position] = isChecked;
                            element.setsizeInc(sizeincrement);
                            // if (holder.chkbxFavrowsel.isChecked() ==
                            // isChecked) {
                            ShrdDatasource.open();
                            ShrdDatasource.createComment(alAppName
                                    .get(position).toString(),
                                    "https://play.google.com/store/apps/details?id="
                                            + alPackagenm.get(position)
                                                    .toString(), String
                                            .valueOf(rate[position]));
                            ShrdDatasource.close();
                            Log.i(TAG, "Check Position is " + position);
                            // }

                        }
                    });
            RowModel model = getModel(position);
            ViewHolder holder = (ViewHolder) row.getTag();
            holder.ratingBar1.setTag(new Integer(position));
            holder.ratingBar1.setRating(model.rating);
            holder.imgvFavrowiconappicon.setImageDrawable(alIcon[position]);
            holder.txvxFavrowiconappname.setText(alAppName.get(position)
                    .toString());
            holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected());
            holder.chkbxFavrowsel.setTag(mlist.get(position));
            return (row);
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             Toast.makeText(getApplicationContext(),
                        "hey this ", Toast.LENGTH_SHORT).show();
             Log.i(TAG, "Click this");
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        ShrdDatasource.close();
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG)
                .show();
        Log.i(TAG, "Click fire");

    }
}


更新3

package com.AppFavorits;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.Toast;

import com.FavoritesDB.CommentsDataSource;
import com.SharedDB.SharedCommentsDataSource;

public class Favorites extends Activity implements OnClickListener, OnItemClickListener {
    protected static final String TAG = "Favorites";
    CommentsDataSource datasource;
    ListView lstFavrowlistv;

    float[] rate;
    static boolean[] bSelected;
    static ArrayList<Comment> alPackagenm;
    static ArrayList alAppName;
    static String[] strAppnm;
    Drawable[] alIcon;
    ViewHolder holder;
    static int sizeincrement = 1;
    private SharedCommentsDataSource ShrdDatasource;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favorites);
        lstFavrowlistv = (ListView)findViewById(R.id.lstFavrowlistv);

        lstFavrowlistv.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {

                  Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG)
                .show();
          Log.i(TAG, "Click fire");
              }
          });
    }

    @Override
    protected void onResume() {
        super.onResume();


        datasource = new CommentsDataSource(this);
        datasource.open();
        ShrdDatasource = new SharedCommentsDataSource(this);

        alAppName = datasource.getAllComments();
        alPackagenm = datasource.getAllPackage();

        Log.i(TAG, "values >>>" + alAppName);
        Log.i(TAG, "values >>>" + alPackagenm);
        int inc = 0;
        alIcon = new Drawable[200];
        for (int i = 0; i < alPackagenm.size(); i++) {
            Log.i(TAG, "Appname >>>" + GetAllApp.lstpinfo.get(i).pname);
            for (int j = 0; j < GetAllApp.lstpinfo.size(); j++) {
                if (alPackagenm
                        .get(i)
                        .toString()
                        .equalsIgnoreCase(
                                GetAllApp.lstpinfo.get(j).pname.toString())) {
                    alIcon[inc] = GetAllApp.lstpinfo.get(j).icon;
                    Log.i("TAG", "sqlPackagename"
                            + alPackagenm.get(i).toString());
                    Log.i("TAG", "from getAllapp"
                            + GetAllApp.lstpinfo.get(j).pname.toString());
                    inc++;
                }

            }
        }

        ArrayList<RowModel> list = new ArrayList<RowModel>();
        ArrayList<Model> Mlist = new ArrayList<Model>();
        rate = new float[alAppName.size()];
        bSelected = new boolean[alAppName.size()];
        Iterator itr = alAppName.iterator();
        String strVal = null;
        while (itr.hasNext()) {
            strVal += itr.next().toString() + ",";

        }

        int lastIndex = strVal.lastIndexOf(",");
        strVal = strVal.substring(0, lastIndex);
        System.out.println("Output String is : " + strVal);
        String strAr[] = strVal.split(",");
        int Appinc = 0;
        for (String s : strAr) {
            list.add(new RowModel(s));
            Appinc += 1;
        }
        for (String s : strAr) {
            Mlist.add(new Model(s));
        }
        lstFavrowlistv.setAdapter(new RatingAdapter(list, Mlist));

        datasource.close();

    }

    class RowModel {
        String label;
        float rating = 0.0f;

        RowModel(String label) {
            this.label = label;
        }

        public String toString() {
            if (rating >= 3.0) {
                return (label.toUpperCase());
            }

            return (label);
        }
    }

    private RowModel getModel(int position) {
        return (((RatingAdapter) lstFavrowlistv.getAdapter()).getItem(position));

    }

    class RatingAdapter extends ArrayAdapter<RowModel> implements OnClickListener {
        private ArrayList<Model> mlist;
        boolean[] checkBoxState;

        RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist) {
            super(Favorites.this, R.layout.outbox_list_item,
                    R.id.txvxFavrowiconappname, list);
            checkBoxState = new boolean[list.size()];
            this.mlist = mlist;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View row = super.getView(position, convertView, parent);
            holder = (ViewHolder) row.getTag();
            if (convertView == null) {
                holder = new ViewHolder(row);
                row.setTag(holder);

            } else {
                row = convertView;
                ((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
                        .get(position));
            }

            RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating,
                        boolean fromTouch) {
                    Integer myPosition = (Integer) ratingBar.getTag();
                    RowModel model = getModel(myPosition);

                    model.rating = rating;
                    rate[position] = rating;

                }
            };
            holder.ratingBar1.setOnRatingBarChangeListener(l);
            holder.chkbxFavrowsel
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Model element = (Model) holder.chkbxFavrowsel
                                    .getTag();
                            element.setSelected(buttonView.isChecked());
                            bSelected[position] = isChecked;
                            element.setsizeInc(sizeincrement);
                            // if (holder.chkbxFavrowsel.isChecked() ==
                            // isChecked) {
                            ShrdDatasource.open();
                            ShrdDatasource.createComment(alAppName
                                    .get(position).toString(),
                                    "https://play.google.com/store/apps/details?id="
                                            + alPackagenm.get(position)
                                                    .toString(), String
                                            .valueOf(rate[position]));
                            ShrdDatasource.close();
                            Log.i(TAG, "Check Position is " + position);
                            // }

                        }
                    });
            RowModel model = getModel(position);
            ViewHolder holder = (ViewHolder) row.getTag();
            holder.ratingBar1.setTag(new Integer(position));
            holder.ratingBar1.setRating(model.rating);
            holder.imgvFavrowiconappicon.setImageDrawable(alIcon[position]);
            holder.txvxFavrowiconappname.setText(alAppName.get(position)
                    .toString());
            holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected());
            holder.chkbxFavrowsel.setTag(mlist.get(position));
            return (row);
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             Toast.makeText(getApplicationContext(),
                        "hey this ", Toast.LENGTH_SHORT).show();
             Log.i(TAG, "Click this");
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        ShrdDatasource.close();
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG)
                .show();
        Log.i(TAG, "Click fire");

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG)
        .show();
Log.i(TAG, "Click fire");

    }
}

最佳答案

在列表活动中使用getListview()获取列表。

在Oncreate中

 ListView lv = getListView();


http://www.mkyong.com/android/android-listview-example/

此链接双向

1- overriding  onListItemClick(
2- Setting you listener..

关于android - 单击ListActivity中的事件监听器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11133480/

10-10 02:50