souravlahoti
souravlahoti

Reputation: 736

Alternate CheckTextBox becoming visible on click

I am trying to make a specific (selected by user) row of a ListView checkbox to become visible. But the problem is,I have dynamically added 10 rows in my list view using class that extends ArrayAdapter and when I select the 1st row then along with my 1st row , 3rd , 5th and so on check box is becoming visible. Similar with 2nd, 4th and so on row. I just want that particular row (eg position 0) that I selected to show up the check box and rest shoulb be invisible.

public class LazyAdapter extends ArrayAdapter<RowItem> {

Context context;

public LazyAdapter(Context context, int resourceId, List<RowItem> items){
    super(context, resourceId, items);
    this.context = context;
}

public class ViewHolder{
    CheckedTextView checkbox;
    TextView title;
    TextView description;
    LinearLayout card;
}


public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder;
    RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null){
        convertView = mInflater.inflate(R.layout.list_row, null);
        holder = new ViewHolder();
        holder.card = (LinearLayout) convertView.findViewById(R.id.card);
        holder.checkbox = (CheckedTextView)convertView.findViewById(R.id.deliverychecktext);
        holder.title = (TextView)convertView.findViewById(R.id.title);
        holder.description = (TextView)convertView.findViewById(R.id.description);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder)convertView.getTag();

    //holder.image.setImageResource(rowItem.getImageId());
    holder.title.setText(rowItem.getTitle());
    holder.description.setText(rowItem.getDesc());

    Animation animation = AnimationUtils.loadAnimation(context, R.anim.card_animation);
    holder.card.startAnimation(animation);


    return convertView;
}
}

MainActivity class

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Intialize and set the Action Bar to Holo Blue
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33b5e5" )));

    ListView lv = (ListView) findViewById(R.id.myList);
     rowItems = new ArrayList<RowItem>();

        String[] titles = {"Address1","Address2","Address3","Address4","Address5","Address6","Address7","Address8"};
        String[] descriptions = {"First Address","Second Address","Third Address","Fourth Address","Fifth Address",
                "Sixth Address","Seventh Address","Eighth Address"};
        //Populate the List
        for (int i = 0; i < titles.length; i++) {
            RowItem item = new RowItem("Delivery Address :", descriptions[i]);
            rowItems.add(item);
        }

        // Set the adapter on the ListView
        LazyAdapter adapter = new LazyAdapter(getApplicationContext(), R.layout.list_row, rowItems);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                CheckedTextView check = (CheckedTextView) view.findViewById(R.id.deliverychecktext);                                
                if (check.getVisibility() == 4)
                check.setVisibility(1);
                else
                    check.setVisibility(4);
                Toast.makeText(getApplicationContext(), "h u clicked : "+position, 
                           Toast.LENGTH_LONG).show();

            } 


            });

list_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/card_no_border"
android:orientation="vertical"
android:padding="2dp" 
android:id="@+id/card">

<CheckedTextView
android:id="@+id/deliverychecktext"
 android:layout_width="fill_parent"
 android:paddingLeft="2dp"
 android:paddingRight="2dp"
 android:paddingTop="2dp"
 android:layout_height="wrap_content"
 android:checkMark="@drawable/ic_launcher"
 android:visibility="invisible"
/>

   <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:text="Dog Tag"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#040404"
    android:textStyle="bold"
    android:typeface="sans" />
</LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#e5e5e5">

<TextView android:layout_width="match_parent"
      android:layout_height="wrap_content" 
      android:text="Your Saved Address"
      android:textSize="20sp"
      android:padding="5dp"/>

<ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:divider="@android:color/transparent"
        android:dividerHeight="10dp"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="1dip"/>
</LinearLayout>

Upvotes: 0

Views: 93

Answers (1)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11622

First one RowItem as class varible to store the selected items.

private RowItem mSelectedItem = null;

ex

public class LazyAdapter extends ArrayAdapter<RowItem> {
Context context;
private RowItem mSelectedItem = null;

After that add the below listener to your LazyAdapter adapter class.

/**
 * On Click Listener for the view.
 */
protected class OnViewClickListener implements
        android.view.View.OnClickListener {

    private RowItem mItem;

    public OnViewClickListener(RowItem item) {
        mItem = item;
    }

    @Override
    public void onClick(View v) {
        mSelectedItem = mItem;

        notifyDataSetChanged();
    }
}

call this listener from your getView method in LazyAdapter before return view and add logic for the show and hid the view.

ex :

    if(mSelectedItem != null && mSelectedItem == rowItem) {
        holder.checkbox.setVisibility(View.VISIBLE);
    } else {
        holder.checkbox.setVisibility(View.INVISIBLE);
    }

    convertView.setOnClickListener(new OnViewClickListener(rowItem));

    return convertView;
}

And remove click lister in ur activity.

on your activity its working because while rendering the adapter view it will reuse the object, so it was happening like that. Now we are checking and updating every time while view rendering.

Upvotes: 1

Related Questions