0

i have referred to recycling mechanism here

I have a list of history of games played. the game can be 2 player,3 player,4 player I have this code for the getView function in the list adaptor where I am making text views invisible according to whether the game is played by 2 or 3 or 4 players

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    System.out.println("getview:" + position + " " + convertView);

    //getting the layout inflater
    LayoutInflater inflater = activity.getLayoutInflater();

    //viewHolder object which is the object used for list values
    ViewHolder holder;

    //if convert view is null then the list view must be populated
    if (convertView == null) {

        //inflate the layout of the listview into convertview
        convertView = inflater.inflate(R.layout.history_layout, null);

        //new holder object
        holder = new ViewHolder();

        //setting the name of the player 1 to textView name1
        holder.txtPlayer1 = (TextView) convertView.findViewById(R.id.name1);

        //setting the name of the player 2 to textView name2
        holder.txtPlayer2 = (TextView) convertView.findViewById(R.id.name2);

        //setting the name of the player 3 to textView name3
        holder.txtPlayer3 = (TextView) convertView.findViewById(R.id.name3);

        //setting the name of the player 4 to textView name4
        holder.txtPlayer4 = (TextView) convertView.findViewById(R.id.name4);

        //setting tag to the convertView
        convertView.setTag(holder);

    } else {
        //convertView to get the viewHolder object from its tag
        holder = (ViewHolder) convertView.getTag();

    }

    //getting the map of values from the list's specific position
    HashMap<String, String> map = list.get(position);

    //setting the text of the player 1 name textView
    holder.txtPlayer1.setText(map.get("PLAYER1"));

    //setting the text of the player 2 name textView
    holder.txtPlayer2.setText(map.get("PLAYER2"));

    //if there was a player 3 in the game then his name is also set in the text View, or else
    //the textview is made invisible
    if (map.get("PLAYER3") != "") {

        //setting player 3 name
        holder.txtPlayer3.setText(map.get("PLAYER3"));
    } else {

        //setting player 3 textView invisible
        holder.txtPlayer3.setVisibility(View.INVISIBLE);

    }

    //if there was a player 3 in the game then his name is also set in the text View, or else
    //the textview is made invisible
    if (map.get("PLAYER4") != "") {
        //setting player 3 name
        holder.txtPlayer4.setText(map.get("PLAYER4"));

    } else {

        //setting player 4 textView invisible
        holder.txtPlayer4.setVisibility(View.INVISIBLE);
    }

    //returning the convertView
    return convertView;

}

private static class ViewHolder {

    //Player 1 Name
    TextView txtPlayer1;

    //Player 2 Name
    TextView txtPlayer2;

    //Player 3 Name
    TextView txtPlayer3;

    //Player 4 Name
    TextView txtPlayer4;
}

these are my layouts

listview

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/menu_bg"
android:orientation="vertical"
    android:weightSum="6">

    <logic.main.com.boardgame.custom.CustomTextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="5"
    android:background="@drawable/score1"
        android:gravity="center"
    android:text="History"
    android:textColor="@color/fontcolor"
        android:textSize="45sp"></logic.main.com.boardgame.custom.CustomTextView>
<ListView
    android:id="@+id/history_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:divider="@android:color/black"
    android:dividerHeight="2dp"
    >


</ListView>
</LinearLayout>

list row

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <logic.main.com.boardgame.custom.CustomTextView
        android:id="@+id/name1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3.20"
        android:background="@drawable/winner2"
        android:gravity="center"
        android:text="Imran"
        android:textColor="@color/fontcolor"
        android:textSize="15sp"
        android:textStyle="bold" />
    <View
        android:layout_width="10dp"
        android:layout_height="48dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:background="#46495A" />

    <logic.main.com.boardgame.custom.CustomTextView
        android:id="@+id/name2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3.20"
        android:background="@drawable/score1"
        android:gravity="center"
        android:textColor="@color/fontcolor"
        android:textSize="15sp"
        android:textStyle="bold" />
    <View
        android:id="@+id/seprt2nd"
        android:layout_width="10dp"
        android:layout_height="48dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:background="#46495A" />

    <logic.main.com.boardgame.custom.CustomTextView
        android:id="@+id/name3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3.20"
        android:background="@drawable/score1"
        android:gravity="center"
        android:textColor="@color/fontcolor"
        android:textSize="15sp"
        android:textStyle="bold" />
    <View
        android:id="@+id/seprt3rd"
        android:layout_width="10dp"
        android:layout_height="48dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:background="#46495A" />

    <logic.main.com.boardgame.custom.CustomTextView
        android:id="@+id/name4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3.20"
        android:background="@drawable/score1"
        android:gravity="center"
        android:textColor="@color/fontcolor"
        android:textSize="15sp"
        android:textStyle="bold" />


</LinearLayout>

when I run this initially values are shown properly but when I scroll even the visible textviews are also made invisible why is that so?

Community
  • 1
  • 1
Rishabh
  • 3,752
  • 4
  • 47
  • 74

2 Answers2

3

Because you're never setting it back to visible from what I can see.

Change your code to this and it should work: (do the same for PLAYER4):

if (map.get("PLAYER3") != "") {

    //setting player 3 name
    holder.txtPlayer3.setVisibility(View.VISIBLE);
    holder.txtPlayer3.setText(map.get("PLAYER3"));
} else {

    //setting player 3 textView invisible
    holder.txtPlayer3.setVisibility(View.INVISIBLE);

}
kha
  • 19,123
  • 9
  • 34
  • 67
1

Its becuase of how you're checking the empty string... Change map.get("PLAYER3") != "" to !map.get("PLAYER3").isEmpty() or !map.get("PLAYER3").equals(""). You cannot check for equality on a string with a boolean operator.

if (!map.get("PLAYER3").isEmpty()) { 
    holder.txtPlayer3.setVisibility(View.VISIBLE);
    holder.txtPlayer3.setText(map.get("PLAYER3")); 
} else { 
    holder.txtPlayer3.setVisibility(View.INVISIBLE); 
}

Also a better and more readable way to approach the ViewHolder patter would be

private static class ViewHolder {

    //Player 1 Name
    TextView txtPlayer1;

    //Player 2 Name
    TextView txtPlayer2;

    //Player 3 Name
    TextView txtPlayer3;

    //Player 4 Name
    TextView txtPlayer4;

    public ViewHolder(View view) {
        txtPlayer1 = (TextView) convertView.findViewById(R.id.name1);
        txtPlayer2 = (TextView) convertView.findViewById(R.id.name2);
        txtPlayer3 = (TextView) convertView.findViewById(R.id.name3);
        txtPlayer4 = (TextView) convertView.findViewById(R.id.name4);
    }
}

Than in your getView method you can simply do this

if (convertView == null) {
    convertView = inflater.inflate(R.layout.history_layout, null);
    holder = new ViewHolder(convertView);
    convertView.setTag(holder);
} else {
    //convertView to get the viewHolder object from its tag
    holder = (ViewHolder) convertView.getTag();

}