如何在片段上查看数据库中的数据

时间:2013-12-12 00:35:26

标签: java android sqlite

我正在尝试在片段上查看数据库中的数据,我使用TableRow来查看数据。

我的代码设置数据库

public class SqliteManager extends Activity {

public static final int VERSI_DATABASE= 1;
public static final String NAMA_DATABASE = "dbCrudSqlite";
public static final String NAMA_TABEL = "tbAgenda";

public static final String FIELD_ID = "_id";
public static final int POSISI_ID = 0;
public static final String FIELD_JUDUL = "judul";
public static final int POSISI_JUDUL = 1;
 public static final String FIELD_DESKRIPSI = "deskripsi";
 public static final int POSISI_DESKRIPSI = 2;
 public static final String FIELD_WAKTU = "waktu";
public static final int POSISI_WAKTU = 3;

public static final String[] FIELD_TABEL ={ SqliteManager.FIELD_ID, SqliteManager.FIELD_JUDUL, SqliteManager.FIELD_DESKRIPSI, SqliteManager.FIELD_WAKTU };

 private Context crudContext;
 private SQLiteDatabase crudDatabase;
private SqliteManagerHelper crudHelper;

private static class SqliteManagerHelper extends SQLiteOpenHelper {
    private static final String BUAT_TABEL =
        "create table " + NAMA_TABEL + " (" +
        SqliteManager.FIELD_ID + " integer primary key autoincrement, " +
        SqliteManager.FIELD_JUDUL + " text not null, " +
        SqliteManager.FIELD_DESKRIPSI + " text not null," +
        SqliteManager.FIELD_WAKTU + " text not null " +
        ");";

    public SqliteManagerHelper(Context context) {
        super(context, NAMA_DATABASE, null, VERSI_DATABASE);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(BUAT_TABEL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {}

}


public void generateData(SQLiteDatabase database){
    ContentValues cv=new ContentValues();
    cv.put(FIELD_ID, "3");
    cv.put(FIELD_JUDUL, "Abang");
    cv.put(FIELD_DESKRIPSI, "Abang");
    cv.put(FIELD_WAKTU, "Merah");
    database.insert(NAMA_TABEL,null,cv);

    cv.put(FIELD_ID, "2");
    cv.put(FIELD_JUDUL, "Opo");
    cv.put(FIELD_DESKRIPSI, "Opo");
    cv.put(FIELD_WAKTU, "Apa");
    database.insert(NAMA_TABEL,null,cv);
}

public SqliteManager(Context context) {
    crudContext = context;
}

public void bukaKoneksi() throws SQLException {
    crudHelper = new SqliteManagerHelper(crudContext);
    crudDatabase = crudHelper.getWritableDatabase();
    generateData(crudDatabase);
}

public void tutupKoneksi() {
    crudHelper.close();
    crudHelper = null;
    crudDatabase = null;
}

public long insertData(ContentValues values) {
    return crudDatabase.insert(NAMA_TABEL, null, values);
}

public boolean updateData(long rowId, ContentValues values) {
    return crudDatabase.update(NAMA_TABEL, values,
            SqliteManager.FIELD_ID + "=" + rowId, null) > 0;
}

public boolean hapusData(long rowId) {
    return crudDatabase.delete(NAMA_TABEL,
            SqliteManager.FIELD_ID + "=" + rowId, null) > 0;
}

public Cursor bacaData() {
    return crudDatabase.query(NAMA_TABEL,FIELD_TABEL,null, null, null, null,SqliteManager.FIELD_JUDUL + " DESC");
}

public Cursor bacaDataTerseleksi(long rowId) throws SQLException {
    Cursor cursor = crudDatabase.query(true, NAMA_TABEL,FIELD_TABEL,FIELD_ID + "=" + rowId,null, null, null, null, null);
    cursor.moveToFirst();
    return cursor;
}

public ContentValues ambilData(String tempat, String lat, String lng) {
    ContentValues values = new ContentValues();
    values.put(SqliteManager.FIELD_JUDUL, tempat);
    values.put(SqliteManager.FIELD_DESKRIPSI, lat);
    values.put(SqliteManager.FIELD_WAKTU, lng);
    return values;
}

public ArrayList<ArrayList<Object>> ambilSemuaBaris(){
    ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
    Cursor cur;
    try
    {
        cur = crudDatabase.query(NAMA_TABEL, new String[]{FIELD_ID,FIELD_JUDUL,FIELD_DESKRIPSI}, null, null, null, null, null);
        cur.moveToFirst();
        if (!cur.isAfterLast())
        {
            do {
                ArrayList<Object> dataList = new ArrayList<Object>();
                dataList.add(cur.getLong(0));
                dataList.add(cur.getString(1));
                dataList.add(cur.getString(2));
                dataArray.add(dataList);
            }while (cur.moveToNext());
        }
    }catch (Exception e)
    {
        e.printStackTrace();
        Log.e("DB ERROR",e.toString());
    }
    return dataArray;
}
}

我想查看片段数据:

public class JawaIndo extends Fragment {
private SqliteManager sqliteDB;
private Activity activity;
TextView bhsjawa,bhsindo;
Button addBtn;
TableLayout tabel4data;

public JawaIndo(){}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.jawaindo, container, false);
    sqliteDB = new SqliteManager(getActivity());
    sqliteDB.bukaKoneksi();

