mardi 2 juin 2015

Populate ListView with SQLite, Images and Text from local resources

I have a ListView that will be able to show two TextViews and an ImageView per ListViewItem. I also have a SQLite Database that has the following fields, _id, name, description, image.

An example record would be as follows: 1,R.string.name1,R.string.description1,R.drawable.image1

The strings for name and description are in the strings.xml file, and the images needed are in the res/drawable folders, which would usually be referenced by R.drawable.image_name

I am using the SQLiteAssetHelper Library to manage the database.

I am able to get a cursor containing all the information needed, and I am able to populate the listview with the text, but when I run the app, the textviews show as R.string.name1, R.string.description1, etc. I have not yet been able to get the image to work at all.

How would I get the text to show properly (so I can have different languages in the future) and how would I get the images to show?

Here is my code so far:

Database Helper

    public class Database extends SQLiteAssetHelper {

        private static final String DATABASE_NAME = "database.sqlite";
        private static final int DATABASE_VERSION = 1;

        public Database(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            setForcedUpgrade();
        }
        public Cursor getList() {

            SQLiteDatabase db = getReadableDatabase();
            SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

            String [] sqlSelect = {"_id","name","description","image"};
            String sqlTables = "tbl_list";
            qb.setTables(sqlTables);
            Cursor c = qb.query(db, sqlSelect, null, null,
                    null, null, null);

            c.moveToFirst();
            return c;

        }
    }

Main Activity

public class SQLite_List extends ActionBarActivity {
    private ListView listView1;
    private Cursor list;
    private Database db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.listview);
        getSupportActionBar().setTitle("List");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        db = new Database(this);
        list = db.getList(); //Move this to its own thread later on

        ListAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_2,
                mods,
                new String[] {"name","description"}, //table values
                new int[] {android.R.id.text1,android.R.id.text2});

        listView1 = (ListView) findViewById(R.id.listview);
        listView1.setAdapter(adapter);
        }
...
}

Thanks

Aucun commentaire:

Enregistrer un commentaire