-2

我有一个包含一个ListView和一个图片的数据库,我想在每个语音数据中包含一个。我试过了,但我有很多错误。以下是我的java和xml。

图片

数据库.java

package com.example.database;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    public class Database extends SQLiteOpenHelper {
    final static String DB_NAME = "db_tum_obat";
public Database(Context context) {
    super(context, DB_NAME, null, 1);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE IF NOT EXISTS tum(_id INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT, nama_latin TEXT, khasiat TEXT, img BLOB, img2 BLOB)";
    db.execSQL(sql);

    ContentValues values = new ContentValues();
    values.put("_id", "1");
    values.put("nama", "Jarak Pagar");
    values.put("nama_latin", "Jatropha curcas Linn.");
    values.put("khasiat",
            "ketombe, lemah saraf, menghitamkan rambut, rambut rontok, rematik");
    values.put("img", R.drawable.im31);
    values.put("img2", R.drawable.home);// im31 nama file gambar dengan
                                        // ukuran 80 x 80 pixel yang ada di
                                        // folder res/drawable pada project
    db.insert("tum", "_id", values);

    values.put("_id", "2");
    values.put("nama", "Kumis Kucing");
    values.put("nama_latin", "Orthosipon aristatus (B1) Miq.");
    values.put("khasiat",
            "ketombe, lemah saraf, menghitamkan rambut, rambut rontok, rematik");
    values.put("img", R.drawable.im32);
    values.put("img2", R.drawable.keluar1);// im32 nama file gambar dengan
                                        // ukuran 80 x 80 pixel yang ada di
                                        // folder res/drawable pada project
    db.insert("tum", "_id", values);

    values.put("_id", "3");
    values.put("nama", "Lidah Buaya");
    values.put("nama_latin", "Aloe Verra Linn.");
    values.put("khasiat",
            "ketombe, lemah saraf, menghitamkan rambut, rambut rontok, rematik");
    values.put("img", R.drawable.im33);
    values.put("img2", R.drawable.home);// im33 nama file gambar dengan
                                        // ukuran 80 x 80 pixel yang ada di
                                        // folder res/drawable pada project
    db.insert("tum", "_id", values);

    values.put("_id", "4");
    values.put("nama", "Pandan Wangi");
    values.put("nama_latin", "Pandanus amryllifolius Roxb");
    values.put("khasiat",
            "ketombe, lemah saraf, menghitamkan rambut, rambut rontok, rematik");
    values.put("img", R.drawable.im34);
    values.put("img2", R.drawable.keluar1);// im34 nama file gambar dengan
                                        // ukuran 80 x 80 pixel yang ada di
                                        // folder res/drawable pada project
    db.insert("tum", "_id", values);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS tum");
    onCreate(db);

}

}

MainActivity.java

package com.example.database;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
protected ListView lv;
protected ListAdapter adapter;
SQLiteDatabase db;
Cursor cursor;
EditText et_db;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = (new Database(this)).getWritableDatabase();
    lv = (ListView) findViewById(R.id.lv);
    et_db = (EditText) findViewById(R.id.et);

    try {
        cursor = db.rawQuery("SELECT * FROM tum ORDER BY nama ASC", null);
        adapter = new SimpleCursorAdapter(this, R.layout.isi_lv, cursor,
                new String[] { "nama", "nama_latin", "img" }, new int[] {
                        R.id.tv_nama, R.id.tv_penyebab, R.id.imV });
        lv.setAdapter(adapter);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                detail(position);

            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }

}

