Pravinsingh Waghela
Pravinsingh Waghela

Reputation: 2534

Hiding and showing view on view click in android BaseAdapter for listview issue

I am trying to show a hidden TextView on a text view click of the listitem of Listview of the BaseAdapter. The Textview of the listitem gets visible on click on some other textviw, but all the other hidden textview of the list are also visiable rather than a particular item of the listview. I want to make a particular list item view visible and not all textview of each listitem.

Below is my XML file for the listitem.

listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <TextView
            android:id="@+id/tvShowView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Textview to Click"          
            />
        <TextView
            android:id="@+id/tvToGetVisiable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This Textview will get visiable on tvShowView click"
            android:visibility="gone"
            />
</LinearLayout>

Below is my adapter class:-

RewardsAdapter.java

public class RewardsAdapter extends BaseAdapter {

    private List<Rewards> liRewards;
    private Context mContext;  

    public RewardsAdapter(Context context, List<Rewards> liRewards) {
        mContext = context;
        liRewards = liRewards;      
    }

    @Override
    public int getCount() {
        return (liRewards == null || liRewards.isEmpty()) ? 0 : liRewards.size();
    }

    @Override
    public Object getItem(int position) {
        return liRewards;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.listitem, null);
            viewHolder.tvShowView = (TextView) convertView.findViewById(R.id.tvShowView);
            viewHolder.tvToGetVisiable = (TextView)convertView.findViewById(R.id.tvToGetVisiable);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final Rewards rewards = liRewards.get(position);

        viewHolder.tvShowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewHolder.tvToGetVisiable.setVisibility(View.VISIBLE);
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private TextView tvShowView;
        private TextView tvToGetVisiable;
    }
}

Any help will be appreciated.

Upvotes: 0

Views: 152

Answers (2)

sushildlh
sushildlh

Reputation: 9056

Use this .....

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.listitem, null);
            viewHolder.tvShowView = (TextView) convertView.findViewById(R.id.tvShowView);
            viewHolder.tvToGetVisiable = (TextView)convertView.findViewById(R.id.tvToGetVisiable);

          //  convertView.setTag(viewHolder);

        final Rewards rewards = liRewards.get(position);

        viewHolder.tvShowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewHolder.tvToGetVisiable.setVisibility(View.VISIBLE);
            }
        });

        return convertView;
    }

Hope duplication problem will be solve ....

Upvotes: 1

Abu Yousuf
Abu Yousuf

Reputation: 6107

You have to save the the visibility state of text view. As you are saving ViewHolder and reusing existing ViewHolder. In getView method

// set a default visibility to View for position
// apply visibility to view 
// When visibility change for any event then change visibility and save it for position
// when getView is called for a position then use previously saved visibility for position

Upvotes: 0

Related Questions