如何在listview中添加按钮功能

时间:2016-11-25 09:48:03

标签: android listview button

我的代码......

ListView listView;
SimpleCursorAdapter adapter;
mydb = new DBHelper(this);
Cursor cursor = mydb.fetch();
    listView = (ListView) findViewById(R.id.listView);
    adapter = new SimpleCursorAdapter(this, R.layout.list, cursor, from, to, 0);
    adapter.notifyDataSetChanged();
    listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            id = (TextView) arg1.findViewById(R.id.id);

            int id_To_Search = Integer.valueOf(id.getText().toString());

            Bundle dataBundle = new Bundle();
            dataBundle.putInt("_id", id_To_Search);

            Intent intent = new Intent(getApplicationContext(), AddPatient.class);

            intent.putExtras(dataBundle);
            startActivity(intent);
        }
    });

我在每个列表中都添加了一个按钮。如何使用该按钮作为项目点击事件列表视图的相同功能。我该如何使用该按钮。?

2 个答案:

答案 0 :(得分:1)

首先,在列表项布局中添加一个Button。然后尝试创建自定义适配器,而不是使用SimpleCursorAdapter。 以下是自定义适配器的示例

    public class MySimpleArrayAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] values;

        public MySimpleArrayAdapter(Context context, String[] values) {
                super(context, -1, values);
                this.context = context;
                this.values = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater) context
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View rowView = inflater.inflate(R.layout.list, parent, false);
                TextView textView = (TextView) rowView.findViewById(R.id.text);
                Button button = (Button) rowView.findViewById(R.id.button);
                textView.setText(values[position]);
                // change the text for your list item
                button.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                      // do whatever you want
            }
        });

        }
}

现在从光标

弹出字符串数组

答案 1 :(得分:0)

您可以在其上设置点击监听器,并将onItemClick的代码用于该onclick。

public class AdHocDealsAdapter extends RecyclerView.Adapter<AdHocDealsAdapter.ViewHolder> {
private final ArrayList<AdHocDealsModel> adHocDealsModelArrayList;
View v;
FragmentActivity context;
private Bundle bundle;
com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
        .cacheOnDisc(true).resetViewBeforeLoading(true).
                build();

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.deals_list_item, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    final AdHocDealsModel adHocDealsModel = adHocDealsModelArrayList.get(position);
    holder.txt_c_title.setText(adHocDealsModel.name + "");
    holder.txt_c_category.setText(adHocDealsModel.category + "");
    holder.txt_c_vendor.setText(adHocDealsModel.vendor_name + "");
    holder.txt_c_discount.setText(adHocDealsModel.volume_of_deal + "");
    if (adHocDealsModel.like == AppDelegate.DISLIKE) {
        holder.img_c_like_dislike.setSelected(false);
    } else if (adHocDealsModel.like == AppDelegate.LIKE) {
        holder.img_c_like_dislike.setSelected(true);
    }
    holder.img_c_like_dislike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (new Prefs(context).getUserdata() != null) {
                if (holder.img_c_like_dislike.isSelected()) {
                    holder.img_c_like_dislike.setSelected(false);
                } else {
                    holder.img_c_like_dislike.setSelected(true);
                }
                if (DealsActivity.onListItemClickListener != null) {
                    DealsActivity.onListItemClickListener.setOnListItemClickListener(Tags.LIKE_DISLIKE_CLICKED, position, holder.img_c_like_dislike.isSelected());
                }
            } else {
                AppDelegate.showToast(context, " Please Login first.");
            }
        }

    });
    holder.img_loading1.setVisibility(View.VISIBLE);
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            AnimationDrawable frameAnimation = (AnimationDrawable) holder. img_loading1.getDrawable();
            frameAnimation.setCallback( holder.img_loading1);
            frameAnimation.setVisible(true, true);
            frameAnimation.start();
            ((Animatable)  holder.img_loading1.getDrawable()).start();
        }
    });
    imageLoader.loadImage(adHocDealsModel.image, options, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            holder.img_c_item_image.setImageBitmap(loadedImage);
            holder.img_loading1.setVisibility(View.GONE);
        }

        @Override
        public void onLoadingCancelled(String imageUri, View view) {
        }
    });
    holder.txt_c_viewdetail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, AdHocDealDetailActivity.class);
            final Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(context, false, new Pair<>(holder.img_c_item_image, "square_blue_name_1"));
            ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(context, pairs);
            intent.putExtra(Tags.deal_id, adHocDealsModel.id);
            context.startActivity(intent, transitionActivityOptions.toBundle());
        }
    });

}

public AdHocDealsAdapter(FragmentActivity context, ArrayList<AdHocDealsModel> adHocDealsModelArrayList) {
    this.context = context;
    this.adHocDealsModelArrayList = adHocDealsModelArrayList;
    // this. ds=ds;
}

@Override
public int getItemCount() {
    return adHocDealsModelArrayList.size();
}


class ViewHolder extends RecyclerView.ViewHolder {
    ImageView img_c_item_image, img_c_like_dislike;
    TextView txt_c_title, txt_c_category, txt_c_vendor, txt_c_discount, txt_c_viewdetail;
    android.widget.ImageView img_loading1;

    public ViewHolder(View itemView) {
        super(itemView);
        img_c_item_image = (ImageView) itemView.findViewById(R.id.img_c_item_image);
        img_loading1 = (android.widget.ImageView) itemView.findViewById(R.id.img_loading1);
        img_c_like_dislike = (ImageView) itemView.findViewById(R.id.img_c_like_dislike);
        txt_c_title = (TextView) itemView.findViewById(R.id.txt_c_title);
        txt_c_category = (TextView) itemView.findViewById(R.id.txt_c_category);
        txt_c_vendor = (TextView) itemView.findViewById(R.id.txt_c_vendor);
        txt_c_discount = (TextView) itemView.findViewById(R.id.txt_c_discount);
        txt_c_viewdetail = (TextView) itemView.findViewById(R.id.txt_c_viewdetail);
        itemView.findViewById(R.id.ll_c_main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
}

}

这是一个示例,您可以在onBindViewHolder方法中看到我设置了新的单击侦听器并保存到共享首选项。