My listview with database is not working
Hi I have created a list view in my activity,in which I want to display
the contents from the database. but an error occured saying there is no
list view found Can anybody give me a better solution to overcome the
error?
my log cat is given below
08-21 06:54:52.103: E/AndroidRuntime(6874): FATAL EXCEPTION: main
08-21 06:54:52.103: E/AndroidRuntime(6874): java.lang.RuntimeException:
Unable to start a
ctivity ComponentInfo{com.neochat/com.neochat.Friends}:
java.lang.RuntimeException: Unable
to start activity ComponentInfo{com.neochat/com.neochat.Friends_list}:
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is
'android.R.id.list'
08-21 06:54:52.103: E/AndroidRuntime(6874): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-21 06:54:52.103: E/AndroidRuntime(6874): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-21 06:54:52.103: E/AndroidRuntime(6874): at
android.app.ActivityThread.access$600(ActivityThread.java:141)
I am giving my code for the listview java class below
public class Friends_list extends ListActivity implements
OnItemClickListener{
private ArrayList<String> results = new ArrayList<String>();
private String tableName = LoginDataBaseAdapter.tableName;
private SQLiteDatabase newDB;
ListView listview;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
openAndQueryDatabase();
displayResultList();
setContentView(R.layout.activity_friends);
context=this;
listview=(ListView)findViewById(R.id.listView1);
//String[] arrayColumns=new String[]{"Name,Username"};
//int[]arrayViewIDs=new int[]
{R.id.textViewSMSSender,R.id.textViewMessageBody};
Cursor cursor;
//cursor.getContentResolver().query(Uri.parse
("content://sms/inbox"),null,null,null,null);
//@SuppressWarnings("deprecation")
//SimpleCursorAdapter adapter=new
SimpleCursorAdapter(this,R.layout.friendsms, cursor,
arrayColumns,
arrayViewIDs);
//listview.setAdapter(adapter);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
value);
listview.setOnItemClickListener(this);
}
private void displayResultList() {
TextView tView = new TextView(this);
tView.setText("Friends");
getListView().addHeaderView(tView);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
private void openAndQueryDatabase() {
try {
LoginDataBaseAdapter loginDataBaseAdapter=new
LoginDataBaseAdapter(this.getApplicationContext());
newDB=loginDataBaseAdapter.getWritableDatabase();
Cursor c=newDB.rawQuery(" SELECT NAME , USERNAME FROM "+
tableName, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String Name=c.getString(c.getColumnIndex("NAME"));
String Username=c.getString(c.getColumnIndex("USERNAME"));
}
while(c.moveToNext());
}
}
}
catch (SQLiteException se) {
Log.e(getClass().getSimpleName(), "Could not open the database");
}
finally {
if(newDB!=null)
newDB.execSQL(" DELETE FROM "+ tableName);
newDB.close();
}
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
Intent in2=new
Intent(Friends_list.this,InboxActivity.class);
startActivity(in2);
}
}
Here is the code where I have created my database
public class LoginDataBaseAdapter extends SQLiteOpenHelper {
public static final int NAME_COLUMN=2;
static final String DATABASE_NAME = "UserDetails.db";
static final int DATABASE_VERSION = 1;
static final String tableName="Signup";
static final String DATABASE_CREATE = "create table "
+ " Signup "+ " " + " "
+ " ( "
+ " ID "
+ " integer primary key autoincrement , "
+ " NAME text , USERNAME text ,
PASSWORD text , EMPLOYEE_CODE text,"
+ " MOBILE_NUMBER integer ); ";
public SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbHelper;
No comments:
Post a Comment