How to apply condition to click of Listview item in android

private void showPopup(final Activity context, Point p) {
        int popupWidth = 800;
        int popupHeight = 1100;

        // Inflate the popup_layout.xml
        LinearLayout viewGroup = (LinearLayout) context
                .findViewById(R.id.popup);
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.notifcationpopup_layout,
                viewGroup);
        popup = new PopupWindow(context);
        popup.setContentView(layout);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setFocusable(true);

        // Some offset to align the popup a bit to the right, and a bit down,
        // relative to button position.
        int OFFSET_X = 100;
        int OFFSET_Y = 40;

        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

        // Displaying the popup at the specified location, + offsets.
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
                + OFFSET_Y);

        rowItems = new ArrayList<RowItem>();
        // for (int i = 0; i < titles.length; i++) {
        // RowItem item = new RowItem(titles[i], descriptions[i]);
        // rowItems.add(item);
        // }
        dbManager1 = new DataBaseManager(context);
        notifies = dbManager1.getNotificationList();
        System.out.println("today " + notifies.size());
        for (int i = 0; i < notifies.size(); i++) {
            System.out
                    .println("Valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee:"
                            + notifies.get(i).getNotificationDescrpiton()
                            + "\n"
                            + notifies.get(i).getNotificationServerId()
                            + "\n" + notifies.get(i).getNotificationDatetime());

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

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub

        String notifcationtype = notifies.get(position).getNotificationType();
        String nOtifcationMessge = notifies.get(position)
                .getNotificationDescrpiton();
        String notifcationdatetime = notifies.get(position)
                .getNotificationDatetime();

        String NotifcationSenderid = notifies.get(position)
                .getNotificationSenderID();
        String NotifcationRecciverid = notifies.get(position)
                .getNotificationRecieverID();

        final int positionRow = listView.getPositionForView((View) view
                .getParent());

        String Latreccvier = "";
        String Lonreccvier = "";
        Cursor cursor = dbManager1.fetchContactInfo(NotifcationRecciverid);
        if (cursor != null && cursor.moveToFirst()) {

            Latreccvier = cursor.getString(cursor

            .getColumnIndex(DataProviderConstants.CONTACT_LAT_COLUMN));

            Lonreccvier = cursor.getString(cursor

            .getColumnIndex(DataProviderConstants.CONTACT_LON_COLUMN));

            cursor.close();

        }

        Log.e("Reccvierid", NotifcationRecciverid);
        Log.e("Reccvierid", Latreccvier);
        Log.e("Reccvierid", Lonreccvier);

        // Intent i = null;
        if (notifcationtype.equals("1")) {
            // i = new Intent(context, PostActivity.class);

            Intent i = new Intent(this, RoutePathActivity.class);
            i.putExtra("lat", Latreccvier);
            i.putExtra("lon", Lonreccvier);
            startActivity(i);
            Toast.makeText(this, "Request", 1000).show();

        } else if (notifcationtype.equals("2")) {

            displayView(0);

            // ((FragHome) fragment).showMap();

            popup.dismiss();

            Toast.makeText(this, "Post", 1000).show();

        }

    }

      

This is my HOmeActivity code for the popup.

public class Notifcationadapter extends BaseAdapter {
    private static ArrayList<Notify> values;
    Context context;
    String senderid;
    String imagepath;

    // 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 noimage;
        TextView revresetime;

        TextView rejected;
        ImageView notificationuserimage;
        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.notificationuserimage = (ImageView) convertView
                    .findViewById(R.id.notificationuserimage);
            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");

            senderid = notifies.get(position).getNotificationSenderID();
            imagepath = "http://api.lociiapp.com/TransientStorage/"

            + senderid + ".jpg";
            AQuery aq = new AQuery(context);

            aq.id(holder.notificationuserimage).image(imagepath);

            holder.yesimage.setVisibility(View.VISIBLE);
            holder.noimage.setVisibility(View.VISIBLE);

            holder.yesimage.setTag(convertView);
            holder.yesimage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    View parentView = (View) arg0.getTag();
                    View view = parentView.findViewById(R.id.imageView1);
                    view.setVisibility(View.GONE);
                    view = parentView.findViewById(R.id.imageView2);
                    view.setVisibility(View.GONE);
                    view = parentView.findViewById(R.id.linear2);
                    view.setVisibility(View.INVISIBLE);
                    view = parentView.findViewById(R.id.runnimage);
                    view.setVisibility(View.INVISIBLE);
                    Toast.makeText(context, "Yes", 10000).show();

                }
            });
            holder.noimage.setTag(convertView);
            holder.noimage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    View parentView = (View) v.getTag();
                    View view = parentView.findViewById(R.id.imageView1);
                    view.setVisibility(View.GONE);
                    view = parentView.findViewById(R.id.imageView2);
                    view.setVisibility(View.GONE);
                    view = parentView.findViewById(R.id.rejectedrequest);
                    view.setVisibility(View.VISIBLE);
                    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()));

            senderid = notifies.get(position).getNotificationSenderID();
            imagepath = "http://api.lociiapp.com/TransientStorage/"

            + senderid + ".jpg";
            AQuery aq = new AQuery(context);
            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 for List-view if the list view has one YES or No button in each list view. i want when we click on "yes" then only itemclik should work. I mean one element. I have to go to another action if we click "No button" then it shouldn't work. Item click click on item item click I shouldn't go to another action.

Below is the list view screen: enter image description here

+3


source to share


1 answer


Please check that I have edited your code. yesimage is your tick icon on the right. An update to the Notifcationadapter class has the code below.

     holder.yesimage.setOnClickListener(new OnItemClickListener( position ));

      private class OnItemClickListener  implements OnClickListener{           
             private int mPosition;         
             OnItemClickListener(int position){
                   mPosition = position;
            }          
           @Override
            public void onClick(View arg0) {    
               context.onItemtickClick(mPosition);           
           }               
     }  

      



In your home activity Add below code for onItemtickClick.

      public void onItemtickClick(int mPosition) {
        // TODO Auto-generated method stub
           Intent i = new Intent(HomeActivity.this,RoutePathActivity.class);    
            i.putExtra("lat", Latreccvier);
            i.putExtra("lon", Lonreccvier); 
            startActivity(i);
      }

      

0


source







All Articles