gpt4 book ai didi

java - 如何在我的 CustomAdapter 上添加 OnItemClickListener

转载 作者:行者123 更新时间:2023-12-02 03:27:45 26 4
gpt4 key购买 nike

我在这里陷入困境,我想将 OnItemClickListener 集成到我的 CustomAdapter 中,因此当单击某个项目时,它会显示一些消息或其他内容。但我不确定在哪里设置它。我看过很多教程和一些帖子,但这只会让我更加困惑。因此,我需要指导如何以及在哪里设置它才能使其正常工作。这是我的代码:

MainActivity.java

ListView listView;
listView = (ListView) findViewById(R.id.list_item);
new ReadRSS(MainActivity.this, listView, "http://malaysiakini.com/en/news.rss").execute();

读取RSS.java

@Override
protected void onPostExecute(Void aVoid) {
//Dismiss progress dialog
super.onPostExecute(aVoid);
progressDialog.dismiss();



if (feedItems != null) {
CustomAdapter customAdapter = new CustomAdapter(context,R.layout.activity_listview,feedItems);
listView.setAdapter(customAdapter);

}

}

自定义适配器.java

public class CustomAdapter extends ArrayAdapter<FeedItem> {

public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}

public CustomAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;

if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_listview, null);
}

FeedItem p = getItem(position);

if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.title_text);
TextView tt2 = (TextView) v.findViewById(R.id.date_text);

if (tt1 != null) {
tt1.setText(p.getTitle());
}

if (tt2 != null) {
tt2.setText(p.getPubDate());
}
}

return v;
}
}

最佳答案

这不是您问题的直接答案,而是我自己的代码示例,因此您可以查看它并了解想法;

    ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.listword,R.id.wordView1, companies);

ListView listView = (ListView) findViewById(R.id.secondListView1);

listView.setAdapter(adapter);

// Item Click listener

listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> ok, View v, int position, long id){

// Do your stuff here

}
});

关于java - 如何在我的 CustomAdapter 上添加 OnItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38581212/

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