1

Hi there,

I searched a lot and all the thread i viewed isn't solving my problem. I don't what's wrong with my code.
First of all, i am kinda new to android programming as i just recently move from web programming to android programming.

I have a listview and it's scrolable. I have few listview items and it's pulled from database. My problem is, each listview items have a button and i've setOnclickListener to the button inside getView. Now let's say there's 5 items and i tap on button for item number 1, the position is 0 and when i scroll to the end of the list, i can see the button for item number 5 is clicked. When i scroll till middle of the list, sometimes the button randomly clicked and i can see from my logcat, the particullar button's position is 0. How come?

Here is my getView' code

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

            View v = convertView;

            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.newsfeed_layout, parent, false);
            }

            final Button btnLike = (Button) v.findViewById(R.id.buttonLike);

            btnLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                        String buttonText = btnLike.getText().toString();

                        if(buttonText.equals("LIKE")){
                            Toast.makeText(postedItems.this, "Liked This item" + position, Toast.LENGTH_SHORT).show();
                            btnLike.setPadding(4,0,12,0);
                            btnLike.setText("UNLIKE");
                            btnLike.setTextColor(Color.parseColor("#ffffff"));
                            btnLike.setBackgroundResource(R.drawable.likedredbutton);
                        }else if(buttonText.equals("UNLIKE")){
                            Toast.makeText(postedItems.this, "Unlike This Item" + position, Toast.LENGTH_SHORT).show();
                            btnLike.setPadding(4,0,20,0);
                            btnLike.setText("LIKE");
                            btnLike.setTextColor(Color.parseColor("#737373"));
                            btnLike.setBackgroundResource(R.drawable.cornerstyledbutton);
                        }
                }
            });

            return v;
        }
2
Contributors
2
Replies
20
Views
3 Years
Discussion Span
Last Post by imchivaa
0

Hey, Thanks for the reply. An user able to like an item and the same user also able to unlike or dislike the item which they've liked. I solved the issue with a like flag to determine if the specific button is clicked or not and from there i am able to develop my code logic.

I know that i can manage it from xml but i want it to be done dynamically by codes. Thanks by the way. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.