ListView with longClick to show and hide checkbox?
I read the code here (web link). And the code was slightly modified to become the following:
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 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 one more object that I have 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();
} }
Paste the list layout configured 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 check a checkbox, I have to put it under the XML (layout) . But my case: I want to make longClick available to show checkboxes . How do you do it?
If I just add this code below, of course it will set the ListView to the 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 do it once onLongClick is done, how do I show the Combobox? and vice versa....
source to share
One possible solution is to hide / show a checkbox based on a flag that toggles when an element receives a long click. Do something like this in your adapter 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;
}
});
source to share
Its a simple add to xml checkbox and remove visibility
android:visibility="gone"
And declare radobox in the FileArrayAdapter class as you would in the textbox and put
onItemLongClickListener();
In this check, if the checkbox is visible then make it disappear or, if it is invisible, make it visible.
if(cb.isVisible()){
cb.setVisibility(View.GONE);
}else{
cb.setVisibility(View.VISIBLE);
}
Here it is.
source to share