1

Hi friends i have one custom list view in which i created two xml one for displaying listview and second one for each row in listview

everything is fine with my code and UI but my problem is lets take example when i show 8 rows in list view and i click on Button/Component in first row then automatically 5th one also get called means onClick event get applied to both 1st and 5th row components i dont know how to tackle this issue i know there is silly mistake is there can you please help me to out thanks in advance

here is code of customAdapter

public CustomAdapter(Activity a, ArrayList<?> d) {

        /********** Take passed values **********/
        activity = a;
        data = d;


        /*********** Layout inflator to call external xml layout () **********************/
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    /******** What is the size of Passed Arraylist Size ************/
    public int getCount() {

        if (data.size() <= 0)
            return 1;
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public int getViewTypeCount() {

        if (getCount() != 0)
            return getCount();

        return 1;
    }

    /********* Create a holder to contain inflated xml file elements ***********/
    public static class ViewHolder {

        public Button btnLike;
        public Button btnVoiceComment;
        public Button btnShare;
        public ImageButton ibMore;
        public ImageButton ibPlayAudio;
        public ImageView ivPostImage;

        public ProgressWheel pw_two;

        public TextView tvLocation, tvListens, tvMessage;

    }

    /*********** Depends upon data size called for each row , Create each ListView row ***********/
    public View getView(final int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        final ViewHolder holder;

        if (convertView == null) {

            /********** Inflate tabitem.xml file for each row ( Defined below ) ************/
            vi = inflater.inflate(R.layout.list_row_new, null);
            holder = new ViewHolder();
            /******** View Holder Object to contain tabitem.xml file elements ************/

            holder.btnLike = (Button) vi.findViewById(R.id.list_row_btn_like);
            holder.btnVoiceComment = (Button) vi
                    .findViewById(R.id.list_row_btn_voicecomment);
            holder.ibMore = (ImageButton) vi
                    .findViewById(R.id.list_row_imgbtn_more);
            holder.btnShare = (Button) vi
                    .findViewById(R.id.list_row_imgbtn_share);

            holder.ivPostImage = (ImageView) vi
                    .findViewById(R.id.list_row_iv_post);

            holder.ibPlayAudio = (ImageButton) vi
                    .findViewById(R.id.list_row_ib_play);

            holder.tvMessage = (TextView) vi.findViewById(R.id.list_row_tv_msg);
            holder.tvListens = (TextView) vi
                    .findViewById(R.id.list_row_tv_listencount);
            holder.tvLocation = (TextView) vi
                    .findViewById(R.id.list_row_tv_city);

            holder.pw_two = (ProgressWheel) vi
                    .findViewById(R.id.progressBarTwo);
            /************ Set holder with LayoutInflater ************/
            vi.setTag(holder);

        } else
            holder = (ViewHolder) vi.getTag();
        if (data.size() <= 0) {
            holder.tvListens.setText("No Data");

        } else {

            // ///////
            tempValues = null;
            tempValues = (ListModel) data.get(position);
            // Toast.makeText(activity, "in else", 100).show();
            /***** Get each Model object from Arraylist ********/

            /************ Set Model values in Holder elements ***********/
            holder.btnLike.setText(tempValues.getlikeCounter());
            holder.btnVoiceComment.setText(tempValues.getvoiceCommentCounter());
            holder.btnShare.setText(tempValues.getShareCounter());
            /******** sSet Item Click Listner for LayoutInflater for each row ***********/
            vi.setOnClickListener(new OnItemClickListener(position));

            holder.btnLike.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    // Toast.makeText(activity, "like clicked", 100).show();

                    View view = (Button) v;
                    ((Button) view).setText(Integer.parseInt(((Button) view)
                            .getText().toString()) + 1 + "");
                }
            });

            holder.btnVoiceComment.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    //
                    // Drawable d = holder.rlBg.getBackground();
                    // Bitmap bitmap = drawableToBitmap(d);
                    // ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    // bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                    // byte[] b = baos.toByteArray();

                }
            });

            holder.ibMore.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // Toast.makeText(activity, "more clicked", 100).show();
                    PopupMenu popup = new PopupMenu(activity, v);

                    /** Adding menu items to the popumenu */
                    popup.getMenuInflater().inflate(R.menu.main,
                            popup.getMenu());
                    popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            // TODO Auto-generated method stub
                            switch (item.getItemId()) {
                            case R.id.spam:

                                Toast.makeText(activity, "Spam clicked",
                                        Toast.LENGTH_SHORT).show();
                                break;

                            case R.id.blockuser:
                                Toast.makeText(activity, " Block user clicked",
                                        Toast.LENGTH_SHORT).show();
                                break;

                            case R.id.remove:
                                Toast.makeText(activity, "Remove clicked",
                                        Toast.LENGTH_SHORT).show();

                                // RecentFragment rf = new RecentFragment();
                                // rf.onItemClick(position);
                                break;

                            default:
                                break;
                            }

                            return false;
                        }
                    });
                    popup.show();
                }
            });

            holder.btnShare.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // Toast.makeText(activity, "share clicked", 100).show();

                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("text/plain");
                    intent.putExtra(android.content.Intent.EXTRA_TEXT,
                            "This message is shared by Nookster application.You clicked on item = "
                                    + position);
                    activity.startActivity(intent);
                }
            });

            holder.ibPlayAudio.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (isTimerRunning) {// stop button is on
                        timer.cancel();
                        holder.ibPlayAudio
                                .setImageResource(R.drawable.playpost);
                        holder.pw_two.setVisibility(View.GONE);
                        isTimerRunning = false;
                    } else {
                        timer = new CounterClass(10000, TIME_INTERVAL, holder);
                        timer.start();
                        // play button is on
                        holder.ibPlayAudio
                                .setImageResource(R.drawable.stoppost);
                        holder.pw_two.setVisibility(View.VISIBLE);
                        isTimerRunning = true;
                    }
                }
            });

            // ///////
        }
        return vi;
    }

    @Override
    public void onClick(View v) {
        Log.v("CustomAdapter", "=====Row button clicked");
    }
  • add Log message Log.i("convertview","new view created") in if (convertView == null) {} and check how many times log is getting printed? – Piyush Kukadiya Aug 25 '14 at 06:59
  • @Piyush at intial level 3 times and when i scrolled then 4th time it get printed –  Aug 25 '14 at 07:06
  • just for testing cut the whole code from if (convertView == null) {} and paste outside it then comment out if (convertView == null) {} else holder = (ViewHolder) vi.getTag(); part and run.Check what happens? – Piyush Kukadiya Aug 25 '14 at 07:12
  • ok let me check........ –  Aug 25 '14 at 07:14
  • heey brother i did as per your suggestion i comment out if else part and it works but now am encountring other problem when i am scrolling the list view customlist view get refresh i mean it starts from initial state –  Aug 25 '14 at 07:20
  • you want to say that on scrolling same rows are getting repeated and new rows are not coming? – Piyush Kukadiya Aug 25 '14 at 07:26
  • when i scrolled then i get same list view which i got initial i mean when onCreate get called and initial list view looks like i mean list view initialise again i hope you understand what i want to say –  Aug 25 '14 at 07:29
  • But you are getting all rows correctly right ? edit your previous comment to discuss now – Piyush Kukadiya Aug 25 '14 at 07:40
  • getting all rows correctly but it start new problem with my list view its weired :( getting issue in scrolling :( –  Aug 25 '14 at 07:45

1 Answers1

0

Change like this: Remove final ViewHolder holder; from getView()
then change this:

public static class ViewHolder {
    public int pos;
    public Button btnLike;
    public Button btnVoiceComment;
    public Button btnShare;
    public ImageButton ibMore;
    public ImageButton ibPlayAudio;
    public ImageView ivPostImage;

    public ProgressWheel pw_two;

    public TextView tvLocation, tvListens, tvMessage;
    void setPosition(int position){
       this.pos=position;
          }
     int getPosition(){
       return pos;
          }

}      

create newView method and remove code from if (convertView == null)

void newView(int position, View convertView, ViewGroup parent){
    vi = inflater.inflate(R.layout.list_row_new, null);
    final ViewHolder holder = new ViewHolder();
    /******** View Holder Object to contain tabitem.xml file elements ************/
   holder.setPosition(position);
    holder.btnLike = (Button) vi.findViewById(R.id.list_row_btn_like);
    holder.btnVoiceComment = (Button) vi
            .findViewById(R.id.list_row_btn_voicecomment);
    holder.ibMore = (ImageButton) vi
            .findViewById(R.id.list_row_imgbtn_more);
    holder.btnShare = (Button) vi
            .findViewById(R.id.list_row_imgbtn_share);

    holder.ivPostImage = (ImageView) vi
            .findViewById(R.id.list_row_iv_post);

    holder.ibPlayAudio = (ImageButton) vi
            .findViewById(R.id.list_row_ib_play);

    holder.tvMessage = (TextView) vi.findViewById(R.id.list_row_tv_msg);
    holder.tvListens = (TextView) vi
            .findViewById(R.id.list_row_tv_listencount);
    holder.tvLocation = (TextView) vi
            .findViewById(R.id.list_row_tv_city);

    holder.pw_two = (ProgressWheel) vi
            .findViewById(R.id.progressBarTwo);
    /************ Set holder with LayoutInflater ************/
    vi.setTag(holder);
}  

also change this carefully

if (convertView == null) {

    newView(position,convertview,parent);
}
else{
    if(holder.getPosition()!=position)
        newView(position,convertview,parent);
    else
    holder = (ViewHolder) vi.getTag();

}

everything else remains same.

Let me know if it works.

Piyush Kukadiya
  • 1,880
  • 16
  • 26
  • i tried but not working even it throws nullpointer in getView –  Aug 25 '14 at 08:34
  • i have one query you created newView method but if you see you called it from two place first one with its member and second one is empty but we created function with members so are you sure empty one is correct? –  Aug 25 '14 at 08:35
  • oops sorry i missed that.it is with three parameters.At which line you are getting null pointer? – Piyush Kukadiya Aug 25 '14 at 08:43
  • 1
    you are facing this problem because you are getting convertview!=null after some rows of listview. check this out http://stackoverflow.com/questions/18323803/getview-parameter-convertview-not-null-on-new-position-parameter – Piyush Kukadiya Aug 25 '14 at 08:49
  • 08-25 14:01:24.105: E/AndroidRuntime(11667): FATAL EXCEPTION: main 08-25 14:01:24.105: E/AndroidRuntime(11667): Process: com.android.nookster, PID: 11667 08-25 14:01:24.105: E/AndroidRuntime(11667): java.lang.NullPointerException 08-25 14:01:24.105: E/AndroidRuntime(11667): at com.android.nookster.customlistview.CustomAdapter.getView(CustomAdapter.java:191) –  Aug 25 '14 at 08:54
  • line at CustomAdapter.java:191 – Piyush Kukadiya Aug 25 '14 at 08:58