how to show images from resources drawable and database

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

how to show images from resources drawable and database

Postby gabisabobo » Sat Oct 08, 2011 4:03 am

I'm trying to put different images (.jpg,.png) dynamically into a ListView from res/drawable . The names from the images I get from a database. The images themselves are in the res/drawable folder.

here is my code;
sql.xml

Code: Select all
<sql>
<statement>
CREATE TABLE IF NOT EXISTS posts (
    _id INTEGER PRIMARY KEY AUTOINCREMENT,
    subcatId INTEGER,
    postOne VARCHAR(255),
    postTwo VARCHAR(255),
    picture VARCHAR(50))
</statement>
<statement>INSERT INTO posts VALUES(1,1,'Kembang Aren arane Dangu','Bunga Aren disebut Dangu','dangu')</statement>
<statement>INSERT INTO posts VALUES(2,1,'Kembang Gedhang Arane Ontong','Bunga Pisang disebut Ontong','ontong')</statement>
</sql>


PepakPostView.java
Code: Select all
public class PepakPostView extends ListActivity {
    protected Cursor cursor;
    protected ListAdapter adapter;
    protected int subcatId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.post);

        subcatId = getIntent().getIntExtra("SUBCATEGORY_ID", 0);
        SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT _id, subcatId, postOne, postTwo, picture FROM posts WHERE subcatId = ?",
                new String[]{""+subcatId});
        adapter = new SimpleCursorAdapter(
        this,
        R.layout.post_list,
        cursor,
        new String[] {"postOne", "postTwo", "picture"},
        new int[] {R.id.postOne, R.id.postTwo, R.id.picture});
        setListAdapter(adapter);
    }

}


and here my xml layout post_list.xml

<ImageView
android:id="@+id/picture"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft = "true"
/>


I am a beginner in android, I want a simple code and easy to understand.
Thanks in advance.
regard,
Gabisabobo
gabisabobo
Freshman
Freshman
 
Posts: 4
Joined: Sat Oct 08, 2011 3:54 am
Location: Ngawi - Indonesia

Top

Re: how to show images from resources drawable and database

Postby gabisabobo » Tue Oct 11, 2011 12:21 pm

anyone please help me.. :(
gabisabobo
Freshman
Freshman
 
Posts: 4
Joined: Sat Oct 08, 2011 3:54 am
Location: Ngawi - Indonesia

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests