我正在使用listAdapter来尝试填充listView。但我有一个问题,它导致我的应用程序崩溃。我最初想要将列表填充到xml对象中,但我相信listView对我的应用程序会更好......这是我的适配器
package com.example.victor.kidsrewards;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.view.LayoutInflater;
import android.widget.TextView;
import java.util.List;
/**
* Created by Victor on 3/20/2016.
*/
public class ListAdapter extends ArrayAdapter<Task> {
private final Context context;
private final List<Task> taskList;
public ListAdapter(Context context, List<Task> taskList){
super(context, R.layout.fragment_tasks, taskList);
this.context = context;
this.taskList= taskList;
}
@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_tasks,parent,false);
/*TextView taskText = (TextView)rowView.findViewById(R.id.newTaskText);
taskText.setText(taskList.get(position).getText());
TextView pointsText = (TextView)rowView.findViewById(R.id.newPointsText);
pointsText.setText(taskList.get(position).getText());*/
return rowView;
}
}
**
** 这是我的xml代码
<!-- TODO: Update blank fragment layout -->
<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>
</FrameLayout>
**
**
这是我调用listadapter的片段
package com.example.victor.kidsrewards;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class TaskList extends ListFragment {
private TaskDataBase tdb;
public TaskList() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_task_list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
tdb = new TaskDataBase(getContext());
setListAdapter(new ListAdapter(getContext(), tdb.getTasks()));
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
}
答案 0 :(得分:0)
您包含的布局文件的名称是什么? 如果是fragment_tasks.xml,那么fragment_tasks.xml不应该有ListView。它至少应该具有id为newTaskText的textView。 定义包含listView的内容布局。然后每行都有一个布局。这应该只包含TextView(以及与单行相关的其他视图),但不包含ListView。