Java 如何在Android中修复带有光标数据的列表数组
嗨,我用SQL在android上创建了一个应用程序。我在设置光标时遇到问题。我不知道我做错了什么,因为这是我第一个使用带游标的数组列表的程序。谢谢你的帮助Java 如何在Android中修复带有光标数据的列表数组,java,android,Java,Android,嗨,我用SQL在android上创建了一个应用程序。我在设置光标时遇到问题。我不知道我做错了什么,因为这是我第一个使用带游标的数组列表的程序。谢谢你的帮助 public class ThoughtManager { private Thought thought; private ThoughtDbHelper dbHelper; private Cursor cursor; private Context context; SQLiteDatabase db; private Conten
public class ThoughtManager {
private Thought thought;
private ThoughtDbHelper dbHelper;
private Cursor cursor;
private Context context;
SQLiteDatabase db;
private ContentValues contentValues;
private ArrayList<Thought> allThoughts;
int iTitle;
int iDetail;
int iTime;
int iDate;
int iReason;
int iComment;
private String[] columns=new String[]{ThoughtDbHelper.KEY,
ThoughtDbHelper.TITLE,
ThoughtDbHelper.DETAIL,
ThoughtDbHelper.TIME,
ThoughtDbHelper.DATE,
ThoughtDbHelper.REASON,
ThoughtDbHelper.COMMENT}; // Tablica wypisywanych elementow
public ThoughtManager(Context context)
{
this.context = context; // CO tu dokładnie robimy????
dbHelper=new ThoughtDbHelper(context);
contentValues=new ContentValues();
}
public ThoughtManager openForWrite() // Gdy chcemy zapisywac cos w bazie nowego
{
db=dbHelper.getWritableDatabase();
return this;
}
public ThoughtManager openForRead() // Gdy chcemy odczytywac dane z bazy
{
db=dbHelper.getReadableDatabase();
return this;
}
public void close()
{
dbHelper.close(); // Bezpieczne zamykanie aplikacji
}
public long createThought(Thought thought) // TWORZE NOWY ELEMENT W BAZIE DANYCH!!!
{
contentValues.put(ThoughtDbHelper.TITLE, thought.getTitle());
contentValues.put(ThoughtDbHelper.DETAIL, thought.getDetails());
contentValues.put(ThoughtDbHelper.TIME, thought.getTime());
contentValues.put(ThoughtDbHelper.DATE, thought.getDate());
contentValues.put(ThoughtDbHelper.REASON, thought.getReason());
contentValues.put(ThoughtDbHelper.COMMENT, thought.getComment());
return db.insert(ThoughtDbHelper.TABLE_NAME, null, contentValues);
}
public int updateThought(Thought thought) // Edytuje element W BAZIE DANYCH!!!
{
contentValues.put(ThoughtDbHelper.TITLE, thought.getTitle());
contentValues.put(ThoughtDbHelper.DETAIL, thought.getDetails());
contentValues.put(ThoughtDbHelper.TIME, thought.getTime());
contentValues.put(ThoughtDbHelper.DATE, thought.getDate());
contentValues.put(ThoughtDbHelper.REASON, thought.getReason());
contentValues.put(ThoughtDbHelper.COMMENT, thought.getComment());
return db.update(ThoughtDbHelper.TABLE_NAME, contentValues, ThoughtDbHelper.TITLE + "=" + thought.getTitle(), null);
}
public int deleteThought(Thought thought) // Usuwa element W BAZIE DANYCH!!!
{
return db.delete(ThoughtDbHelper.TABLE_NAME, ThoughtDbHelper.TITLE + "=" + thought.getTitle(), null);
}
public ArrayList<Thought> getAllThoughts() // Pobieramy wszystkie elementy do tablicy
{
//Cursor cursor=db.query(ThoughtDbHelper.TABLE_NAME, columns, null,null,null,null,null);
//cursor.moveToFirst();
cursor = db.query(ThoughtDbHelper.TABLE_NAME, new String[]{ThoughtDbHelper.KEY,
ThoughtDbHelper.TITLE,
ThoughtDbHelper.DETAIL,
ThoughtDbHelper.TIME,
ThoughtDbHelper.DATE,
ThoughtDbHelper.REASON,
ThoughtDbHelper.COMMENT}, (String)null, (String[])null, (String)null, (String)null, (String)null);
iTitle = cursor.getColumnIndex(ThoughtDbHelper.TITLE);
iDetail=cursor.getColumnIndex(ThoughtDbHelper.DETAIL);
iTime=cursor.getColumnIndex(ThoughtDbHelper.TIME);
iDate=cursor.getColumnIndex(ThoughtDbHelper.DATE);
iReason=cursor.getColumnIndex(ThoughtDbHelper.REASON);
iComment=cursor.getColumnIndex(ThoughtDbHelper.COMMENT);
// W petli wpisuje kolejno wszystkie dane do kolejnych tablic w zlaeznosic ile mamy elemetow
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()) // (zacznij od poczatku ; rózny(!) od ostatniego ; idz co jeden
{
thought=new Thought(cursor.getString(iTitle), // pobieram tytul
cursor.getString(iDetail), // poberam opis
cursor.getString(iReason), // pobierma czas etc...
cursor.getString(iComment),
cursor.getString(iDate),
cursor.getString(iTime));
allThoughts.add(thought);
}
return allThoughts;
}
public Thought getThought(Thought thought)
{
cursor=db.query(ThoughtDbHelper.TABLE_NAME, columns, null, null, null, null, null);
iTitle=cursor.getColumnIndex(ThoughtDbHelper.TITLE);
iDetail=cursor.getColumnIndex(ThoughtDbHelper.DETAIL);
iTime=cursor.getColumnIndex(ThoughtDbHelper.TIME);
iDate=cursor.getColumnIndex(ThoughtDbHelper.DATE);
iReason=cursor.getColumnIndex(ThoughtDbHelper.REASON);
iComment=cursor.getColumnIndex(ThoughtDbHelper.COMMENT);
if(cursor!=null) // Jezeli mamy jakies dane
{
cursor.moveToFirst(); // przerzuc kursor do poczatku, by odczytywac dane od 1
thought=new Thought(cursor.getString(iTitle), // pobieram tytul
cursor.getString(iDetail), // posberam opis
cursor.getString(iReason), // pobierma czas etc...
cursor.getString(iComment),
cursor.getString(iDate),
cursor.getString(iTime));
allThoughts.add(thought);
}
return thought;
}
查看想法:在那里我想查看我保存的想法
public class ViewThought extends ListActivity {
ThoughtManager manager;
ArrayList<Thought> allThoughts;
ArrayList<String> allTitles;
ListView listView;
Thought thought;
String titlePosition;
Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.view_thought);
manager=new ThoughtManager(this);
allTitles = new ArrayList<String>();
listView = getListView();
//listView.setBackgroundResource();
listView.setPadding(10, 20, 10, 15);
listView.setFooterDividersEnabled(true);
listView.setHeaderDividersEnabled(true);
d
allThoughts = manager.getAllThoughts();
// Pobieram wszystkie zadania/notatki
for (Thought thought : allThoughts) // od d ostatniego elemetnu
{
allTitles.add(thought.getTitle());
}
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1 ,allTitles));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
/* titlePosition = allTitles.get(position);
//thought=manager.getThought(titlePosition);
dialog=new Dialog(ViewThought.this);
dialog.setContentView(R.layout.view_details);
// 1 sposob
//dialog.setTitle("Thought Detail view");
//2 sposob
TextView tvTime,tvDetail,tvReason,tvComment;
tvComment=(TextView) dialog.findViewById(R.id.tvViewComment);
tvDetail=(TextView) dialog.findViewById(R.id.tvViewDetails);
tvReason=(TextView) dialog.findViewById(R.id.tvViewReason);
tvTime=(TextView) dialog.findViewById(R.id.tvViewTime);
dialog.setTitle(thought.getTitle());
tvComment.setText(thought.getComment());
tvDetail.setText(thought.getDetails());
tvReason.setText(thought.getReason());
tvTime.setText("Created on"+thought.getDate()+"At "+thought.getTime());
dialog.show();
*/
}
}
公共类视图扩展了ListActivity{
思想经理人;
所有思想;
arraylistalltitles;
列表视图列表视图;
思维;
字符串标题位置;
对话;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.view\u思想);
管理者=新思维管理者(本);
allTitles=新的ArrayList();
listView=getListView();
//listView.setBackgroundResource();
setPadding(10,20,10,15);
setFooterDividerEnabled(true);
setHeaderDivisEnabled(true);
D
AllThinks=manager.getAllThinks();
//Pobieram wszystkie zadania/notatki
(思想思想:所有思想)//od d ostatniego elemetnu
{
allTitles.add(think.getTitle());
}
setAdapter(新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,allTitles));
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
/*titlePosition=allTitles.get(位置);
//思想=管理者。思考(标题位置);
dialog=新建对话框(viewThink.this);
对话框.setContentView(R.layout.view\u详细信息);
//1 sposob
//setTitle(“思想细节视图”);
//2 sposob
text查看电视时间、电视详情、电视原因、电视评论;
TVComent=(TextView)dialog.findViewById(R.id.tvViewComment);
tvDetail=(TextView)dialog.findViewById(R.id.tvViewDetails);
tvReason=(TextView)dialog.findViewById(R.id.tvViewReason);
tvTime=(TextView)dialog.findViewById(R.id.TVTViewTime);
setTitle(think.getTitle());
tvecomment.setText(think.getComment());
tvDetail.setText(think.getDetails());
tvReason.setText(think.getReason());
setText(“在”+think.getTime()处的“+think.getDate()+”上创建);
dialog.show();
*/
}
}
查看完整的演练
您需要初始化数据库,如:
SQLiteDatabase db = mDbHelper.getReadableDatabase();
在你读到任何东西之前。怎么了?您的确切问题、期望是什么,您有什么..?我有一个错误:java.lang.NullPointerException.Where?发布你的logcat。调试程序告诉我,问题在于游标。如果可能的话,我也可以显示我的所有项目。你对getReadableDatabase()有什么看法?根据官方教程(按照上面的链接!),你需要一个,这将为你提供一个数据库。我添加了我的所有ThoughtManager代码,你可以再看一次吗。我将非常感激,因为它仍然不起作用。我会解决这个问题。现在我有另一个;/当我想把想法添加到数组列表中时,我有错误,你能看看我做错了什么吗?代码与我的第一个问题相同。
SQLiteDatabase db = mDbHelper.getReadableDatabase();