我有将问题转换为片段的问题,但“getBaseContext”和“this”的功能无法正常工作
package prj.apdev.c17;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class DataBase extends Fragment {
TempatDatabase dm;
EditText inama, idosis, ilabel;
Button btambah;
TableLayout tabel4data;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View va = inflater.inflate(R.layout.mapping_fragment, container, false);
return va;
dm = new TempatDatabase(this);
tabel4data = (TableLayout) va.findViewById (R.id.tabel_data);
inama = (EditText) va.findViewById(R.id.dose_nama_e);
idosis = (EditText) va.findViewById(R.id.dose_dosis_e);
ilabel = (EditText) va.findViewById(R.id.dose_label_e);
btambah = (Button) va.findViewById(R.id.button_add);
btambah.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
simpanData();
}
});
updateTable();
}
private void updateTable() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
while (tabel4data.getChildCount() > 1) {
tabel4data.removeViewAt(1);
}
double aa = tabel4data.getChildCount();
String a = String.valueOf(aa);
Toast.makeText(getBaseContext(), "tabel data child : " + a,
Toast.LENGTH_SHORT).show();
ArrayList<ArrayList<Object>> data = dm.ambilSemuaBaris();//
for (int posisi = 0; posisi < data.size(); posisi++) {
TableRow tabelBaris = new TableRow(this);
ArrayList<Object> baris = data.get(posisi);
TextView idTxt = new TextView(this);
idTxt.setText(baris.get(0).toString());
tabelBaris.addView(idTxt);
TextView namaTxt = new TextView(this);
namaTxt.setText(baris.get(1).toString());
tabelBaris.addView(namaTxt);
TextView dosisTxt = new TextView(this);
dosisTxt.setText(baris.get(2).toString());
tabelBaris.addView(dosisTxt);
TextView labelTxt = new TextView(this);
labelTxt.setText(baris.get(3).toString());
tabelBaris.addView(labelTxt);
tabel4data.addView(tabelBaris);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void simpanData() {
// TODO Auto-generated method stub
try {
dm.addRow(inama.getText().toString(), idosis.getText().toString(), ilabel.getText().toString());
Toast.makeText(getBaseContext(), inama.getText().toString() + ", berhasil disimpan",
Toast.LENGTH_SHORT).show();
updateTable();
kosongkanField();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "gagal simpan, " + e.toString(),
Toast.LENGTH_LONG).show();
}
}
private void kosongkanField() {
// TODO Auto-generated method stub
inama.setText("");
idosis.setText("");
ilabel.setText("");
}
}
这是我的数据库类
package prj.apdev.c17;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
public class TempatDatabase {
private static final String ROW_ID = "_id";
private static final String ROW_NAMA = "nama";
private static final String ROW_DOSIS = "dosis";
private static final String ROW_LABEL = "label";
private static final String NAMA_DB ="APdev";
private static final String NAMA_TABEL="Doseme";
private static final int DB_VERSION=1;
private static final String CREATE_TABLE =
"create table "+NAMA_TABEL+" ("+ROW_ID+" integer PRIMARY KEY autoincrement, "+ROW_NAMA+" text,"+ROW_DOSIS+" text, "+ROW_LABEL+" text)";
//membuat mendeklarasikan itu adalah context
private final Context context;
//membuat mendeklarasikan DatabaseOpenHelper itu adalah dbhelper
private DatabaseOpenHelper dbhelper;
//membuat mendeklarasikan SQLiteDatabase itu adalah db
private SQLiteDatabase db;
//mengambil context untuk mengakses system di android
public TempatDatabase(DataBase ctx) {
//mendeklarasikan ctx adalah context ( context context di ganti ctx )
this.context = ctx;
// membuat DatabaseOpenHelper
dbhelper = new DatabaseOpenHelper(context);
//menuliskan DatabaseOpenHelper = SQLiteDatabase
db = dbhelper.getWritableDatabase();
}
private static class DatabaseOpenHelper extends SQLiteOpenHelper {
//membuat database
public DatabaseOpenHelper(Context context) {
super(context, NAMA_DB, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE);
}
//memperbarui database bila sudah ada
@Override
public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+NAMA_DB);
onCreate(db);
}
}
//menutup DatabaseOpenHelper
public void close() {
dbhelper.close();
}
//menambahkan pada row
public void addRow(String nama, String dosis, String label) {
ContentValues values = new ContentValues();
values.put(ROW_NAMA, nama);
values.put(ROW_DOSIS, dosis);
values.put(ROW_LABEL, label);
try {
//menambahkan nama tabel bila tidak akan error
// db.delete(NAMA_TABEL, null, null);
db.insert(NAMA_TABEL, null, values);
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
//membuat array pada table layout
public ArrayList<ArrayList<Object>> ambilSemuaBaris() {
ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
Cursor cur;
try {
cur = db.query(NAMA_TABEL,
new String[] { ROW_ID, ROW_NAMA, ROW_DOSIS, ROW_LABEL }, 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));
dataList.add(cur.getString(3));
dataArray.add(dataList);
} while (cur.moveToNext());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("DEBE ERROR", e.toString());
}
return dataArray;
}
}
或者如果您有任何方法可以完成此条件,您可以将其包括在内。 感谢
答案 0 :(得分:0)
您可以使用getActivity()。getBaseContext,它返回与片段关联的活动。
答案 1 :(得分:0)
您的 TempatDatabase 构造函数中的上下文类型为 DataBase 。用上下文即
进行更改public TempatDatabase(Context ctx) {
//mendeklarasikan ctx adalah context ( context context di ganti ctx )
this.context = ctx;
// membuat DatabaseOpenHelper
dbhelper = new DatabaseOpenHelper(context);
//menuliskan DatabaseOpenHelper = SQLiteDatabase
db = dbhelper.getWritableDatabase();
}
并且片段 getActivity()方法将返回上下文
答案 2 :(得分:-1)
问题已解决
我已经意识到这个功能&#34;返回&#34;必须在部分的末尾
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View va = inflater.inflate(R.layout.dose_fragment, container, false);
dm = new TempatDatabase(getActivity());
tabel4data = (TableLayout) va.findViewById(R.id.tabel_data);
inama = (EditText) va.findViewById(R.id.dose_nama_e);
idosis = (EditText) va.findViewById(R.id.dose_dosis_e);
ilabel = (EditText) va.findViewById(R.id.dose_label_e);
btambah = (Button) va.findViewById(R.id.button_add);
btambah.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
simpanData();
}
});
updateTable();
return va;
}
&#13;
感谢帮助人们。