我想以简单的方式访问列表视图中的数据库,但我的代码输出:
致命异常:java.lang.RuntimeException:无法恢复活动
我不知道如何解决这个问题。请帮忙。
package com.example.janmat;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewActivity extends Activity {
public SQLiteDatabase db;
public String name, add;
Cursor cursor;
SQLiteListAdapter ListAdapter;
ArrayList<String> ID_ArrayList = new ArrayList<String>();
ArrayList<String> NAME_ArrayList = new ArrayList<String>();
//private String DB_PATH = "/sdcard/Ringtones/";
//private static String DB_NAME = "bhushanDB.db";
//String myPath=DB_PATH + DB_NAME;
ListView LISTVIEW;
public class SQLiteListAdapter implements android.widget.ListAdapter{
Context context;
ArrayList<String> userID;
ArrayList<String> UserName;
public SQLiteListAdapter(
Context context2,
ArrayList<String> name,
ArrayList<String> add)
{
this.context = context2;
this.userID = name;
this.UserName = add;
}
public int getCount() {
// TODO Auto-generated method stub
return userID.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View child, ViewGroup parent) {
Holder holder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.listviewdatalayout, null);
holder = new Holder();
holder.textviewid = (TextView) child.findViewById(R.id.textViewID);
holder.textviewname = (TextView) child.findViewById(R.id.textViewNAME);
child.setTag(holder);
} else {
holder = (Holder) child.getTag();
}
holder.textviewid.setText(userID.get(position));
holder.textviewname.setText(UserName.get(position));
return child;
}
public class Holder {
TextView textviewid;
TextView textviewname;
}
@Override
public int getItemViewType(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Overrie
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void registerDataSetObserver(DataSetObserver arg0) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEnabled(int arg0) {
// TODO Auto-generated method stub
return false;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
LISTVIEW = (ListView) findViewById(R.id.listView1);
}
@Override
protected void onResume() {
ShowSQLiteDBdata() ;
super.onResume();
}
private void ShowSQLiteDBdata() {
db=openOrCreateDatabase("Hanuman", Context.MODE_PRIVATE, null);
cursor = db.rawQuery("SELECT * FROM persons", null);
ID_ArrayList.clear();
NAME_ArrayList.clear();
if (cursor.moveToFirst()) {
do {
ID_ArrayList.add(cursor.getString(cursor.getColumnIndex(name)));
NAME_ArrayList.add(cursor.getString(cursor.getColumnIndex(add)));
} while (cursor.moveToNext());
}
ListAdapter = new SQLiteListAdapter(ListViewActivity.this,
ID_ArrayList,
NAME_ArrayList);
LISTVIEW.setAdapter(ListAdapter);
cursor.close();
}
}