I have created an android app in which i have to fetch data from sqlite database and set it on custom listview. Problem is that data is not shown. my code is same as a required for showing output.
Asked
Active
Viewed 1,138 times
-1

James Z
- 12,209
- 10
- 24
- 44

Muhammad Hamza Anwar
- 11
- 3
-
If you have problem with your code, you need to include **it**, not ask other people to provide you a complete code. – James Z Jan 30 '18 at 16:09
1 Answers
1
You need to create a class which extends CursorAdapter. Below is the demo code:
public class PassCursorAdapter extends CursorAdapter {
public PassCursorAdapter(Context context, Cursor c) {
super(context, c,0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item_todo,parent,false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textID = (TextView) view.findViewById(R.id.textView6);
TextView textName = (TextView) view.findViewById(R.id.textView3);
TextView textUser = (TextView) view.findViewById(R.id.textView4);
TextView textPass = (TextView) view.findViewById(R.id.textView5);
int idColumnIndex = cursor.getColumnIndex(PassDBHelper.COLUMN_ID);
int nameColumnIndex = cursor.getColumnIndex(PassDBHelper.PASS_COLUMN_NAME);
int userColumnIndex = cursor.getColumnIndex(PassDBHelper.PASS_COLUMN_USERNAME);
int passColumnIndex = cursor.getColumnIndex(PassDBHelper.PASS_COLUMN_PASSWORD);
String id = cursor.getString(idColumnIndex);
String name = cursor.getString(nameColumnIndex);
String user = cursor.getString(userColumnIndex);
String pass = cursor.getString(passColumnIndex);
textID.setText(id);
textName.setText(name);
textUser.setText(user);
textPass.setText(pass);
}
}
In the newView method you are returning the layout file. This is how your List View layout file will be with 4 Text Views. In the End there is the method bindView where you set the id's.
Now to display the database you need to get the data from the sq-lite database as follows:
private void displayDataBaseInfo() {
PassDBHelper passDBHelper = new PassDBHelper(this);
SQLiteDatabase db = passDBHelper.getReadableDatabase();
String [] columns = {
PassDBHelper.COLUMN_ID,
PassDBHelper.PASS_COLUMN_NAME,
PassDBHelper.PASS_COLUMN_USERNAME,
PassDBHelper.PASS_COLUMN_PASSWORD
} ;
Cursor cursor = db.query(PassDBHelper.TABLE_NAME,columns,null,null,null,null,null);
ListView listView = (ListView)findViewById(R.id.list);
PassCursorAdapter passCursorAdapter = new PassCursorAdapter(this,cursor);
listView.setAdapter(passCursorAdapter);
}//displayDatabaseInfo