我正在尝试创建一份待办事项清单。我承认我还在学习如何使用Fragment,但我认为我的问题是与Fragment的设计有冲突。现在我有一个任务的Fragment,在那里我将添加任务,并且下面有一个列表。当我键入时,我会点击添加按钮,然后它应该显示在列表上。我还有一个list_inner_view,其中有一个复选框。
现在,在XML中,我已经调用了android:onClick="addTaskNow"方法。
以下是我的代码:
我的fragment_tasks.xml
现在,在XML中,我已经调用了android:onClick="addTaskNow"方法。
以下是我的代码:
我的fragment_tasks.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentTasks" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:text="Add"
android:onClick="addTaskNow"/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1" >
</ListView>
</RelativeLayout>
我的FragmentTasks.java
package com.example.navbar;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*
*/
public class FragmentTasks extends Fragment {
public FragmentTasks() {
// Required empty public constructor
}
protected TaskerDbHelper db;
List<Task> list;
MyAdapter adapt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_tasks, container, false);
//return inflater.inflate(R.layout.fragment_tasks, container, false);
db = new TaskerDbHelper(getActivity());
list = db.getAllTasks();
adapt = new MyAdapter(getActivity(), R.layout.list_inner_view, list);
ListView listTask = (ListView) rootView.findViewById(R.id.list);
listTask.setAdapter((ListAdapter) adapt);
Button b = (Button) rootView.findViewById(R.id.button1);
return rootView;
}
public void addTaskNow(View v) {
EditText t = (EditText) v.findViewById(R.id.editText1);
String s = t.getText().toString();
if (s.equalsIgnoreCase("")) {
Toast.makeText(getActivity(), "enter the task description first!!",
Toast.LENGTH_LONG);
} else {
Task task = new Task(s, 0);
db.addTask(task);
Log.d("tasker", "data added");
t.setText("");
adapt.add(task);
adapt.notifyDataSetChanged();
}
}
private class MyAdapter extends ArrayAdapter<Task> {
Context context;
List<Task> taskList = new ArrayList<Task>();
int layoutResourceId;
public MyAdapter(Context context, int layoutResourceId,
List<Task> objects) {
super(context, layoutResourceId, objects);
this.layoutResourceId = layoutResourceId;
this.taskList = objects;
this.context = context;
}
/**
* This method will DEFINe what the view inside the list view will
* finally look like Here we are going to code that the checkbox state
* is the status of task and check box text is the task name
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckBox chk = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_inner_view,
parent, false);
chk = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(chk);
chk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Task changeTask = (Task) cb.getTag();
changeTask.setStatus(cb.isChecked() == true ? 1 : 0);
db.updateTask(changeTask);
/*Toast.makeText(
getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG)
.show();
} */
}});
} else {
chk = (CheckBox) convertView.getTag();
}
Task current = taskList.get(position);
chk.setText(current.getTaskName());
chk.setChecked(current.getStatus() == 1 ? true : false);
chk.setTag(current);
Log.d("listener", String.valueOf(current.getId()));
return convertView;
}
}}