android – How to apply function on button click in ListView adapter-ThrowExceptions

Exception or error:

Using this code I am able to display Listview in Android:

listView = (ListView) layout.findViewById(R.id.listView1);
        Notifcationadapter adapter = new Notifcationadapter(this, notifies);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);


public class Notifcationadapter extends BaseAdapter {
    private static ArrayList<Notify> values;
    Context context;
    // List<RowItem> rowItems;
    ViewHolder holder = null;

    LinearLayout linear2;

    DataBaseManager dbManager = new DataBaseManager(context);
    ArrayList<Notify> notifies;

    public Notifcationadapter(Context context, ArrayList<Notify> notifies) {
        super();
        this.context = context;
        this.notifies = notifies;
    }

    /* private view holder class */
    private class ViewHolder {

        TextView txtTitle;
        TextView txtDesc;
        ImageView yesimage;
        ImageView runningimage;
        ImageView noimage;
        TextView revresetime;

        TextView rejected;

        LinearLayout linear2;
    }

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

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.notifcationlistitem, null);
            holder = new ViewHolder();
            holder.linear2 = (LinearLayout) convertView
                    .findViewById(R.id.linear2);
            holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);

            holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
            holder.yesimage = (ImageView) convertView
                    .findViewById(R.id.imageView1);
            holder.noimage = (ImageView) convertView
                    .findViewById(R.id.imageView2);
            holder.revresetime = (TextView) convertView
                    .findViewById(R.id.revresetime);
            holder.rejected = (TextView) convertView
                    .findViewById(R.id.rejectedrequest);

            holder.runningimage = (ImageView) convertView
                    .findViewById(R.id.runnimage);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // holder.txtTitle.setText("Title");
        String statustype = notifies.get(position).getNotificationType();

        if (statustype.equals("1")) {

            holder.txtDesc.setText(notifies.get(position)
                    .getNotificationDescrpiton());

            holder.revresetime.setText(Functions
                    .setLastSeenNotifcationTime(notifies.get(position)
                            .getNotificationDatetime()));
            holder.txtTitle.setText("Request For travelling");
            holder.yesimage.setVisibility(View.VISIBLE);
            holder.noimage.setVisibility(View.VISIBLE);

            holder.yesimage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                    Toast.makeText(context, "Yes", 10000).show();
                    holder.yesimage.setVisibility(View.GONE);
                    holder.noimage.setVisibility(View.GONE);
                    // holder.linear2.setVisibility(View.INVISIBLE);
                    holder.runningimage.setVisibility(View.VISIBLE);
                }
            });

            holder.noimage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "NO", 10000).show();
                    // holder.linear2.setVisibility(View.INVISIBLE);
                    holder.yesimage.setVisibility(View.GONE);
                    holder.noimage.setVisibility(View.GONE);
                    holder.rejected.setVisibility(View.VISIBLE);
                }
            });
        }

        else {

            holder.txtDesc.setText(notifies.get(position)
                    .getNotificationDescrpiton());
            holder.txtTitle.setText("Post");
            holder.revresetime.setText(Functions
                    .setLastSeenNotifcationTime(notifies.get(position)
                            .getNotificationDatetime()));
            holder.yesimage.setVisibility(View.GONE);
            holder.noimage.setVisibility(View.GONE);
        }

        if (position % 2 == 0) {

            convertView.setBackgroundColor(Color.parseColor("#ffffff"));
        }

        else {
            convertView.setBackgroundColor(Color.parseColor("#f5f6f1"));
        }

        return convertView;
    }

    @Override
    public int getCount() {
        return notifies.size();
    }

    @Override
    public Object getItem(int position) {
        return notifies.get(position).getNotificationType();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}

This is My adapter class my Listview Print Like this enter image description here

I have apply yes and not Button click function But when I click On first item yes Button than Item second data Invisible how I don’t know why this is coming. I want when I click on Yes (-/) then Both yes and no Image button should hide there should be text view visible it’s happening but it’s not per item when I click on first then it’s disappear second or fourth item like this please check my adapter where am doing mistake .

How to solve:

I suggest you to android on your buttons (Yes and no buttons) layout :

onclick="yesHandle"

And

onclick="noHandle"

Then in you activty, create the two methods :

public void yesHandle(View v){
   final int positionRow = listView.getPositionForView((View) v.getParent());
   //TODO rest 
   adapter.notifyDataSetChanged();
}

public void noHandle(View v){
   final int positionRow = listView.getPositionForView((View) v.getParent());
   //TODO rest
   adapter.notifyDataSetChanged();
}

Leave a Reply

Your email address will not be published. Required fields are marked *