我正在创建一个Android应用程序,它从用户获取任务详细信息并将其插入数据库。当用户想要查看当天的任务列表时(某些任务将显示不是所有任务),他将转到" viewTasks.java"扩展listActivity并使用服装适配器显示任务列表的活动。
列表视图从数据库中返回List<TodaysTask>
的函数中获取其项。现在这个活动什么都没有!虽然我确信有任务插入数据库!定制列表不会出现在我身上!这有什么不对?
如果从数据库返回的列表不为空并且出现此消息,我还会添加Toast消息!你能帮我解决这个问题吗? TodaysTask类具有字符串名称,类别和int time_H,time_M。
这里将向用户显示days任务的ViewTasks.java
public class ViewTask extends Activity implements
OnItemClickListener, DialogInterface.OnClickListener {
private ListView todays_task_list;
private List<TodayTasks> todaysTaskArrayList;
private CustomAdapter customAdapter;
private int clickedPosition;
SchedulerHelper schedulerHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task_view);
schedulerHelper = new SchedulerHelper(this);
schedulerHelper.open();
todaysTaskArrayList = new ArrayList<TodayTasks>();
todaysTaskArrayList = schedulerHelper.getTodayTasks();
if (!todaysTaskArrayList.isEmpty())//has elements
Toast.makeText(getApplicationContext(), "not empty", Toast.LENGTH_SHORT)
.show();
todays_task_list = (ListView) findViewById(R.id.tasksList);
customAdapter = new CustomAdapter(this, R.layout.list_row,
todaysTaskArrayList);
todays_task_list.setAdapter(customAdapter);
todays_task_list.setOnItemClickListener(this);}
@Override
protected void onResume() {
super.onResume();
todaysTaskArrayList = new ArrayList<TodayTasks>();
todaysTaskArrayList = schedulerHelper.getTodayTasks();
customAdapter = new CustomAdapter(this, R.layout.list_row,
todaysTaskArrayList);
todays_task_list.setAdapter(customAdapter);
todays_task_list.setOnItemClickListener(this); }
@Override
protected void onStart() {
super.onStart();
todaysTaskArrayList = new ArrayList<TodayTasks>();
todaysTaskArrayList = schedulerHelper.getTodayTasks();
customAdapter = new CustomAdapter(this, R.layout.list_row,
todaysTaskArrayList);
todays_task_list.setAdapter(customAdapter);
todays_task_list.setOnItemClickListener(this);}
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {//do somethings
}}
task_view.xml
<ListView
android:id="@+id/tasksList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginTop="10dp"/>
这是定制的适配器代码段
public class CustomAdapter extends ArrayAdapter<TodayTasks> {
private Context context;
public CustomAdapter(Context context, int resource, List<TodayTasks> objects) {
super(context, resource, objects);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final int pos = position;
if (convertView == null)
view = LayoutInflater.from(getContext()).inflate(R.layout.list_row,
null);
// task name
TextView name = (TextView) convertView.findViewById(R.id.task_name);
// category name
TextView category = (TextView) convertView
.findViewById(R.id.task_category);
// proposed_time
TextView time = (TextView) convertView.findViewById(R.id.proposed_time);
TodayTasks todaysTask = getItem(position);
name.setText(todaysTask.name);
category.setText(todaysTask.category);
time.setText(String.format("%02d : %02d", todaysTask.time_H,
todaysTask.time_M));
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
((ViewTask) context).startOnClick(pos);
}
});
return view;
}}
List_row.xml
<TextView
android:id="@+id/task_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/assignment"/>
<TextView
android:id="@+id/task_category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/task_name"
android:text="@string/university" />
<TextView
android:id="@+id/proposed_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/title"
android:gravity="right"
android:layout_marginTop="10dp"
android:text="@string/_16_30"/>
以及返回List片段的databaseHelper中的函数
public List<TodayTasks> getTodayTasks() {
Cursor cursor = sqliteDatabase.query(TODAYTASKS_TABLE, null, null,
null, null, null, null);
ArrayList<TodayTasks> tasksInfo = new ArrayList<TodayTasks>();
if (cursor != null && cursor.moveToFirst()) {
do {
tasksInfo
.add(new TodayTasks(
cursor.getString(cursor
.getColumnIndex(COLUMN_NAME_TODAYTASKS_NAME)),
cursor.getString(cursor
.getColumnIndex(COLUMN_NAME_TODAYTASKS_CATEGORY)),
cursor.getInt(cursor
.getColumnIndex(COLUMN_NAME_TODAYTASKS_TIME_HOUR)),
cursor.getInt(cursor
.getColumnIndex(COLUMN_NAME_TODAYTASKS_TIME_MINUTE))));
} while (cursor.moveToNext());
}
if (!tasksInfo.isEmpty()) {
cursor.close();
return tasksInfo;
} else {
cursor.close();
return null;
}
}
答案 0 :(得分:1)
如果您的活动扩展了列表活动,那么它应该有一个列表视图,其ID为android.R.id.list
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
你可以用
查找ListView lv = (ListView) findViewById(android.R.id.list);
答案 1 :(得分:0)
使用此oncreate您的代码应该工作。如果没有发生其他错误,那么我们将看到。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//remove setcontentview()
schedulerHelper = new SchedulerHelper(this);
schedulerHelper.open();
todaysTaskArrayList = new ArrayList<TodayTasks>();
todaysTaskArrayList = schedulerHelper.getTodayTasks();
if (!todaysTaskArrayList.isEmpty())//has elements
Toast.makeText(getApplicationContext(), "not empty", Toast.LENGTH_SHORT)
.show();
todays_task_list = getListView();//write this.
customAdapter = new CustomAdapter(this, R.layout.list_row,
todaysTaskArrayList);
todays_task_list.setAdapter(customAdapter);
todays_task_list.setOnItemClickListener(this);}
@Override
protected void onResume() {
super.onResume();
todaysTaskArrayList = new ArrayList<TodayTasks>();
todaysTaskArrayList = schedulerHelper.getTodayTasks(); //this should be here
customAdapter = new CustomAdapter(this, R.layout.list_row,
todaysTaskArrayList);
todays_task_list.setAdapter(customAdapter);
todays_task_list.setOnItemClickListener(this);
//instead of here.
}
现在你的代码应该可以运行。
答案 2 :(得分:0)
错误可能在这里
if (convertView == null)
view = LayoutInflater.from(getContext()).inflate(R.layout.list_row,
null);
更改为
if (convertView == null)
view = LayoutInflater.from(getContext()).inflate(R.layout.list_row,
parent);
也可以在onCreate Method
中使用setListAdapter(customAdapter);
使用getView回调:
if(view == null){
//inflate
}
而不是if(convertView == null)也低于使用视图而不是convertView并返回视图