来自SQLite DB的listActivity中使用onClickListener时出错

时间:2014-02-10 20:24:36

标签: java android sqlite android-listview

我创建了一个ListActivity并从SQLite读取数据以在ListView中显示。

然后,我需要点击列表并以新的布局显示我的数据,但是当我使用

时我遇到了问题

onClickListener所以这是我的代码

MainActivity.java

import com.hci.Entry.NoteEntry;
import com.hci.data.Constants;
import com.hci.data.ReminderDB;

import java.util.ArrayList;

public class MainActivity extends ListActivity {
    private ReminderDB dba;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        dba = new ReminderDB(this);
        dba.open();
        setContentView(R.layout.reminder_main);
        super.onCreate(savedInstanceState);
        this.setListAdapter(new NoteAdapter(this));

    }
}

private class NoteAdapter extends BaseAdapter{
    private LayoutInflater mInflater;
    private ArrayList<NoteEntry> notes;
    public NoteAdapter(Context context){
        mInflater = LayoutInflater.from(context);
        notes = new ArrayList<NoteEntry>();
        getData();
    }
    public void getData(){
        Cursor c = dba.getNote();
        startManagingCursor(c);

        if(c.moveToFirst()){
            do {
                String title = c.getString(c.getColumnIndex(Constants.TITLE_NAME));
                String subject = c.getString(c.getColumnIndex(Constants.SUBJECT_NAME));
                String content = c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
                NoteEntry temp = new NoteEntry(title,content,subject);
                notes.add(temp);
            }while(c.moveToNext());
        }

    }
    private Holder holder;
    @Override
    public int getCount() {
        return notes.size();
    }

    @Override
    public NoteEntry getItem(int i) {
        return notes.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        if(view == null){
            view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.note_row,null);
            holder = new Holder();
            holder.subject = (TextView) view.findViewById(R.id.subject_name);
            holder.title = (TextView) view.findViewById(R.id.Note_content);
            view.setTag(holder);
        }else{
            holder = (Holder) view.getTag();
        }
        holder.mNote = getItem(position);
        holder.title.setText(holder.mNote.getTitle());
        holder.subject.setText(holder.mNote.getSubject());

        view.setTag(holder);
        return view;

    }


    private class Holder {
        NoteEntry mNote;
        public TextView subject;
        public TextView title;
     }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.create_button:
            newActivities();
            return true;
        case R.id.action_settings:
            openSetting();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
private void newActivities() {
    Intent newActivities = new Intent(getApplicationContext(),NewActivities.class);
    startActivity(newActivities);

}

private void openSetting() {
    // TODO Auto-generated method stub

}
@Override
public void onBackPressed(){
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
   //now I have a problem 
   Cursor selectNote  = (Cursor) l.getItemAtPosition(position);
    String title =      selectNote.getString(selectNote.getColumnIndex(Constants.TITLE_NAME));
    String subject = selectNote.getString(selectNote.getColumnIndex(Constants.SUBJECT_NAME));
    String content = selectNote.getString(selectNote.getColumnIndex(Constants.CONTENT_NAME));

    Log.d("Title = ",title);
    Log.d("Subject = ",subject);
    Log.d("Content = ",content);
  }
 }

NoteDBHelper.java

public class NoteDBHelper extends SQLiteOpenHelper {
private static final String CREATE_TABLE = "create table "+Constants.TABLE_NAME+"("+
        Constants.NOTE_ID+" integer primary key autoincrement, "+
        Constants.TITLE_NAME+" text not null, "+
        Constants.SUBJECT_NAME+" text not null, "+
        Constants.CONTENT_NAME+" text not null);";
public NoteDBHelper(Context context) {
    super(context, Constants.TABLE_NAME, null, Constants.DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w("TaskDBAdapter","Upgrading from version"+oldVersion+"to"+newVersion+",which will destroy all old data");
    db.execSQL("drop table if exists"+Constants.TABLE_NAME);
    onCreate(db);

  }
}

ReminderDB.java

public class ReminderDB {
private SQLiteDatabase db;
private final Context context;
private final NoteDBHelper dbhelper;
private String[] allcolumns = {Constants.SUBJECT_NAME,Constants.TITLE_NAME,Constants.CONTENT_NAME};

public ReminderDB(Context c){
    context = c;
    dbhelper = new NoteDBHelper(context);
    db = dbhelper.getWritableDatabase();
}
public void close(){
    db.close();
}
public void open() throws SQLiteException {
        db = dbhelper.getWritableDatabase();
}
public long insertNotes(String title,String content,String S_name){
        ContentValues newTaskValue = new ContentValues();
        newTaskValue.put(Constants.TITLE_NAME, title);
        newTaskValue.put(Constants.CONTENT_NAME, content);
        newTaskValue.put(Constants.SUBJECT_NAME, S_name);

        return db.insert(Constants.TABLE_NAME, null,newTaskValue );
}
public Cursor getNote(){

    Cursor cursor =  db.query(Constants.TABLE_NAME,allcolumns,null,null,null,null,null);

    return cursor;
  }
}

NoteEntry.java

public class NoteEntry {
    int id;
    String title, subject, content;
    public NoteEntry(String t,String c,String s){
        title = t;subject = s;content = c;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

感谢您的帮助:))

1 个答案:

答案 0 :(得分:0)

我猜你得到了ClassCastException错误。在尝试将其转换为Cursor实例时,将使用NoteEntry对象实例填充适配器。所以,而不是

(Cursor)l.getItemAtPosition(position);

使用以下内容:

(NoteEntry)l.getItemAtPosition(position);