Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView with longClick for showing and hiding Checkbox?

I read along the code here (weblink). And the code has been modified a little bit to become like this:

FileArrayAdapter.java

public class FileArrayAdapter extends ArrayAdapter<Item> {

    private Context c;
    private int id;
    private List<Item> items;

    public FileArrayAdapter(Context context, int textViewResourceId,
            List<Item> objects) {
        super(context, textViewResourceId, objects);
        c = context;
        id = textViewResourceId;
        items = objects;
    }

    public Item getItem(int i) {
        return items.get(i);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) c
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(id, null);
        }

        /* create a new view of my layout and inflate it in the row */
        // convertView = ( RelativeLayout ) inflater.inflate( resource, null );

        final Item o = items.get(position);
        if (o != null) {
            TextView t1 = (TextView) v.findViewById(R.id.TextView01);
            setDefaultTextColor(t1);

            TextView t2 = (TextView) v.findViewById(R.id.TextView02);
            setDefaultTextColor(t2);

            TextView t3 = (TextView) v.findViewById(R.id.TextViewDate);
            setDefaultTextColor(t3);

            /* Take the ImageView from layout and set the city's image */
            ImageView imageCity = (ImageView) v.findViewById(R.id.fd_Icon1);
            String uri = "drawable/" + o.getImage();
            int imageResource = c.getResources().getIdentifier(uri, null,
                    c.getPackageName());
            Drawable image = c.getResources().getDrawable(imageResource);
            imageCity.setImageDrawable(image);

            if (t1 != null)
                t1.setText(o.getName());
            if (t2 != null)
                t2.setText(o.getData());
            if (t3 != null)
                t3.setText(o.getDate());
        }
        return v;
    }

    private void setDefaultTextColor(TextView tx) {
        tx.setTextColor(Color.parseColor("#f8f9fe"));
    }
}

And also another object I made so far:

Item.java

public class Item implements Comparable<Item>{
    private String name;
    private String data;
    private String date;
    private String path;
    private String image;

    public Item(String n,String d, String dt, String p, String img)
    {
            name = n;
            data = d;
            date = dt;
            path = p;
            image = img;           
    }
    public String getName()
    {
            return name;
    }
    public String getData()
    {
            return data;
    }
    public String getDate()
    {
            return date;
    }
    public String getPath()
    {
            return path;
    }
    public String getImage() {
            return image;
    }
    public int compareTo(Item o) {
            if(this.name != null)
                    return this.name.toLowerCase().compareTo(o.getName().toLowerCase());
            else
                    throw new IllegalArgumentException();
    } }

WIth the list layout a bit customized below: listupload_row.xml

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

    <ImageView
        android:contentDescription="@string/file_sharing_file_folder"
        android:id="@+id/fd_Icon1"
        android:layout_width="50dip"
        android:layout_height="50dip" >
    </ImageView>

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="5dip"
        android:layout_toRightOf="@+id/fd_Icon1"
        android:singleLine="true"
        android:text="@+id/TextView01"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView01"
        android:layout_marginLeft="10dip"
        android:layout_toRightOf="@+id/fd_Icon1"
        android:text="@+id/TextView02" >
    </TextView>

    <TextView
        android:id="@+id/TextViewDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/TextView01"
        android:layout_marginLeft="5dip"
        android:text="@+id/TextViewDate" >
    </TextView>

</RelativeLayout>

My question is:

I know If I want to put Checkbox then I should put it under the XML (layout). But my case is, I want to make the longClick available for showing the Checkboxes. And how to do that?

If I simply add this code below, of course it will set the ListView to have onLongClick event....

dList.setLongClickable(true);

dList.setOnItemLongClickListener(new OnItemLongClickListener() {

    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
            int pos, long id) {

        // show the checkbox of each lines

        return true;
    }
});

But to make it once onLongClick executed, how do I show the Combobox? and vice versa....

like image 345
gumuruh Avatar asked Aug 25 '14 17:08

gumuruh


2 Answers

Its simple add checkbox in xml and make visibility gone

android:visibility="gone"

And declare a radiobox in FileArrayAdapter class as you did with textbox and the put your

onItemLongClickListener();

In that check if checkbox is visible then make it disappear or if not visible the make it visible.

if(cb.isVisible()){
    cb.setVisibility(View.GONE);
}else{
    cb.setVisibility(View.VISIBLE);
}

Thats's it.

like image 34
Archit Jain Avatar answered Oct 26 '22 20:10

Archit Jain


One possible solution is to hide/show the checkbox based on a flag that is toggled when an item receives a long click. Do something like this in your adapter's getView method:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ...
    if(showCheckBoxes) {
        v.findViewById(R.id.checkbox).setVisible(View.VISIBLE);
    } else {
        v.findViewById(R.id.checkbox).setVisible(View.GONE);
    }
    ...
}

and then in your long click listener:

dList.setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
        showCheckBoxes = !showCheckBoxes;
        fileArrayAdapter.notifyDataSetChanged();
        return true;
    }
});
like image 196
Jeremy Dowdall Avatar answered Oct 26 '22 21:10

Jeremy Dowdall