@SuppressWarnings("deprecation")
public void search_db(View v) {
    String edit_db = et_db.getText().toString();
    if (!edit_db.equals("")) {
        try {
            cursor = db.rawQuery("SELECT * FROM tum WHERE nama LIKE ?",
                    new String[] { "%" + edit_db + "%" });
            adapter = new SimpleCursorAdapter(this, R.layout.isi_lv,
                    cursor, new String[] { "nama", "nama_latin", "img" },
                    new int[] { R.id.tv_nama, R.id.tv_penyebab, R.id.imV });
            if (adapter.getCount() == 0) {
                Toast.makeText(
                        this,
                        "Tidak ditemukan data dengan kata kunci " + edit_db
                                + "", Toast.LENGTH_SHORT).show();
            } else {
                lv.setAdapter(adapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        try {
            cursor = db.rawQuery("SELECT * FROM tum ORDER BY nama ASC",
                    null);
            adapter = new SimpleCursorAdapter(this, R.layout.isi_lv,
                    cursor, new String[] { "nama", "nama_latin", "img" },
                    new int[] { R.id.tv_nama, R.id.tv_penyebab, R.id.imV });
            lv.setAdapter(adapter);
            lv.setTextFilterEnabled(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public void detail(int position) {
    int im2 = 0;
    String _id = "";
    String nama = "";
    String latin = "";
    String khasiat = "";
    if (cursor.moveToFirst()) {
        cursor.moveToPosition(position);
        im2 = cursor.getInt(cursor.getColumnIndex("img2"));
        nama = cursor.getString(cursor.getColumnIndex("nama"));
        latin = cursor.getString(cursor.getColumnIndex("nama_latin"));
        khasiat = cursor.getString(cursor.getColumnIndex("khasiat"));
    }

    Intent iIntent = new Intent(this, DetailTum.class);
    iIntent.putExtra("dataIM2", im2);
    iIntent.putExtra("dataNama", nama);
    iIntent.putExtra("dataLatin", latin);
    iIntent.putExtra("dataKhasiat", khasiat);
    setResult(RESULT_OK, iIntent);
    startActivityForResult(iIntent, 99);
}

 }

DetailTum.java

package com.example.database;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;


public class DetailTum extends Activity {
ImageView Im2;
TextView tv_nama, tv_latin, tv_khasiat, id, namaIm;
Gallery gallery;
ImageSwitcher imageSwitcher;
Integer[] imageIDs = new Integer[3];
int msg_im2;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail);

    Intent iIdentifikasi = getIntent();
    msg_im2 = iIdentifikasi.getIntExtra("dataIM2", 0);
    String msg_nama = iIdentifikasi.getStringExtra("dataNama");
    String msg_latin = iIdentifikasi.getStringExtra("dataLatin");
    String msg_khasiat = iIdentifikasi.getStringExtra("dataKhasiat");
    Im2 = (ImageView) findViewById(R.id.iv_detail2);
    tv_nama = (TextView) findViewById(R.id.tvNama);
    tv_latin = (TextView) findViewById(R.id.tvLatin);
    tv_khasiat = (TextView) findViewById(R.id.tvKhasiat);
    Im2.setImageResource(msg_im2);
    tv_nama.setText(msg_nama);
    tv_latin.setText(msg_latin);
    tv_khasiat.setText(msg_khasiat);
}
  }

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF567"
android:orientation="vertical"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/btn" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="search_db"
    android:text="Search" />

<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et" >
</ListView>

isi_lv.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/tv_nama"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imV"
    android:text="TextView"
    android:textSize="10pt" />

<TextView
    android:id="@+id/tv_penyebab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tv_nama"
    android:layout_below="@+id/tv_nama"
    android:text="TextView" />

详细信息.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF567"
android:orientation="vertical" >


<ImageView
    android:id="@+id/iv_detail2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nama" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=":" />

        <TextView
            android:id="@+id/tvNama"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nama Latin" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=":" />

        <TextView
            android:id="@+id/tvLatin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Khasiat" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=":" />

        <TextView
            android:id="@+id/tvKhasiat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </TableRow>
</TableLayout>

4

1 回答 1

0

如果您想保存路径,请按照 Telthien 所说的做。

如果要将文件作为一个整体保存,可以将数据保存为 BLOB 类型。http://www.sqlite.org/datatype3.html

于 2013-04-24T19:00:13.257 回答