在列表视图的适配器内,按钮单击侦听器存在问题

发布于 2024-12-27 05:16:08 字数 1550 浏览 2 评论 0原文

public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {

//   new method
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;
     private ImageAndText imageAndText;

//constructor
public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts) {
    super(activity, 0, imageAndTexts);


    asyncImageLoader = new AsyncImageLoader();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Activity activity = (Activity) getContext();


    ////////////////////////////////////////////////////////////////////////////////////////////////
    // Load the image and set it on the ImageView


    //new method
    // Inflate the views from XML
    View rowView = convertView;
    ViewCache viewCache;

    if (rowView == null) {
        LayoutInflater inflater = activity.getLayoutInflater();
        rowView = inflater.inflate(R.layout.image_and_text_row, null);
        viewCache = new ViewCache(rowView);
        rowView.setTag(viewCache);
    } else {
        viewCache = (ViewCache) rowView.getTag();
    }
    imageAndText = getItem(position);

    Button btn2=(Button) findViewById(R.id.button1);

      btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                     //...........
               }
      });

该适配器适用于ListView,它采用代表listview 的行数据的image_and_text_row.xml。当我为 btn2 设置点击侦听器时,程序崩溃了。如果删除侦听器,则程序运行正常。

问题是为什么适配器不能在代码中包含按钮单击侦听器?

public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {

//   new method
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;
     private ImageAndText imageAndText;

//constructor
public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts) {
    super(activity, 0, imageAndTexts);


    asyncImageLoader = new AsyncImageLoader();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Activity activity = (Activity) getContext();


    ////////////////////////////////////////////////////////////////////////////////////////////////
    // Load the image and set it on the ImageView


    //new method
    // Inflate the views from XML
    View rowView = convertView;
    ViewCache viewCache;

    if (rowView == null) {
        LayoutInflater inflater = activity.getLayoutInflater();
        rowView = inflater.inflate(R.layout.image_and_text_row, null);
        viewCache = new ViewCache(rowView);
        rowView.setTag(viewCache);
    } else {
        viewCache = (ViewCache) rowView.getTag();
    }
    imageAndText = getItem(position);

    Button btn2=(Button) findViewById(R.id.button1);

      btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                     //...........
               }
      });

This adapter is for the ListView, and it takes the image_and_text_row.xml which represent the row data of the listview. The program crashed when I set a click listener to the btn2. If the listener is deleted then the program runs fine.

the question is why the adapter cannot have a button click listener inside the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡墨 2025-01-03 05:16:08

只是在黑暗中拍摄:但是您的行 Button btn2=(Button) findViewById(R.id.button1); 引用了适配器所在的 ListActivity 或 ListView。因此,R .id.button1 不存在...

您尝试过: Button btn2=((Button)rowView.findViewById(R.id.button1)); 这可能是您的问题(即无法看到您的 logcat)。 findViewById() 查找子项。原始语句将查找 Activity 或 View 的子级。这个新语句将找到 rowView 的子视图。

当然,这是一个假设,因为您还没有完整地描述应用程序或问题,所以我必须根据我们掌握的信息假设每一行都有一个按钮。

希望这会有所帮助,

FuzzicalLogic

PS 希望您能抓住这一暗示:如果缺乏相关信息,您将得到像这样的粗制滥造的答案。一个好的指导是:1)发生了什么? 2)你预计会发生什么? 3)你的调试资源表明什么? 4)我们需要了解哪些补充研究或概念?这些会导致更长的问题,但它们肯定更有效,并且由此产生的答案也更有效。

Just a shot in the dark: but your line Button btn2=(Button) findViewById(R.id.button1); refers to the ListActivity or ListView that the adapter is in. Therefore, R.id.button1 does not exist...

Have you tried: Button btn2=((Button)rowView.findViewById(R.id.button1)); This may be your issue (without being able to see your logcat, that is). findViewById() finds children. The original statement would look for the child of the Activity or View. This new statement would find the child of the rowView.

Of course, this is an assumption, as since you have not described the application or problem completely, I must assume according to the information that we do have that there is a button for every row.

Hope this helps,

FuzzicalLogic

P.S. Hope you caught the hint that you will get shoddy answers like this one if there is a lack of pertinent information. A good guide is: 1) What is happening? 2) What do you expect to happen? 3) What do your debugging resources indicate? 4) What supplemental research or concepts do we need to know? These lead to longer questions, but they are certainly more effective and so are the answers that result from them.

ヤ经典坏疍 2025-01-03 05:16:08

您应该做的是将 onClick 添加到按钮所在的 xml 文件中。
还要确保您的按钮位于您正在膨胀的同一布局内。

What you should do is add the onClick to your xml file where the button is.
Also make sure your button is inside the same layout you are inflating.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文