I have a project that I want to populate a listview with an SqlDatabase in android studio but when I run the app it carshes. please I need help with this project.
Adapter Class
package com.rawa.poplistview;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.rawa.poplistview.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by RRR on 3/8/2016.
*/
public class Adapter extends ArrayAdapter<Item> {
private Activity activity;
int id;
ArrayList<Item> items;
public Adapter(Activity context, int resource, ArrayList<Item> objects) {
super(context, resource, objects);
this.activity = context;
this.id = resource;
this.items = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null){
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(id,null);
}
Item item =items.get(position);
TextView tv_id = (TextView) convertView.findViewById(R.id.tv_id);
TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title);
TextView tv_content = (TextView) convertView.findViewById(R.id.tv_content);
tv_id.setText(item.getId());
tv_title.setText(item.getTitle());
tv_content.setText(item.getContent());
return convertView;
}
}
Database Helper
package com.rawa.poplistview;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.nfc.Tag;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_Path = "data/data/com.rawa.poplistview/";
private static String DB_Name = "my_database.sqlite";
private SQLiteDatabase myDatabase;
private final Context myContext;
public DatabaseHelper(Context context) {
super(context, DB_Name, null, 1);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private boolean checkDatabase(){
SQLiteDatabase checkDB=null;
try {
String myPath = DB_Path + DB_Name;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLException e) {
} if (checkDB != null) checkDB.close();
return checkDB != null ? true : false;
}
private void copyDatabase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_Name);
String outFileName = DB_Path+DB_Name;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length= myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myOutput.close();
}
public void openDatabase() throws SQLException {
String myPath = DB_Path + DB_Name;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public void ExeSQLData(String sql) throws SQLException {
myDatabase.execSQL(sql);
}
public Cursor QueryData(String query) throws SQLException {
return myDatabase.rawQuery(query,null);
}
@Override
public synchronized void close(){
if (myDatabase!=null)
myDatabase.close();
super.close();
}
public void checkAndCopyDatabase() {
boolean dbExist = checkDatabase();
if(dbExist){
Log.d("Tag","Database already Exist");
} else {
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e){
Log.d("Tag","Error Copying Database");
}
}
}
}
Item Class
package com.rawa.poplistview;
/**
* Created by RRR on 3/8/2016.
*/
public class Item {
private String id;
private String title;
private String content;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
MainActivity Class
package com.rawa.poplistview;
import android.database.Cursor;
import android.database.SQLException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listViewVar;
private DatabaseHelper dbHelper;
ArrayList<Item> arrayList = new ArrayList<Item>();
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper =new DatabaseHelper(this);
try {
dbHelper.checkAndCopyDatabase();
dbHelper.openDatabase();
} catch (SQLException e){
}
try {
Cursor cursor = dbHelper.QueryData("SELECT * FROM my_database");
if(cursor != null){
if (cursor.moveToFirst()){
do {
Item item = new Item();
item.setId(cursor.getString(0));
item.setTitle(cursor.getString(1));
item.setContent(cursor.getString(2));
arrayList.add(item);
} while (cursor.moveToNext());
}
}
} catch (SQLException e){
adapter = new Adapter(this,R.layout.custom_list_item,arrayList);
listViewVar = (ListView) findViewById(R.id.list_item);
listViewVar.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
Aucun commentaire:
Enregistrer un commentaire