android 可检查列表视图
我在开发 Android 应用程序方面还是个新手。 我已经创建了自己的列表适配器,如下所示,我希望该列表可供检查。我需要做什么才能使列表的每一行都包含一个复选框?
public class listAvtivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
static final String[] title = new String[] {
"*New*Apple iPad Wi-Fi (16GB)", "7 Touch Tablet -2GB Google Android",
"Apple iPad Wi-Fi (16GB) Rarely Used ","Apple iPad Wi-Fi (16GB) AppleCase" };
static final String[] detail = new String[] {
"1h 37m Shipping: $10.00","1h 39m Shipping: Free","58m 6s Shipping:$10.00","59m 30s Shipping: $10.95" };
//private Integer[] imgid = {
// R.drawable.bsfimg,R.drawable.bsfimg4,R.drawable.bsfimg2,
// R.drawable.bsfimg5
//};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){
try {
rd = new RowData(i,title[i],detail[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.list,
R.id.title, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position,
long id) {
Toast.makeText(getApplicationContext(), "You have selected "
+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}
private class RowData {
protected int mId;
protected String mTitle;
protected String mDetail;
RowData(int id,String title,String detail){
mId=id;
mTitle = title;
mDetail=detail;
}
@Override
public String toString() {
return mId+" "+mTitle+" "+mDetail;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
TextView detail = null;
//ImageView i11=null;
RowData rowData= getItem(position);
if(null == convertView){
convertView = mInflater.inflate(R.layout.list, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
itle.setText(rowData.mTitle);
detail = holder.getdetail();
detail.setText(rowData.mDetail);
//i11=holder.getImage();
//i11.setImageResource(imgid[rowData.mId]);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private TextView detail = null;
//private ImageView i11=null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if(null == title){
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
public TextView getdetail() {
if(null == detail){
detail = (TextView) mRow.findViewById(R.id.detail);
}
return detail;
}
//public ImageView getImage() {
// if(null == i11){
// i11 = (ImageView) mRow.findViewById(R.id.img);
// }
//return i11;
//}
}
}
}
I am quite new in developing android applications.
I have created my own list adapter as show below and I want that list to be checkable. What do I have to do so that my list contains a checkbox for each row?
public class listAvtivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
static final String[] title = new String[] {
"*New*Apple iPad Wi-Fi (16GB)", "7 Touch Tablet -2GB Google Android",
"Apple iPad Wi-Fi (16GB) Rarely Used ","Apple iPad Wi-Fi (16GB) AppleCase" };
static final String[] detail = new String[] {
"1h 37m Shipping: $10.00","1h 39m Shipping: Free","58m 6s Shipping:$10.00","59m 30s Shipping: $10.95" };
//private Integer[] imgid = {
// R.drawable.bsfimg,R.drawable.bsfimg4,R.drawable.bsfimg2,
// R.drawable.bsfimg5
//};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){
try {
rd = new RowData(i,title[i],detail[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.list,
R.id.title, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position,
long id) {
Toast.makeText(getApplicationContext(), "You have selected "
+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}
private class RowData {
protected int mId;
protected String mTitle;
protected String mDetail;
RowData(int id,String title,String detail){
mId=id;
mTitle = title;
mDetail=detail;
}
@Override
public String toString() {
return mId+" "+mTitle+" "+mDetail;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
TextView detail = null;
//ImageView i11=null;
RowData rowData= getItem(position);
if(null == convertView){
convertView = mInflater.inflate(R.layout.list, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
itle.setText(rowData.mTitle);
detail = holder.getdetail();
detail.setText(rowData.mDetail);
//i11=holder.getImage();
//i11.setImageResource(imgid[rowData.mId]);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private TextView detail = null;
//private ImageView i11=null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if(null == title){
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
public TextView getdetail() {
if(null == detail){
detail = (TextView) mRow.findViewById(R.id.detail);
}
return detail;
}
//public ImageView getImage() {
// if(null == i11){
// i11 = (ImageView) mRow.findViewById(R.id.img);
// }
//return i11;
//}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我喜欢 Carlos Sessa 所著《50 Android hacks》中的解决方案。
我的activity_favorites.xml 看起来像这样:
我的FavoritesActivity.java 看起来像这样:
我的ListView 适配器(FavoritesPackageArrayAdapter.java)非常简单:
为了使列表项可检查,您的视图必须实现Checkable 接口。
我的 PackageView.java 看起来像:
最后每个列表项的 xml 布局 (favorites_package.xml):
如果您想在布局中有实际的复选框,它的 xml 应该看起来像:
如果您让复选框可单击,则只能选中它通过单击复选框本身。
也不要让你的布局可点击,由于某种原因它不能与 Checkable 界面一起使用。
I liked Carlos Sessa solution from his book "50 Android hacks".
my activity_favorites.xml looks like this:
My FavoritesActivity.java looks like this:
My ListView adapter (FavoritesPackageArrayAdapter.java) is quite simple:
In order to have list items checkable, your view has to implement Checkable interface.
My PackageView.java looks like:
And finally xml layout of each list item (favorites_package.xml):
If you wanted to have actual checkbox in your layout it's xml should look something like:
If you left checkbox clickable, you could only check it only by clicking on checkbox itself.
Also don't make your layout clickable, for some reason it doesn't work with Checkable interface.
查看该代码示例
http://www.androidpeople.com/android-listview- multiple-choice-example
--- 编辑 --
您没有发布您的 xml 文件。为了拥有自定义列表行,我这样做了。首先,我设置了内容视图,其中包含我的 java 代码中的列表视图。
然后,您的 list_main.xml 文件应包含一个 listview
AFterwards 设置数据适配器。
要获取自定义列表项,请创建一个包含自定义行的 list_item.xml 文件。您可以在此处放置线性布局、图像或复选框。
然后获取你的listview
你可以在这里设置onitemclick监听器
然后在你的自定义适配器文件中你可以实现getview函数
我无法查看你的代码并说出什么是错误的,但这是在android中实现listview自定义行的正确方法。
Look at that code sample
http://www.androidpeople.com/android-listview-multiple-choice-example
--- Edit --
You did not post your xml files. To have a custom list row I did it this way. First I setcontentview which contains my listview in your java code.
Then your list_main.xml file should contain a listview
AFterwards set data adapter
To get a custom list item, create a list_item.xml file which contains your custom row. You can put linearlayouts, images or checkboxes here.
Then get your listview
You can set onitemclick listener here
Then in your custom adapter file you can implement getview function
I could not look at your code and say what is wrong, but this is the correct way of implementing custom rows for listview in android.