我的目标是填充片段内的微调器(TASKS)。我用方法" public List getChildren()"创建了一个sqlite数据库。返回一个ArrayList。我发现的大多数资源和教程都可以帮助您使用在同一个类或xml文件中创建的数组填充微调器。我无法找到声明我可以在我的oncreate方法中使用的适配器的方法。
SQLDATABASE
package com.example.victor.kidsrewards;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Victor on 4/3/2016.
*/
public class ChildDataBase {
private SQLiteDatabase db;
private ChildSQLiteHelper childSQLiteHelper;
String _parentName = Globals._username;
public ChildDataBase(Context context)
{
childSQLiteHelper = new ChildSQLiteHelper(context);
db = childSQLiteHelper.getWritableDatabase();
}
public void Close()
{
db.close();
}
public void createChild(String childNameText, String nickNameText, String genderText, int ageText )
{
String query = "select * from child";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
ContentValues contentValues = new ContentValues();
contentValues.put("id", count);
contentValues.put("childname", childNameText);
contentValues.put("nickname",nickNameText);
contentValues.put("age", ageText);
contentValues.put("gender", genderText);
contentValues.put("parent", _parentName);
//insert
db.insert("child" , null, contentValues);
}
public List<ChildUser> getChildren()
{
List<ChildUser> childList = new ArrayList<ChildUser>();
//select columns
String[] tableColumns = new String[]{"id", "childname", "nickname"};
Cursor cursor = db.query("child" , tableColumns, "parent =? ",new String[]{_parentName},null, null, null);
cursor.moveToFirst();
while(!cursor.isAfterLast())
{
ChildUser child =new ChildUser();
//take values from database
child.setId(cursor.getInt(0));
child.setChildName(cursor.getString(1));
child.setNickName(cursor.getString(2));
//add to db
childList.add(child);
cursor.moveToNext();
}
return childList;
}
}
我想调用公共List getChildren()方法来帮助我在我的onCreateView中填充我的Spinner。
/**
* A simple {@link Fragment} subclass.
*/
public class Tasks extends ListFragment implements OnClickListener {
private EditText taskText;
private Button addNew;
private TaskDataBase tdb;
private EditText pointsText;
private Spinner spinner;
private ChildDataBase cdb;
private ChildSQLiteHelper childSQLiteHelper;
public Tasks() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ChildDataBase cdb = new ChildDataBase(getContext());
View v = inflater.inflate(R.layout.spinner_layout, container, false);
spinner = (Spinner)v.findViewById(R.id.child_spinner);
setListAdapter(new ChildListAdapter(getContext(), cdb.getChildren()));
ArrayList<ChildUser> adapter = (ArrayList<ChildUser>) cdb.getChildren();
spinner.setAdapter(adapter);
// return inflater.inflate(R.layout.fragment_tasks, container, false);//fragment_tasks
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
taskText = (EditText)getView().findViewById(R.id.newTaskText);
pointsText = (EditText)getView().findViewById(R.id.newPointsText);
// spinner = (Spinner)getView().findViewById(R.id.child_spinner);
//spinner.setAdapter((SpinnerAdapter) childList);
addNew = (Button) getView().findViewById(R.id.addNewTodoButton);
addNew.setOnClickListener(this);
tdb = new TaskDataBase(getContext());
//setListAdapter(new ListAdapter(getContext(), tdb.getTasks()));
}
/* @Override
protected void onListItemClick(ListView 1, View v, int position, long id){
Task task = (Task)getListAdapter().getItem(position);
tdb.deleteTask(task.getId());
setListAdapter(new ListAdapter(getContext(), tdb.getTasks()));
Toast.makeText(getActivity(),"Deleted!", Toast.LENGTH_LONG).show();
}*/
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onClick(View v) {
String addNewText = taskText.getText().toString();
String addPoints = pointsText.getText().toString();
//if add button was clicked do this
if(addNew.isPressed() && !addNewText.equals("") && !addPoints.equals("") ){
String taskTextValue = taskText.getText().toString();
taskText.setText("");
String taskpointsValue = pointsText.getText().toString();
int pointsInt = Integer.parseInt(taskpointsValue);
pointsText.setText("");
TaskDataBase tdb = new TaskDataBase(getContext());
String sampleChild = "sample child"; //place holder c
//add to db
tdb.createTask(taskTextValue, pointsInt, sampleChild);
Toast.makeText(getActivity(), "New Task Added!", Toast.LENGTH_LONG).show();
tdb.Close();
}
else{
Toast.makeText(getActivity(), "Task must not be blank!", Toast.LENGTH_LONG).show();
}
}
}
这是我的微调器布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.victor.kidsrewards.Tasks">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="TODO TEXT GOES_HERE">
</ListView>
</RelativeLayout>
我还创建了一个ChildListAdapter类
package com.example.victor.kidsrewards;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
/**
* Created by Victor on 4/3/2016.
*/
public class ChildListAdapter extends ArrayAdapter<ChildUser> {
private final Context context;
private final List<ChildUser> childList;
public ChildListAdapter(Context context, List<ChildUser> childList){
super(context, R.layout.fragment_child_list_layout, childList);//fragment_child_list
this.context = context;
this.childList= childList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.fragment_child_list_layout,parent,false);
Button childName = (Button)rowView.findViewById(R.id.childButton);
childName.setText(childList.get(position).getNickName());
/* TextView taskText = (TextView)rowView.findViewById(R.id.taskTextView);
taskText.setText(taskList.get(position).getText());
TextView pointsText = (TextView)rowView.findViewById(R.id.pointsTextView);
pointsText.setText(Integer.toString(taskList.get(position).getPoints())+ " Points");*/
return rowView;
}
}
这是我收到的错误
Error:(69, 16) error: no suitable method found for setAdapter(ArrayList<ChildUser>)
method Spinner.setAdapter(SpinnerAdapter) is not applicable
(actual argument ArrayList<ChildUser> cannot be converted to SpinnerAdapter by method invocation conversion)
method AbsSpinner.setAdapter(SpinnerAdapter) is not applicable
(actual argument ArrayList<ChildUser> cannot be converted to SpinnerAdapter by method invocation conversion)
method AdapterView.setAdapter(SpinnerAdapter) is not applicable
(actual argument ArrayList<ChildUser> cannot be converted to SpinnerAdapter by method invocation conversion)