我沿着代码在这里(网络链接)。 http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php并且代码稍微修改了一下,变成了这样:
文件数组适配器.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"));
}
}
还有我迄今为止制作的另一个对象:
项目.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();
} }
下面对列表布局进行了一些自定义: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>
我的问题是:
I know 如果我想放置复选框,那么我应该将其放在 XML(布局)下。
但我的情况是,我想让 longClick 可用于显示复选框。
以及如何做到这一点?
如果我只是在下面添加此代码,当然它会将 ListView 设置为具有 onLongClick 事件......
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;
}
});
但要使其在执行 onLongClick 后,如何显示组合框?反之亦然......