我正在尝试从SQLLite数据库中捕获值并将其显示在列表视图中。
我完成了编码,但我的代码中没有发现错误,但在执行后,列表视图中没有显示任何内容。
任何人都可以帮我解决这个问题。
这是我的代码。
Dbhandler.java文件
package lk.adspace.testing;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class Dbhandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "jaffnatempletest";
// Temple table name
private static final String TABLE_TEMPLE = "templ";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TMPNAME = "temple_name";
private static final String KEY_TMPTYPE = "temple_type";
private static final String KEY_LATITUDE = "latitude";
private static final String KEY_LONGITUDE = "longitude";
private static final String KEY_IMGNAME = "image_name";
private static final String KEY_YEARBUILD = "year_build";
private static final String KEY_ADDRESS = "address";
private static final String KEY_CITY = "city";
private static final String KEY_EMAIL = "email";
private static final String KEY_WEB = "website";
private static final String KEY_TEL1 = "telephone1";
private static final String KEY_TEL2 = "telephone2";
private static final String KEY_DESCRI = "Description";
private final ArrayList<kovil> temple_list = new ArrayList<kovil>();
public Dbhandler (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TEMPLE_TABLE = "CREATE TABLE " + TABLE_TEMPLE + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + KEY_TMPNAME + " TEXT," + KEY_TMPTYPE + " TEXT," + KEY_LATITUDE + " TEXT," + KEY_LONGITUDE + " TEXT," + KEY_IMGNAME + " TEXT,"
+ KEY_YEARBUILD + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_CITY + " TEXT," + KEY_EMAIL + " TEXT," + KEY_WEB + " TEXT," + KEY_TEL1 + " TEXT," + KEY_TEL2 + " TEXT,"
+ KEY_DESCRI + " TEXT" + ")";
db.execSQL(CREATE_TEMPLE_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEMPLE);
// Create tables again
onCreate(db);
}
// Adding new temple
public void Add_Temple(kovil Kovil) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TMPNAME, Kovil.gettemplename());
values.put(KEY_TMPTYPE, Kovil.gettempletype());
values.put(KEY_LATITUDE, Kovil.getlatitude());
values.put(KEY_LONGITUDE, Kovil.getlongitude());
values.put(KEY_IMGNAME, Kovil.getimage_name());
values.put(KEY_YEARBUILD, Kovil.getyear_build());
values.put(KEY_ADDRESS, Kovil.getaddress());
values.put(KEY_CITY, Kovil.getcity());
values.put(KEY_EMAIL, Kovil.getemail());
values.put(KEY_WEB, Kovil.getwebsite());
values.put(KEY_TEL1, Kovil.gettelephone1());
values.put(KEY_TEL2, Kovil.gettelephone2());
values.put(KEY_DESCRI, Kovil.getDescription());
// Inserting Row
db.insert(TABLE_TEMPLE, null, values);
db.close(); // Closing database connection
}
// Getting single contact
kovil Get_Temple(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_TEMPLE, new String[] { KEY_ID,
KEY_TMPNAME, KEY_TMPTYPE, KEY_LATITUDE, KEY_LONGITUDE, KEY_IMGNAME, KEY_YEARBUILD, KEY_ADDRESS, KEY_CITY, KEY_EMAIL, KEY_EMAIL, KEY_WEB, KEY_TEL1, KEY_TEL2, KEY_DESCRI }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
kovil Kovil = new kovil(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13));
// return contact
cursor.close();
db.close();
return Kovil;
}
// Getting All Contacts
public ArrayList<kovil> Get_Temple() {
try {
temple_list.clear();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_TEMPLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
kovil Kovil = new kovil();
Kovil.setID(Integer.parseInt(cursor.getString(0)));
Kovil.settemplename(cursor.getString(1));
Kovil.settempletype(cursor.getString(2));
Kovil.setlatitude(cursor.getString(3));
Kovil.setlongitude(cursor.getString(4));
Kovil.setimage_name(cursor.getString(5));
Kovil.setyear_build(cursor.getString(6));
Kovil.setaddress(cursor.getString(7));
Kovil.setcity(cursor.getString(8));
Kovil.setemail(cursor.getString(9));
Kovil.setwebsite(cursor.getString(10));
Kovil.settelephone1(cursor.getString(11));
Kovil.settelephone2(cursor.getString(12));
Kovil.setDescription(cursor.getString(13));
// Adding contact to list
temple_list.add(Kovil);
} while (cursor.moveToNext());
}
// return contact list
cursor.close();
db.close();
return temple_list;
} catch (Exception e) {
// TODO: handle exception
Log.e("all_temples", "" + e);
}
return temple_list;
}
// Getting contacts Count
public int Get_Total_Temple() {
String countQuery = "SELECT * FROM " + TABLE_TEMPLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
db.close();
return count;
}
}
Kovil.java文件
package lk.adspace.testing;
public class kovil {
//public variables
public int _id;
public String _temple_name;
public String _temple_type;
public String _latitude;
public String _longitude;
public String _image_name;
public String _year_build;
public String _address;
public String _city;
public String _email;
public String _website;
public String _telephone1;
public String _telephone2;
public String _Description;
//empty constructor
public kovil (){
}
// int id, String temple_name, String temple_type, String latitude, String longitude, String image_name, String year_build, String address, String city, String email, String website, String telephone1, String telephone2, String Description
public kovil(int id, String temple_name, String temple_type, String latitude, String longitude, String image_name, String year_build, String address,
String city, String email, String website, String telephone1, String telephone2, String Description) {
// TODO Auto-generated constructor stub
this._id= id;
this._temple_name=temple_name;
this._temple_type=temple_type;
this._latitude=latitude;
this._longitude=longitude;
this._image_name=image_name;
this._year_build=year_build;
this._address=address;
this._city=city;
this._email=email;
this._website=website;
this._telephone1=telephone1;
this._telephone2=telephone2;
this._Description=Description;
}
public kovil(String temple_name, String temple_type, String latitude, String longitude, String image_name, String year_build, String address,
String city, String email, String website, String telephone1, String telephone2, String Description) {
// TODO Auto-generated constructor stub
this._temple_name=temple_name;
this._temple_type=temple_type;
this._latitude=latitude;
this._longitude=longitude;
this._image_name=image_name;
this._year_build=year_build;
this._address=address;
this._city=city;
this._email=email;
this._website=website;
this._telephone1=telephone1;
this._telephone2=telephone2;
this._Description=Description;
}
public int getID() {
return this._id;
}
public void setID(int id) {
this._id = id;
}
public String gettemplename() {
return this._temple_name;
}
public void settemplename(String temple_name) {
this._temple_name=temple_name;
}
public String gettempletype() {
return this._temple_type;
}
public void settempletype(String temple_type) {
this._temple_type=temple_type;
}
public String getlatitude() {
return this._latitude;
}
public void setlatitude(String latitude) {
this._latitude=latitude;
}
public String getlongitude() {
return this._longitude;
}
public void setlongitude(String longitude) {
this._longitude=longitude;
}
public String getimage_name() {
return this._image_name;
}
public void setimage_name(String image_name) {
this._image_name=image_name;
}
public String getyear_build() {
return this._year_build;
}
public void setyear_build(String year_build) {
this._year_build=year_build;
}
public String getaddress() {
return this._address;
}
public void setaddress(String address) {
this._address=address;
}
public String getcity() {
return this._city;
}
public void setcity(String city) {
this._city=city;
}
public String getemail() {
return this._email;
}
public void setemail(String email) {
this._email=email;
}
public String getwebsite() {
return this._website;
}
public void setwebsite(String website) {
this._website=website;
}
public String gettelephone1() {
return this._telephone1;
}
public void settelephone1(String telephone1) {
this._telephone1=telephone1;
}
public String gettelephone2() {
return this._telephone2;
}
public void settelephone2(String telephone2) {
this._telephone2=telephone2;
}
public String getDescription() {
return this._Description;
}
public void setDescription(String Description) {
this._Description=Description;
}
}
MainActivity.java文件 包lk.adspace.testing;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.ContactsContract.Data;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity<LayoutInfalter> extends Activity {
Temple_Adapter tAdapter;
ArrayList<kovil> temple_data = new ArrayList<kovil>();
ListView temple_listview;
Dbhandler dbhand = new Dbhandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
temple_listview = (ListView) findViewById(R.id.list);
kovil insertData = new kovil("temple_name", "temple_type", "latitude", "longitude", "image_name", "year_build", "address",
"city", "email", "website", "telephone1", "telephone2", "Description");
Dbhandler dbhand = new Dbhandler(this);
dbhand .Add_Temple(insertData );
kovil insertData2 = new kovil("temple_name", "temple_type", "latitude", "longitude", "image_name", "year_build", "address",
"city", "email", "website", "telephone1", "telephone2", "Description");
dbhand .Add_Temple(insertData2 );
int count =dbhand .Get_Total_Temple();
TextView textView = (TextView) findViewById(R.id.count);
textView.setText(Integer.toString(count));
// i want to display all the values in a list over here.
View_all_temples();
}
public void View_all_temples(){
temple_data.clear();
ArrayList<kovil> temple_array_from_db = dbhand.Get_Temple();
for (int i = 0; i < temple_array_from_db.size(); i++) {
int tidno = temple_array_from_db.get(i).getID();
String tempname = temple_array_from_db.get(i).gettemplename();
String city = temple_array_from_db.get(i).getcity();
String telphon = temple_array_from_db.get(i).gettelephone1();
kovil kov = new kovil();
kov.setID(tidno);
kov.settemplename(tempname);
kov.setcity(city);
kov.settelephone1(telphon);
temple_data.add(kov);
}
dbhand.close();
tAdapter = new Temple_Adapter(MainActivity.this, R.layout.activity_main,temple_data);
//tAdapter = new Temple_Adapter(MainActivity.this, R.layout.activity_main);
temple_listview.setAdapter(tAdapter);
tAdapter.notifyDataSetChanged();
}
public class Temple_Adapter extends ArrayAdapter<kovil> {
Activity activity;
int layoutResourceId;
kovil user;
LayoutInfalter mInfalter;
ArrayList<kovil> data = new ArrayList<kovil>();
ViewHolder holder;
public Temple_Adapter(Context act,
int layoutResourceId, ArrayList<kovil> data) {
super(act,layoutResourceId,data);
tAdapter = new Temple_Adapter(MainActivity.this, R.layout.activity_main,temple_data);
this.layoutResourceId = layoutResourceId;
this.activity = (Activity) act;
this.data = data;
}
@SuppressWarnings("unchecked")
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = null;
convertView = mInflater.inflate(R.layout.activity_main,parent, false);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder); // set tag on view
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText((CharSequence) data.get(position).temple_name);
return convertView;
}
public class ViewHolder
{
TextView tv;
}
}
@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;
}
}
主要活动.XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="TextView" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/count"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
完整错误日志..
02-02 13:44:26.000: E/AndroidRuntime(13097): FATAL EXCEPTION: main
02-02 13:44:26.000: E/AndroidRuntime(13097): java.lang.StackOverflowError
02-02 13:44:26.000: E/AndroidRuntime(13097): at java.util.ArrayList.<init>(ArrayList.java:81)
02-02 13:44:26.000: E/AndroidRuntime(13097): at android.database.Observable.<init>(Observable.java:34)
02-02 13:44:26.000: E/AndroidRuntime(13097): at android.database.DataSetObservable.<init>(DataSetObservable.java:24)
02-02 13:44:26.000: E/AndroidRuntime(13097): at android.widget.BaseAdapter.<init>(BaseAdapter.java:31)
02-02 13:44:26.000: E/AndroidRuntime(13097): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:152)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:110)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(MainActivity.java:111)
02-02 13:44:26.000: E/AndroidRuntime(13097): at lk.adspace.testing.MainActivity$Temple_Adapter.<init>(Ma
答案 0 :(得分:0)
更改为
public void Temple_Adapter_Adapter(Contextact, int layoutResourceId,
ArrayList<kovil> data) {
super(act,layoutResourceId,data);
this.layoutResourceId = layoutResourceId;
this.activity = act;
this.data = data;
}
你也忘了在这里传递arraylist
tAdapter = new Temple_Adapter(MainActivity.this, R.layout.activity_main,temple_data);
并删除
public Temple_Adapter(Context context,
int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
您还没有覆盖getView
。建议将适配器移动到单独的.java文件
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.layoutResourceId,
parent, false);
holder = new ViewHolder();
holder.tv1 = (TextView) convertView.findViewById(R.id.textView1)
convertView.setTag(holder); // set tag on view
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv1.setText(data.get(position).temple_name);
return convertView;
}
static class ViewHolder
{
TextView tv;
}
答案 1 :(得分:0)
我遇到了同样的问题,通过询问2个问题解决了问题。
此处回答了同一问题Getting a column from .sqlite containing multiple tables with multiple columns,然后是How to display particular data from ArrayList<> object to ListView?