本文介绍了OnItemClick监听器自定义的ListView不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我创建了一个自定义列表视图,并创建了自己的适配器类
我已经充入低于code中的观点:

  @覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
    LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看streamrowView = layoutInflater.inflate(R.layout.streamrow,父母,假);
    initUI(streamrowView);    tvUserMsg.setText(值[位置] .Message);
    tvPostDate.setText(值[位置] .postDate);    返回streamrowView;
}    私人无效initUI(查看streamrowView){
    tvUserMsg =(TextView中)streamrowView.findViewById(R.id.tvUserMsg);
    tvPostDate =(TextView中)streamrowView.findViewById(R.id.tvPostDate);}

列表视图可以正常使用,但设置onItemClick侦听器事件无法正常工作

  listView.setOnItemClickListener(新OnItemClickListener(){        公共无效onItemClick(适配器视图<>数据视图V,诠释POS机,
                长ARG3){            Toast.makeText(getApplicationContext列表点击,1).show();
            的System.out.println(*****列表点击!!!);        }
    });

有没有其他的办法了onItemClick监听器自定义行。


解决方案

 公共查看getView(最终诠释的立场,观点convertView,
                父母的ViewGroup){
            最终的VI; // = convertView;
            ViewHolder持有人;
            //如果(convertView == NULL){
            VI = inflater.inflate(R.layout.row_mbc_language,NULL);
            持有人=新ViewHolder();
            holder.txtViewLangItem =(TextView中)六
                    .findViewById(R.id.txtViewLangItem);            holder.imgViewSelectedLang =(ImageView的)六
                    .findViewById(R.id.imgViewSelectedLang);            vi.setTag(保持器);            vi.setOnClickListener(新OnClickListener(){                @覆盖
                公共无效的onClick(查看为arg0){
                    // TODO自动生成方法存根
                    BibleActivity.selectedLang code = MBCLanguageListAdapter.this.resultsLang [位置]。外语code;
                    MainActivity.Save preferences(郎code,
                            BibleActivity.selectedLang code,
                            MBCLanguageListAdapter.this.activity);
                    notifyDataSetChanged();                }
            });            holder.txtViewLangItem.setText(this.resultsLang [位置]。外语);
            如果(this.resultsLang [位置]。外语code
                    .equals(BibleActivity.selectedLang code)){
                holder.imgViewSelectedLang.setVisibility(View.VISIBLE);
            }            返回VI;
        }

I have created a custom list view and created my own adapter classI have inflated the view with below code:

@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);

}

List view is working perfectly, but the set onItemClick listener event is not working

    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!!!");

        }
    });

is there any other approach for onItemClick listener for custom rows.

解决方案
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;
        }

这篇关于OnItemClick监听器自定义的ListView不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 19:09