先附上源码:
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.util.Log;
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.ListView;
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;
public static final String EXTRA_SCHEDULE_ID = “scheduleId”;
public static final String EXTRA_SCHEDULE_COURSE = “scheduleCourse”;
private OnClickListener btnClickHandler=new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnMonday:
queryScheduleByDay(0);
// notifyDataSetChanged();
break;
case R.id.btnTuesday:
queryScheduleByDay(8);
break;
case R.id.btnWednesday:
queryScheduleByDay(16);
break;
case R.id.btnThursday:
queryScheduleByDay(24);
break;
case R.id.btnFriday:
queryScheduleByDay(32);
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);
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;
}
@Override
protected void onDestroy() {
dbRead.close();
super.onDestroy();
}
public void queryScheduleByDay(int a){
adapter = new ScheduleAdapter(this);
setListAdapter(adapter);
db = new ScheduleDB(this);
dbRead = db.getReadableDatabase();
Cursor c = dbRead.rawQuery(“SELECT * FROM schedule Where _id>=? AND _id<=?”, new String[]{“”+a+””,””+(a+7)+””});
while(c.moveToNext()){
adapter.add(new ScheduleListCellData(c.getString(c.getColumnIndex(ScheduleDB.COLUMN_NAME_SCHEDULE_NAME)), c.getInt(c.getColumnIndex(ScheduleDB.COLUMN_NAME_ID))));
}
};
// 点击列表课程进行设置
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.i(“TEST”, “点击列表触发spinner”);
ScheduleListCellData data = adapter.getItem(position);
Log.i(“TEST”, “获取position”+position);
Intent i = new Intent(this, AtyEditSchedule.class);
i.putExtra(Schedule.EXTRA_SCHEDULE_ID, data.id);
Log.i(“TEST”, “插入数据”+data.id);
i.putExtra(Schedule.EXTRA_SCHEDULE_COURSE, data.course);
Log.i(“TEST”, “插入数据”+data.course);
startActivity(i);
Log.i(“TEST”, “开始activity”);
super.onListItemClick(l, v, position, id);
}
static class ScheduleAdapter extends BaseAdapter{
private Context context;
private List<ScheduleListCellData> list = new ArrayList<Schedule.ScheduleListCellData>();
public ScheduleAdapter(Context context){
this.context = context;
}
public void add(ScheduleListCellData data){
list.add(data);
}
@Override
public int getCount() {
Log.i(“TEST”,”getCount:”+list.size());
return list.size();
}
@Override
public ScheduleListCellData getItem(int position) {
Log.i(“TEST”, “getItem():”+getItem(position).toString());
return list.get(position);
}
@Override
public long getItemId(int position) {
Log.i(“TEST”, “getIemId()”+getItemId(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);
}
TextView tvNo = (TextView) convertView.findViewById(R.id.tvNo);
TextView tvCourse = (TextView) convertView.findViewById(R.id.tvCourse);
ScheduleListCellData data = list.get(position);
tvCourse.setText(data.course);
tvNo.setText(“”+(position+1));
return convertView;
}
}
static class ScheduleListCellData{
String course = “”;
int id = -1;
public ScheduleListCellData(String course, int id){
this.course = course;
this.id = id;
}
}
}
通过点击按钮来显示每天的课程 用的是listactivity 但是无法触发onlistitemclick 只要点击列表项就会报错
错误是stackoverflow 然后是无限出现getItemid的错误
上网查是说用Linearlayout 嵌套太多 我用了3层嵌套 一是主布局 二是界面里的按钮布局 三是List里面的内容布局
求大神指点下