2017-06-22 14 views
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); 
     } 
    } 
} 
+0

にデータを設定するには、この方法を試してみてくださいコードどこのデータベースから取得しているデータを設定しているのですか? –

+0

解決済みですか? –

答えて

0

テーブルビュー

あなたのテーブルビューは、あなたの中にある
public class MainActivity extends Activity { 
    private Context context; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     context=this; 
     // Create DatabaseHelper instance 
     DatabaseHelper dataHelper=new DatabaseHelper(context); 
    // Insert sample data 
     dataHelper.insertData("Kent","HoReCa"); 
     dataHelper.insertData("FineE","HoReCa"); 
     dataHelper.insertData("MKent","GTrade"); 
     dataHelper.insertData("MeviusLove","MTrade"); 
     dataHelper.insertData("XEMo","HoReCa"); 
     // Reference to TableLayout 
     TableLayout tableLayout=(TableLayout)findViewById(R.id.tablelayout); 
     // Add header row 
     TableRow rowHeader = new TableRow(context); 
     rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0")); 
     rowHeader.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 
      TableLayout.LayoutParams.WRAP_CONTENT)); 
     String[] headerText={"ID","NAME","TYPE"}; 
     for(String c:headerText) { 
     TextView tv = new TextView(this); 
     tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 
     tv.setGravity(Gravity.CENTER); 
     tv.setTextSize(18); 
     tv.setPadding(5, 5, 5, 5); 
     tv.setText(c); 
     rowHeader.addView(tv); 
     } 
     tableLayout.addView(rowHeader); 

     // Get data from sqlite database and add them to the table 
     // Open the database for reading 
     SQLiteDatabase db = dataHelper.getReadableDatabase(); 
     // Start the transaction. 
     db.beginTransaction(); 

     try 
     { 
     String selectQuery = "SELECT * FROM "+ DatabaseHelper.TABLE_OUTLET; 
     Cursor cursor = db.rawQuery(selectQuery,null); 
     if(cursor.getCount() >0) 
     { 
      while (cursor.moveToNext()) { 
       // Read columns data 
       int outlet_id= cursor.getInt(cursor.getColumnIndex("outlet_id")); 
       String outlet_name= cursor.getString(cursor.getColumnIndex("outlet_name")); 
       String outlet_type= cursor.getString(cursor.getColumnIndex("outlet_type")); 

       // dara rows 
       TableRow row = new TableRow(context); 
       row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 
        TableLayout.LayoutParams.WRAP_CONTENT)); 
       String[] colText={outlet_id+"",outlet_name,outlet_type}; 
       for(String text:colText) { 
       TextView tv = new TextView(this); 
       tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 
         TableRow.LayoutParams.WRAP_CONTENT)); 
       tv.setGravity(Gravity.CENTER); 
       tv.setTextSize(16); 
       tv.setPadding(5, 5, 5, 5); 
       tv.setText(text); 
       row.addView(tv); 
       } 
       tableLayout.addView(row); 

      } 

     } 
     db.setTransactionSuccessful(); 

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

     } 
     finally 
     { 
     db.endTransaction(); 
     // End the transaction. 
     db.close(); 
     // Close database 
     } 
} 

http://www.worldbestlearningcenter.com/tips/Android-sqlite-table-layout-example.htm

関連する問題