Android,当用户设置复选框时如何重绘列表?
我有一个列表视图,每行包含两个文本视图和一个复选框。 我需要用户仅设置其中一个复选框,如果用户设置其他复选框,则应清除前一个复选框并可以设置第二个复选框。
我可以生成列表,但我不知道为什么 onListItemClick()
方法不起作用?因此,我不知道设置了哪个复选框。
我的下一个问题是,用户单击复选框后如何重绘列表?
我的代码是:
package com.Infindo.DRM;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
public class Infindo_PackageActivity extends ListActivity {
private final String[] titels = {"Try before buy", "Pay per play", "Pay per day", "Pay per week", "Pay per month", "Daily subscription", "Weekly subscription", "Monthly subscription"};
private final String[] descriptions = {"Free of charge", "Price: $1.00", "Price: $5.00", "Price: $10.00", "Price: $30.00", "Price: $5.00", "Price: $10.00", "Price: $30.00"};
private String[] flag = {"false", "false", "false", "false", "false", "false", "false", "false"};
private ListAdapter listAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.infindo_packagepage);
listAdapter = new ListAdapter(this);
setListAdapter(listAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("List Clicked:", "******");
}
public void onCheckboxClicked(View v){
int i = getSelectedItemPosition();
Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
}
//*********************
// RowModel Class
//*********************
private class RowModel{
TextView title;
TextView description;
CheckBox checkbox;
}
//*********************
// ListAdapter Class
//*********************
private class ListAdapter extends ArrayAdapter<String>{
public ListAdapter(Context c) {
super(c, R.layout.infindo_listformat, titels);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RowModel holder;
View row = convertView;
if(row == null){
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.infindo_listformat, parent, false);
holder = new RowModel();
holder.title = (TextView) row.findViewById(R.id.title);
holder.description = (TextView) row.findViewById(R.id.description);
holder.checkbox = (CheckBox) row.findViewById(R.id.checkbox);
row.setTag(holder);
} else {
holder = (RowModel) row.getTag();
}
holder.title.setText(titels[position]);
holder.description.setText(descriptions[position]);
String s = flag[position];
if(s.equalsIgnoreCase("false"))
holder.checkbox.setChecked(false);
else
holder.checkbox.setChecked(true);
return(row);
}
}
}
更新: 我已经添加
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
Log.i("List Redrawed:", "**notifyDataSetChanged()**");
}
到我的 List Adapter 类中并调用它,
public void onCheckboxClicked(View v){
int i = getSelectedItemPosition();
Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
listAdapter.notifyDataSetChanged();
}
我认为重绘的问题已经解决了。但我仍然不知道用户单击哪个复选框来更改标志数组。
I have a list view and each row contains two textviews and a checkbox.
I need user sets just one of checkboxes and if user sets other checkbox, previous checkbox should be cleared and second checkbox can be set.
I can generate the list but i don't know why onListItemClick()
method does not work? therefore, I don't know which checkbox is set.
and my next question is, how can i redraw list after clicking checkbox by user?
my code is:
package com.Infindo.DRM;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
public class Infindo_PackageActivity extends ListActivity {
private final String[] titels = {"Try before buy", "Pay per play", "Pay per day", "Pay per week", "Pay per month", "Daily subscription", "Weekly subscription", "Monthly subscription"};
private final String[] descriptions = {"Free of charge", "Price: $1.00", "Price: $5.00", "Price: $10.00", "Price: $30.00", "Price: $5.00", "Price: $10.00", "Price: $30.00"};
private String[] flag = {"false", "false", "false", "false", "false", "false", "false", "false"};
private ListAdapter listAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.infindo_packagepage);
listAdapter = new ListAdapter(this);
setListAdapter(listAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("List Clicked:", "******");
}
public void onCheckboxClicked(View v){
int i = getSelectedItemPosition();
Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
}
//*********************
// RowModel Class
//*********************
private class RowModel{
TextView title;
TextView description;
CheckBox checkbox;
}
//*********************
// ListAdapter Class
//*********************
private class ListAdapter extends ArrayAdapter<String>{
public ListAdapter(Context c) {
super(c, R.layout.infindo_listformat, titels);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RowModel holder;
View row = convertView;
if(row == null){
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.infindo_listformat, parent, false);
holder = new RowModel();
holder.title = (TextView) row.findViewById(R.id.title);
holder.description = (TextView) row.findViewById(R.id.description);
holder.checkbox = (CheckBox) row.findViewById(R.id.checkbox);
row.setTag(holder);
} else {
holder = (RowModel) row.getTag();
}
holder.title.setText(titels[position]);
holder.description.setText(descriptions[position]);
String s = flag[position];
if(s.equalsIgnoreCase("false"))
holder.checkbox.setChecked(false);
else
holder.checkbox.setChecked(true);
return(row);
}
}
}
Update:
I have added
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
Log.i("List Redrawed:", "**notifyDataSetChanged()**");
}
into my List Adapter class and called it in
public void onCheckboxClicked(View v){
int i = getSelectedItemPosition();
Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
listAdapter.notifyDataSetChanged();
}
I think the problem of redraw is solved. but I still I don't know what checkbox is clicked by the user in order to change flag array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 setOnCheckedChangeListener 来了解选中了哪个复选框。您可以在此处查看带有 CheckBox 的 ListView 的完整示例。
更新
要使您的
onListItemClick()
工作,您需要为 ListView 的其他项目编写android:focusable="false"
,这是因为其他视图 ListView 的onListItemClick()
的焦点未按其应有的方式执行。看看这个答案,为什么Android自定义ListView无法点击项目
You can use
setOnCheckedChangeListener
to know which checkbox was selected. You can look at complete example of ListView with CheckBox here.UPDATE
To make your
onListItemClick()
work you need to writeandroid:focusable="false"
for other items of the ListView, its because of the focus of other views ListView'sonListItemClick()
is not peforming as it should be performing.Checkout this answer, why Android custom ListView unable to click on items
您可能需要处理来自复选框本身的单击,而不仅仅是列表视图。
You probably need to handle the click from the checkbox itself and not just the listview.