我有一个列表片段,其中每个项目都包含一个复选框,用户可以选中并取消选中它。列表片段中的每个项目对应于类Task中的对象,该对象存储有关是否选中该复选框的信息。
我的问题是,如何修改Task对象以响应复选框的选中和取消选中?我知道您通常会创建一个Task对象,然后通知适配器以便UI响应更改,但是如何反转该过程?
列表片段:
public class ListViewDemoFragment extends ListFragment {
private static List<Task> mItems; // ListView items list
private ListViewDemoAdapter mAdapter;
private IntentFilter intfil;
private DataBaseWrapper db;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mItems = new ArrayList<Task>();
intfil = new IntentFilter("CUSTOM");
db = new DataBaseWrapper(getActivity());
mItems = db.getAllTasks();
mAdapter = new ListViewDemoAdapter(getActivity(), mItems);
setListAdapter(mAdapter);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
mItems.remove(arg2);
mAdapter.notifyDataSetChanged();
return true;
}
});
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// remove the dividers from the ListView of the ListFragment
getListView().setDivider(null);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// retrieve theListView item
Task item = mItems.get(position);
Toast.makeText(getActivity(), item.title, Toast.LENGTH_SHORT).show();
}
private BroadcastReceiver b = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getStringExtra("name");
String dline = intent.getStringExtra("dline");
int pri = intent.getIntExtra("pri", 2);
//String des = intent.getStringExtra("des");
Task t = new Task(pri, name, dline, false);
mItems.add(t);
mAdapter.notifyDataSetChanged();
db.addTask(t);
}
};
public void onResume() {
super.onResume();
getActivity().registerReceiver(b, intfil);
}
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(b);
}
public void onPause() {
super.onPause();
db.close();
}
public List<Task> getlist() {
return mItems;
}
}
适配器:
public class ListViewDemoAdapter extends ArrayAdapter<Task> {
public ListViewDemoAdapter(Context context, List<Task> items) {
super(context, R.layout.listview_item, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listview_item, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
viewHolder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescription);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
Task item = getItem(position);
Drawable d = null;
switch (item.icon) {
case 0:
d = ContextCompat.getDrawable(getContext(), R.drawable.red);
break;
case 1:
d = ContextCompat.getDrawable(getContext(), R.drawable.yellow);
break;
case 2:
d = ContextCompat.getDrawable(getContext(), R.drawable.green);
break;
}
viewHolder.ivIcon.setImageDrawable(d);
viewHolder.tvTitle.setText(item.title);
viewHolder.tvDescription.setText(item.deadline);
viewHolder.checkbox.setChecked(item.check);
return convertView;
}
/**
* The view holder design pattern prevents using findViewById()
* repeatedly in the getView() method of the adapter.
*/
private static class ViewHolder {
ImageView ivIcon;
TextView tvTitle;
TextView tvDescription;
CheckBox checkbox;
}
}
任务类(列表中的项目)
public Task(int icon, String title, String deadline,boolean check) {
this.icon = icon;
this.title = title;
this.deadline = deadline;
this.check = check;
}
public String getName()
{return title;}
public int getIcon()
{return icon;}
public String getDeadline()
{return deadline;}
public boolean getCheck()
{return check;}
public void setCheck(Boolean b )
{
check = b;
}
}
主要活动
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View view) {
startActivityForResult(new Intent(MainActivity.this, TaskInput.class), 1);
}
public void save(View view)
{
DataBaseWrapper db = new DataBaseWrapper(this);
db.deleteAll();
ListViewDemoFragment f = (ListViewDemoFragment)getFragmentManager().findFragmentById(R.id.frag);
for (Task item: f.getlist())
{
db.addTask(item);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1)
{
sendBroadcast(data);
}
}
答案 0 :(得分:0)
尝试修改适配器,如下所示:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final Task item = getItem(position);
if (convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listview_item, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
viewHolder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescription);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
item.setCheck(isChecked);
}
});
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
Drawable d = null;
switch (item.icon) {
case 0:
d = ContextCompat.getDrawable(getContext(), R.drawable.red);
break;
case 1:
d = ContextCompat.getDrawable(getContext(), R.drawable.yellow);
break;
case 2:
d = ContextCompat.getDrawable(getContext(), R.drawable.green);
break;
}
viewHolder.ivIcon.setImageDrawable(d);
viewHolder.tvTitle.setText(item.title);
viewHolder.tvDescription.setText(item.deadline);
viewHolder.checkbox.setChecked(item.check);
return convertView;
}