2014-02-10 46 views
0

我创建了一个ListActivity并从SQLite读取数据以显示在ListView中。在SQLite数据库的listActivity中使用onClickListener时出现错误

然后,我需要点击列表,显示我的新布局数据,但我有一个问题,当我使用

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; 
} 
} 

感谢帮助:))

+0

什么是logcat的说? –

+0

因此,你得到了什么样的错误? –

回答

0

我猜你得到ClassCastException异常错误。当您试图将NoteEntry对象实例转换为Cursor实例时,您的适配器将被填充为NoteEntry对象实例。所以,相反的

(Cursor)l.getItemAtPosition(position); 

使用以下命令:

(NoteEntry)l.getItemAtPosition(position); 
+0

感谢您的帮助,但它的剂量工作 但是我发现真正的代码 – armst

相关问题