venura venura - 5 months ago 19
Android Question

How do I retrieve two fields from a prepopulate sqlite db and assign it to custom list view?

I want to retrieve data from two fields from prepopulate sqlite database. The fields are Organization Name (org_name) and Contact Number (contact_no). After that I need to assign org_name data to a large text field and contact_no to small text field in my custom list view.

I have tried this only with one field. It's working fine. But when I'm trying to retrieve two fields it's not working. This is what I tried. Please help me to solve this issue.

ContactView class

public class ContactView extends Activity {
private ListView listView;
private ListView listView1;
List<Organization> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);



DBAccess databaseAccess = DBAccess.getInstance(this);
databaseAccess.open();
List<String> quotes = databaseAccess.getQuotes(getIntent().getStringExtra("ID_EXTRA"));
databaseAccess.close();


List<Organization> rowItem=new ArrayList<Organization>();
for(String quote:quotes){
Organization temp=new Organization(quote);
rowItem.add(temp);
}
listView = (ListView)findViewById(R.id.listView);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.single_row_item, rowItem);
listView.setAdapter(adapter);



}


Database Access Class

public class DBAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DBAccess instance;
String passedVar = null;
private ListView listView;

public DBAccess(Context context) {

this.openHelper = new HelloDatabase(context);
}

public static DBAccess getInstance(Context context) {
if (instance == null) {
instance = new DBAccess(context);
}
return instance;
}

public void open() {
this.database = openHelper.getWritableDatabase();
}

public void close() {
if (database != null) {
this.database.close();
}
}

public List<String> getQuotes(String id) {
List<String> list = new ArrayList<>();
Integer value;


if (id != null) {
Cursor cursor = database.rawQuery("SELECT org_name,contact_no FROM org_name WHERE category_id = \"" + id + "\"", null);


cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
list.add(cursor.getString(cursor.getColumnIndex("contact_no")));
cursor.moveToNext();

}

cursor.close();


}


return list;
}}


Bean class

public class Organization {

public String title;
public String telenum;

public Organization(String title,String telenum) {

this.title = title;
this.telenum=telenum;

}

public String getTitle() {

return title;
}
public void setTitle(String title) {

this.title = title;
}

public String getTelenum(){
return telenum;
}

public void setTelenum(String telenum){
this.telenum=telenum;
}

}


CustomListViewAdapter class

public class CustomListViewAdapter extends ArrayAdapter<Organization> {

Context context;

public CustomListViewAdapter(Context context, int layout,
List<Organization> items) {
super(context, layout, items);
this.context = context;
}

/*private view holder class*/
private class ViewHolder {

TextView txtTitle;
TextView txtTele;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Organization rowItem = getItem(position);

LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.single_row_item, null);
holder = new ViewHolder();

holder.txtTitle = (TextView) convertView.findViewById(R.id.org_name);
holder.txtTele = (TextView) convertView.findViewById(R.id.tele_num);

convertView.setTag(holder);


} else
holder = (ViewHolder) convertView.getTag();


holder.txtTitle.setText(rowItem.getTitle());
holder.txtTele.setText(rowItem.getTelenum());

return convertView;
}}

Answer

Note : If any one facing the same issue, kindly use SimpleCursorAdapter. Its simple, easy to use and efficient. Find a simple example here or checkout my answer on this thread http://stackoverflow.com/a/37560755/5460053


It is not working for two fields because of the following lines in getQuotes() method of DBAccess class :

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        list.add(cursor.getString(0));//Adding org_name first. org_name is added at indexes : 0,2,4,...
        list.add(cursor.getString(cursor.getColumnIndex("contact_no")));//Then adding contact_no. contact_no is added at indexes : 1,3,5,...
        cursor.moveToNext();

    }

Then while creating a datasource for the adapter, org_name and contact_no are added at alternate indexes

    List<Organization> rowItem=new ArrayList<Organization>();
    for(String quote:quotes){
        Organization temp=new Organization(quote);//I wonder how this worked as there is only one contructor for Organization which is expecting 2 parameters
        rowItem.add(temp);
    }

Change your DBAccess class getQuotes() to this :

public List<Organization> getQuotes(String id) {
    List<Organization> list = new ArrayList<>();

    if (id != null) {
        Cursor cursor = database.rawQuery("SELECT org_name,contact_no FROM org_name WHERE category_id = \"" + id + "\"", null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Organization org = new Organization(cursor.getString(0),
                cursor.getString(cursor.getColumnIndex("contact_no")));
                list.add(org);
            cursor.moveToNext();
        }

        cursor.close();
    }
    return list;
}

And change your ContactView Activity's onCreate() to this :

public class ContactView extends Activity {
private ListView listView;
private ListView listView1;
List<Organization> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view);

    DBAccess databaseAccess = DBAccess.getInstance(this);
    databaseAccess.open();
    List<Organization> rowItem = databaseAccess.getQuotes(getIntent().getStringExtra("ID_EXTRA"));
    databaseAccess.close();

    listView = (ListView)findViewById(R.id.listView);
    CustomListViewAdapter adapter = new CustomListViewAdapter(this,
            R.layout.single_row_item, rowItem);
    listView.setAdapter(adapter);



}
Comments