OnItemClick侦听器无法在Custom ListView中工作

时间:2011-11-28 10:35:56

标签: android android-listview adapter onitemclicklistener

我创建了一个自定义列表视图并创建了自己的适配器类 我用以下代码夸大了视图:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View streamrowView = layoutInflater.inflate(R.layout.streamrow, parent,false);
    initUI(streamrowView);

    tvUserMsg.setText(values[position].Message);
    tvPostDate.setText(values[position].postDate);

    return streamrowView;
}



    private void initUI(View streamrowView) {
    tvUserMsg = (TextView) streamrowView.findViewById(R.id.tvUserMsg);
    tvPostDate = (TextView) streamrowView.findViewById(R.id.tvPostDate);

}

列表视图工作正常,但set onItemClick侦听器事件无法正常工作

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> data, View v, int pos,
                long arg3) {

            Toast.makeText(getApplicationContext,  "list clicked" ,1).show();
            System.out.println("*****List clicked!!!");

        }
    });

对于自定义行的onItemClick侦听器是否还有其他方法。

2 个答案:

答案 0 :(得分:7)

public View getView(final int position, View convertView,
                ViewGroup parent) {
            final View vi ;//= convertView;
            ViewHolder holder;
            // if (convertView == null) {
            vi = inflater.inflate(R.layout.row_mbc_language, null);
            holder = new ViewHolder();
            holder.txtViewLangItem = (TextView) vi
                    .findViewById(R.id.txtViewLangItem);

            holder.imgViewSelectedLang = (ImageView) vi
                    .findViewById(R.id.imgViewSelectedLang);

            vi.setTag(holder);

            vi.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    BibleActivity.selectedLangCode = MBCLanguageListAdapter.this.resultsLang[position].languageCode;
                    MainActivity.SavePreferences("langCode",
                            BibleActivity.selectedLangCode,
                            MBCLanguageListAdapter.this.activity);
                    notifyDataSetChanged();

                }
            });

            holder.txtViewLangItem.setText(this.resultsLang[position].language);
            if (this.resultsLang[position].languageCode
                    .equals(BibleActivity.selectedLangCode)) {
                holder.imgViewSelectedLang.setVisibility(View.VISIBLE);
            }

            return vi;
        }

答案 1 :(得分:3)

public View getView(final int position, View convertView, ViewGroup parent) {
convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {}
}