0
私はアンドロイド開発には初めてです。 SQLiteから取得したデータをテーブルビューに表示しようとしています。私はMainとHelperAdapterという2つのクラスを作った。私は解決策を見つけるのに
分を費やします。しかし、私は有用なリソースを見つけられませんでした。SQLiteから取得したデータをandriodのテーブルビューに表示するには
Main.java
public class Main extends Activity implements View.OnClickListener {
private HelperAdapter hpAdp;
private Button btnCreate,btnRead,btnPost;
private LinearLayout llCreate,llRead;
private EditText ptTitle,ptContent;
private TextView tvRead;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hpAdp = new HelperAdapter(this);
btnCreate = (Button)findViewById(R.id.btnCreate);
btnRead = (Button)findViewById(R.id.btnRead);
btnPost = (Button)findViewById(R.id.btnPost);
llCreate = (LinearLayout)findViewById(R.id.llCreate);
llRead = (LinearLayout)findViewById(R.id.llRead);
ptTitle = (EditText)findViewById(R.id.ptTitle);
ptContent = (EditText)findViewById(R.id.ptContent);
tvRead = (TextView)findViewById(R.id.tvRead);
llCreate.setVisibility(View.VISIBLE);
llRead.setVisibility(View.INVISIBLE);
btnCreate.setOnClickListener(this);
btnRead.setOnClickListener(this);
btnPost.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnCreate:
llCreate.setVisibility(View.VISIBLE);
llRead.setVisibility(View.INVISIBLE);
break;
case R.id.btnRead:
llCreate.setVisibility(View.INVISIBLE);
llRead.setVisibility(View.VISIBLE);
loadData();
break;
case R.id.btnPost:
postData();
break;
}
}
private void postData() {
String title = ptTitle.getText().toString();
String content = ptContent.getText().toString();
hpAdp.dbOpen();
long id = hpAdp.postData(title, content);
hpAdp.dbClose();
if(id < 0) {
Toast.makeText(getApplicationContext(),"Successfully Post", Toast.LENGTH_LONG);
} else {
Toast.makeText(getApplicationContext(),"Fail Post", Toast.LENGTH_LONG);
}
}
private void loadData() {
hpAdp.dbOpen();
String str = hpAdp.loadData();
tvRead.setText(str);
hpAdp.dbClose();
}
}
HelperAdapter.java
public class HelperAdapter {
public SQLiteDatabase db;
public Context context;
public Helper hp;
public HelperAdapter(Context context) {
this.context = context;
}
public void dbOpen() {
Helper hp = new Helper(context);
db = hp.getWritableDatabase();
}
public long postData(String title, String content) {
ContentValues cv = new ContentValues();
cv.put(hp.PTITLE, title);
cv.put(hp.PCONTENT, content);
long id = db.insert(hp.TB_NAME,null,cv);
return id;
}
public String loadData() {
String[] columns = { hp. PID,hp.PTITLE,hp.PCONTENT };
Cursor c = db.query(hp.TB_NAME,columns,null,null,null,null,null);
StringBuilder sb = new StringBuilder();
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
int PID = c.getColumnIndex(hp.PID);
int PTT = c.getColumnIndex(hp.PTITLE);
int PCT = c.getColumnIndex(hp.PCONTENT);
sb.append("ID :"+ c.getString(PID) +"Title : "+c.getString(PTT) +"Content :"+c.getString(PCT));
}
return sb.toString();
}
public void dbClose() {
db.close();
}
public class Helper extends SQLiteOpenHelper {
private final static String DB_NAME = "blog";
private final static String TB_NAME = "posts";
private final static int DB_VERSION = 1;
private final static String PID = "id";
private final static String PTITLE = "title";
private final static String PCONTENT = "content";
public Helper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TB_NAME + " (" +
"" + PID + " INTEGER PRIMARY KEY AUTOINCREMENT ," +
"" + PTITLE + " VARCHAR(200) NOT NULL," +
"" + PCONTENT + " TEXT NOT NULL)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DELETE TABLE IF EXISTS " + TB_NAME + "";
db.execSQL(sql);
onCreate(db);
}
}
}
にデータを設定するには、この方法を試してみてくださいコードどこのデータベースから取得しているデータを設定しているのですか? –
解決済みですか? –