引用 3 楼 jiaqiangm 的回复:
Quote: 引用 2 楼 tablecat123 的回复:
Quote: 引用 1 楼 XiaToxic 的回复:
adapter是没有add数据的方法的 只有数据集发生变化了去刷新
你应该把数据添加到data里面然后用adapter.notifyDataSetChanged去更新吧
个人见解
cursor存放从数据库拿出的东西 adapter存放的是listView的内容 那么cursor 和adapter 怎么结合在一起呢
cursor 和adapter没有任何关系。查询出来的。你只需要使用一个list之类的集合存放起来。通过adapter进行设置。当list内容改变时。用adapter.notifyDataSetChanged去更新。
就像分页一样。list.add之后。用adapter.notifyDataSetChanged去更新
按照你说的 加到list里面 想直接按钮触发刷新list 还是不行
贴上源码
package com.ui.learningassistant;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import android.app.ListActivity;
import android.app.DownloadManager.Query;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.ui.learningassistant.db.ScheduleDB;
public class Schedule extends ListActivity{
private TextView tvDate;
private Button btnMonday, btnTuesDay, btnWednesDay, btnThursday, btnFriday;
private Button btnManageCourse;
private ScheduleAdapter adapter;
private static ScheduleDB db;
private static SQLiteDatabase dbRead;
private OnClickListener btnClickHandler=new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnMonday:
adapter = new ScheduleAdapter();
setListAdapter(adapter);
adapter.queryScheduleByDay(1);
adapter.notifyDataSetChanged();
break;
case R.id.btnTuesday:
break;
case R.id.btnWednesday:
break;
case R.id.btnThursday:
break;
case R.id.btnFriday:
break;
case R.id.btnManageCourse:
Intent i = new Intent(Schedule.this, AtyCourse.class);
startActivity(i);
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aty_schedule);
// adapter = new ScheduleAdapter(this);
// db = new ScheduleDB(this);
// dbRead = db.getReadableDatabase();
// adapter = new SimpleCursorAdapter(this, R.layout.schedule_list_cell, null, new String[]{ScheduleDB.COLUMN_NAME_ID, ScheduleDB.COLUMN_NAME_SCHEDULE_NAME}, new int[]{R.id.tvNo, R.id.tvCourse});
tvDate = (TextView) findViewById(R.id.tvDate);
tvDate.setText(“”+StringData());
findViewById(R.id.btnMonday).setOnClickListener(btnClickHandler);
findViewById(R.id.btnTuesday).setOnClickListener(btnClickHandler);
findViewById(R.id.btnWednesday).setOnClickListener(btnClickHandler);
findViewById(R.id.btnThursday).setOnClickListener(btnClickHandler);
findViewById(R.id.btnFriday).setOnClickListener(btnClickHandler);
findViewById(R.id.btnManageCourse).setOnClickListener(btnClickHandler);
}
//—————-显示日期 星期——————————
public static String StringData(){
String mYear;
String mMonth;
String mDay;
String mWay;
final Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone(“GMT+8:00”));
mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
if(“1″.equals(mWay)){
mWay =”休息”;
}else if(“2″.equals(mWay)){
mWay =”一”;
}else if(“3″.equals(mWay)){
mWay =”二”;
}else if(“4″.equals(mWay)){
mWay =”三”;
}else if(“5″.equals(mWay)){
mWay =”四”;
}else if(“6″.equals(mWay)){
mWay =”五”;
}else if(“7″.equals(mWay)){
mWay =”休息”;
}
return mYear + “年” + mMonth + “月” + mDay+”日”+”/星期”+mWay;
}
// public void findScheduleByDay(int a){
// db = new ScheduleDB(this);
// db.getReadableDatabase();
// Cursor c = dbRead.query(ScheduleDB.TABLE_NAME_SCHEDULE, null, ScheduleDB.COLUMN_NAME_ID, null, null, null, null);
// c.moveToPosition(a);
// System.out.println(c+”第二步”);
// for (int i = 0; i < 7; i++) {
// c.moveToNext();
// adapter.
//
// }
// }
@Override
protected void onDestroy() {
dbRead.close();
super.onDestroy();
}
static class ScheduleAdapter extends BaseAdapter{
public ScheduleAdapter(Context context){
this.context = context;
}
public void queryScheduleByDay(int a){
db = new ScheduleDB(this.context);
dbRead = db.getReadableDatabase();
// Cursor c = dbRead.execSQL(“SELECT * FROM SCHEDULE WHERE _ID>=1 AND _ID<=7”);
Cursor c = dbRead.query(ScheduleDB.TABLE_NAME_SCHEDULE, null, null, null, null, null, null);
c.moveToPosition(a);
for (int i = 0; i < 7; i++) {
c.moveToNext();
add(new ScheduleListCellData(c.getString(c.getColumnIndex(ScheduleDB.COLUMN_NAME_SCHEDULE_NAME)), c.getInt(c.getColumnIndex(ScheduleDB.COLUMN_NAME_ID))));
}
};
public void add(ScheduleListCellData data){
list.add(data);
}
@Override
public int getCount() {
return list.size();
}
@Override
public ScheduleListCellData getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView = LayoutInflater.from(context).inflate(R.layout.schedule_list_cell, null);
}
ScheduleListCellData data = getItem(position);
TextView tvNo = (TextView) convertView.findViewById(R.id.tvNo);
TextView tvCourse = (TextView) convertView.findViewById(R.id.tvCourse);
tvNo.setText(data.id);
tvCourse.setText(data.course);
return convertView;
}
private Context context;
private List<ScheduleListCellData> list = new ArrayList<Schedule.ScheduleListCellData>();
}
static class ScheduleListCellData{
String course = “”;
int id = -1;
public ScheduleListCellData(String course, int id){
this.course = course;
this.id = id;
}
}
}
case btnMonday 里面 adapter 无法 new ScheduleAdapater()
求大神指教