    activity = new Activity();

    tabel4data = (TableLayout) activity.findViewById(R.id.tabel_data);
    bhsjawa = (TextView) activity.findViewById(R.id.nama_id);
    bhsindo = (TextView) activity.findViewById(R.id.hobi_id);
    updateTable();

    return rootView;
}

protected void updateTable() {
    while (tabel4data.getChildCount()>1){
        tabel4data.removeViewAt(1);
    }
    ArrayList<ArrayList<Object>> data = sqliteDB.ambilSemuaBaris();
    for (int posisi = 0; posisi < data.size(); posisi++) {
        TableRow tabelBaris = new TableRow(getActivity());
        ArrayList<Object> baris = data.get(posisi);

        TextView idTxt = new TextView(getActivity());
        idTxt.setText(baris.get(0).toString());
        tabelBaris.addView(idTxt);

        TextView namaTxt = new TextView(getActivity());
        namaTxt.setText(baris.get(1).toString());
        tabelBaris.addView(namaTxt); 

        TextView hobiTxt = new TextView(getActivity());
        hobiTxt.setText(baris.get(2).toString());
        tabelBaris.addView(hobiTxt);
        tabel4data.addView(tabelBaris);
    }
}
}

在JawaIndo类上编写此代码时出错:

    activity = new Activity();
    tabel4data = (TableLayout) activity.findViewById(R.id.tabel_data);
    bhsjawa = (TextView) activity.findViewById(R.id.nama_id);
    bhsindo = (TextView) activity.findViewById(R.id.hobi_id);
    updateTable();

3 个答案:

答案 0 :(得分:1)

您刚刚在 onCreateView( )方法中更改了一些内容:

它应该是

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

  View rootView = inflater.inflate(R.layout.jawaindo, container, false);
  sqliteDB = new SqliteManager(getActivity());
  sqliteDB.bukaKoneksi();

  //activity = new Activity();

  // Update Here
  tabel4data = (TableLayout) rootView .findViewById(R.id.tabel_data);
  bhsjawa = (TextView) rootView .findViewById(R.id.nama_id);
  bhsindo = (TextView) rootView .findViewById(R.id.hobi_id);
  updateTable();

  return rootView;
}

在此处传递了您的View的名为 rootView 的对象作为参考,以查找您的UI元素的ID。

答案 1 :(得分:0)

没有阅读所有代码,只是你突出显示的代码片段和logcat,甚至还发现了多个问题:

  • 当您尝试从generateData()致电onCreateView()时,约束失败。再次运行代码时,会重新插入相同的数据,导致约束失败失败。

  • 然后,在这里:

    activity = new Activity();
    tabel4data = (TableLayout) activity.findViewById(R.id.tabel_data);
    bhsjawa = (TextView) activity.findViewById(R.id.nama_id);
    bhsindo = (TextView) activity.findViewById(R.id.hobi_id);
    updateTable();
    

    您永远不应使用new实例化活动。现在,findViewById()所有null将返回NullPointerException并在updateTable()稍后为您{{1}}。

答案 2 :(得分:0)

您无法从活动实例中获取您的参考。它始终只来自ViewView类负责初始化和查找屏幕中的视图。因此,请始终按布局查找您的观看次数。

这是你的代码:

activity = new Activity();
tabel4data = (TableLayout) activity.findViewById(R.id.tabel_data);
bhsjawa = (TextView) activity.findViewById(R.id.nama_id);
bhsindo = (TextView) activity.findViewById(R.id.hobi_id);
updateTable();

更改如下。将您对activity的引用更改为rootView

  activity = new Activity();  
  tabel4data = (TableLayout) rootView .findViewById(R.id.tabel_data);
  bhsjawa = (TextView) rootView .findViewById(R.id.nama_id);
  bhsindo = (TextView) rootView .findViewById(R.id.hobi_id);
  updateTable();