Adi*_*ilp 1 java android android-listview android-fragments
我试图创建一个ToDo列表.我承认我还在学习如何使用片段,但我认为我的问题与片段的设计方式存在冲突.现在我有一个任务片段.我将在哪里添加任务,并在下面有一个列表.当我输入时,我点击添加按钮,它应该显示在列表中.我还有一个list_inner_view,其中有一个复选框.
现在在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>
Run Code Online (Sandbox Code Playgroud)
我的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;
}
}}
Run Code Online (Sandbox Code Playgroud)
Len*_*Bru 12
方法" addTaskNow"需要在片段添加到的活动内.如果希望片段处理单击,则需要将单击侦听器转发到片段,或者在片段内设置单击侦听器.onClick仅适用于活动,而不适用于片段.
我一直发现在布局文件中使用 android:onClick 属性会使代码难以导航。您的代码无法正常工作的原因可能是由于@Lena Bru 的回答(该方法需要在活动中)。
我建议您执行以下操作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// ...
Button b = (Button) rootView.findViewById(R.id.button1);
b.setOnClickListener(mButtonClickListener);
return rootView;
}
private OnClickListener mButtonClickListener = new OnClickListener() {
public void onClick(View v) {
addTaskNow();
}
}
Run Code Online (Sandbox Code Playgroud)
然后android:onClick从 fragment_tasks.xml 中删除该属性。
为了响应您之前的评论,您将拥有一个活动工具onClick,并在 xml 文件中指定指向onClick. 一般来说,我发现通常最好避免让片段/活动实现点击侦听器,因为当有多个按钮时会引起混乱。
此外,为了避免NullPointerExceptionin addTaskNow,您需要更改一些内容。您正在调用v.findViewById(R.id.editText1),它将返回null。这是因为findViewById只能返回包含在该视图中的视图。您传入的视图将addTaskNow是按钮,而不是包含R.id.editText1.
为了解决这个问题,最好的选择是让一个EditText在你的片段的成员变量,并初始化它onCreateView与R.id.editText1,就像你与你的按钮做。因此,您不再需要 aView作为 的参数addTaskNow。