Dont know what I am doing wrong, I have been on this for days; I want to load data from a database to a layout using a custom cursor adapter, anytime I run the app, it exits with errors. Here is my code. Any help is appreciated. MainActivity.java:
package com.example.makmessengerprototype;
import java.util.List;
import android.os.Bundle;
import android.os.Handler;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView listView;
private ChatListAdapter adapter;
private DbHelper dbHelper;
private CharSequence[] dialogItems = {"Chat", "View Profile", "Call", "Block", "Delete Conversation", "Email Conversation", "Send Video"};
private CharSequence status = "Hi, Am using Mak Messenger";
private CharSequence name = "Enokela Acheme Paul";
private String chatterName;
private Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_list);
listView = (ListView) findViewById(R.id.chatsListView);
dbHelper = new DbHelper(this);
if (dbHelper.getCount() < 1) {
dbHelper.populateChatTable();
}
c = dbHelper.getCursor();
//images = dbHelper.getImages();
//ActionBar actionBar = getActionBar();
//actionBar.setIcon(R.drawable.omotola);
//actionBar.setTitle(name);
//actionBar.setSubtitle(status);
new Handler().post(new Runnable() {
public void run() {
ChatListAdapter adapter = new ChatListAdapter(MainActivity.this, c, 0);
listView.setAdapter(adapter);
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int index, long arg3) {
// TODO Auto-generated method stub
int searchIndex = index+1;
chatterName = dbHelper.getChatterName(searchIndex);
openDialog(chatterName);
return false;
}
});
}
public void openDialog(String chatterName) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(chatterName);
alertDialogBuilder.setItems(dialogItems, null);
alertDialogBuilder.setIcon(R.drawable.jlo);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_chat_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case R.id.my_profile:
myProfileActivity();
break;
}
return false;
}
private void myProfileActivity() {
Intent myProfileIntent = new Intent(this, MyProfileActivity.class);
startActivity(myProfileIntent);
}
}
DbHelper.java:
package com.example.makmessengerprototype;
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 DbHelper extends SQLiteOpenHelper{
private static final String DB_NAME = "CHATS_DB";
private static final String DB_TABLE = "CHATS_TABLE";
private static final int DB_VERSION = 1;
//COLUMN NAMES
private static final String SENDER = "sender_";
private static final String RECEIVER = "receiver_";
private static final String MSG = "msg";
private static final String MSG_ID = "msg_id";
private static final String MSG_COUNT = "mssg_count";
private static final String SENT_STATUS = "sent_status";
private static final String DELIVER_STATUS = "deliver_status";
private static final String READ_STATUS = "read_status";
private static final String TIME = "time_";
private String[] staticDataNames = new String[] {"Jennifer Lopez", "Cristiano Ronaldo",
"Bill Gates", "Enokela Paul", "Warren Buffet", "Chris Brown", "Bakare"};
private int[] images = {R.drawable.jlo, R.drawable.ronaldo, R.drawable.omotola, R.drawable.milan,
R.drawable.rihanna, R.drawable.chrisbrown, R.drawable.bakare};
private String[] mssgs = {"Hi there", "How are you", "You should fear not", "Eat while you can"
,"Finally, I meet you", "Very Funny", "Coooool!"};
private String[] msgCounts = {"3", "2", "1", "4", "2", "3", "1"};
public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DB_TABLE + " (id_ integer primary key, sender_ text, receiver_ text, msg text," +
"msg_id text, sent_status text, deliver_status text, read_status text, time_ text, mssg_count text, image_ int)");
Log.d("Created successfully", "");
}
public void populateChatTable () {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
for (int i=0; i<staticDataNames.length; i++) {
if (i==1 || i==3 || i==5) {
values.put(SENDER, "1");
values.put(RECEIVER, staticDataNames[i]);
}
else {
values.put(RECEIVER, staticDataNames[i]);
values.put(SENDER, "1");
}
values.put(MSG, mssgs[i]);
values.put(MSG_ID, "1123AA");
values.put(SENT_STATUS, "1");
values.put(DELIVER_STATUS, "1");
values.put(READ_STATUS, "1");
values.put(TIME, "3:50");
values.put(MSG_COUNT, msgCounts[i]);
values.put("image_", images[i]);
db.insert(DB_TABLE, null, values);
}
Log.d("Table populated", "");
}
public int getCount() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM "+ DB_TABLE, null);
return res.getCount();
}
public Cursor getCursor() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM "+ DB_TABLE, null);
return res;
}
public String getChatterName(int listItemIndex) {
SQLiteDatabase db = this.getWritableDatabase();
String chatterName = new String();
String sql ="SELECT receiver_, sender_ FROM " + DB_TABLE + " WHERE id_="+listItemIndex;
Cursor res = db.rawQuery(sql, null);
res.moveToFirst();
if (res.getString(res.getColumnIndex(RECEIVER))== "1") {
chatterName = res.getString(res.getColumnIndex(SENDER));
}
else {
chatterName = res.getString(res.getColumnIndex(RECEIVER));
}
return chatterName;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS" + DB_TABLE);
this.onCreate(db);
}
}
ChatListAdapter.java
package com.example.makmessengerprototype;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class ChatListAdapter extends CursorAdapter{
private LayoutInflater mInflater;
public ChatListAdapter (Context context, Cursor c, int flags) {
super(context, c, flags);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View newView(Context context, Cursor cursors, ViewGroup parent) {
// TODO Auto-generated method stub
View v = mInflater.inflate(R.layout.chat_list_inflater,parent,false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
ViewHolder viewHolder = new ViewHolder();
String chatter = new String();
// TODO Auto-generated method stub
ViewHolder.name = (TextView) v.findViewById(R.id.name_c);
viewHolder.lastMsg = (TextView) v.findViewById(R.id.lastMessage_c);
viewHolder.msgCount = (TextView) v.findViewById(R.id.numMessages_c);
viewHolder.date = (TextView) v.findViewById(R.id.date_c);
ViewHolder.img = (ImageView) v.findViewById(R.id.imageView_c);
if(c.getString(c.getColumnIndex("sender_"))=="1") {
chatter = c.getString(c.getColumnIndex("receiver_"));
}
else {
chatter = c.getString(c.getColumnIndex("sender_"));
}
ViewHolder.name.setText(chatter);
viewHolder.lastMsg.setText(c.getString(c.getColumnIndex("msg")));
viewHolder.msgCount.setText("2");
//viewHolder.img.setImageResource(images[2]);
}
}
class ViewHolder {
public static ImageView img;
public static TextView name;
public TextView lastMsg;
public TextView date;
public TextView msgCount;
}
chat_list_inflater.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
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"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/jlo" />
<TextView
android:id="@+id/date_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageView_c"
android:text="@string/default_date" />
<TextView
android:id="@+id/name_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_alignBottom="@+id/date_c"
android:layout_toRightOf="@+id/imageView_c"
android:text="@string/default_name" />
<TextView
android:id="@+id/numMessages_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/lastMessage_c"
android:layout_alignBottom="@+id/lastMessage_c"
android:layout_alignRight="@+id/date_c"
android:text="@string/default_num_mssgs" />
<TextView
android:id="@+id/lastMessage_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_alignBottom="@+id/imageView_c"
android:layout_alignLeft="@+id/name_c"
android:text="@string/default_mssg" />
</RelativeLayout>
And the Error log
06-05 19:18:59.963: D/AndroidRuntime(1940): Shutting down VM
06-05 19:18:59.963: W/dalvikvm(1940): threadid=1: thread exiting with uncaught exception (group=0xb3e89648)
06-05 19:18:59.963: E/AndroidRuntime(1940): FATAL EXCEPTION: main
06-05 19:18:59.963: E/AndroidRuntime(1940): java.lang.IllegalArgumentException: column '_id' does not exist
06-05 19:18:59.963: E/AndroidRuntime(1940): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
06-05 19:18:59.963: E/AndroidRuntime(1940): at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
06-05 19:18:59.963: E/AndroidRuntime(1940): at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:151)
06-05 19:18:59.963: E/AndroidRuntime(1940): at com.example.makmessengerprototype.ChatListAdapter.<init>(ChatListAdapter.java:16)
06-05 19:18:59.963: E/AndroidRuntime(1940): at com.example.makmessengerprototype.MainActivity$1.run(MainActivity.java:55)
06-05 19:18:59.963: E/AndroidRuntime(1940): at android.os.Handler.handleCallback(Handler.java:730)
06-05 19:18:59.963: E/AndroidRuntime(1940): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 19:18:59.963: E/AndroidRuntime(1940): at android.os.Looper.loop(Looper.java:137)
06-05 19:18:59.963: E/AndroidRuntime(1940): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-05 19:18:59.963: E/AndroidRuntime(1940): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 19:18:59.963: E/AndroidRuntime(1940): at java.lang.reflect.Method.invoke(Method.java:525)
06-05 19:18:59.963: E/AndroidRuntime(1940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-05 19:18:59.963: E/AndroidRuntime(1940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-05 19:18:59.963: E/AndroidRuntime(1940): at dalvik.system.NativeStart.main(Native Method)
06-05 19:19:01.813: I/Process(1940): Sending signal. PID: 1940 SIG: 9
Aucun commentaire:
Enregistrer un commentaire