gpt4 book ai didi

android - OnItemClick 监听器在自定义 ListView 中不起作用

转载 作者:行者123 更新时间:2023-11-29 14:25:30 25 4
gpt4 key购买 nike

我创建了一个自定义 ListView 并创建了我自己的适配器类我用下面的代码膨胀了 View :

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

}

ListView 工作正常,但设置的 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 监听器是否有任何其他方法。

最佳答案

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;
}

关于android - OnItemClick 监听器在自定义 ListView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8294558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com