2014-04-21 9 views
-2

onCreate 함수에서 테이블 (학생)이있는 데이터베이스 (mydb)를 만든 다음 button click.Now를 사용하여 값을 동적으로 입력했습니다. 테이블에있는 학생이 버튼을 클릭하여 listview에 표시하십시오.SQLite 데이터베이스에서 데이터 가져 오기 및 ListView에 표시 --android

public class MainActivity extends Activity   
     { 

     String name, phone; 
      SQLiteDatabase db; 

      @Override 
      protected void onCreate(Bundle savedInstanceState) 
      { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
       db=openOrCreateDatabase("mydb", MODE_PRIVATE, null); 
       db.execSQL("CREATE TABLE IF NOT EXISTS student(dbname VARCHAR, dbphone VARCHAR);"); 

      } 

      public void btnaddtodb(View v) 
      { 
       EditText edtxtname = (EditText)findViewById(R.id.edtxtname); 
       EditText edtxtphone = (EditText)findViewById(R.id.edtxtphone); 

       name=edtxtname.getText().toString(); 
       phone=edtxtphone.getText().toString(); 

       db.execSQL("INSERT INTO student values('"+name+"','"+phone+"');"); 

       edtxtname.setText(""); 
       edtxtphone.setText(""); 

       } 
      } 
+0

당신은 어떤 자습서를 시도 :

가장 간단한 예제 5 월은 다음과 같습니다? – GrIsHu

+1

지금까지 무엇을 했습니까? 문제/오류는 무엇입니까? –

+0

시도해 보았습니다 .. bt got confused – danish

답변

0

..

try{ 
     mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null); 
     Cursor allrows = mydb.rawQuery("SELECT * FROM "+ TABLE, null); 
     System.out.println("COUNT : " + allrows.getCount()); 
     Integer cindex = allrows.getColumnIndex("BOOK_DATE"); 
     Integer cindex1 = allrows.getColumnIndex("TRIP_DATE"); 
     Integer cindex2 = allrows.getColumnIndex("LOCATION"); 

     TextView t = new TextView(MybookingsActivity.this); 
     t.setText("========================================"); 
     //Linear.removeAllViews(); 
     Linear.addView(t); 

     if(allrows.moveToFirst()){ 
      do{ 
       LinearLayout id_row = new LinearLayout(MybookingsActivity.this); 
       LinearLayout book_date_row = new LinearLayout(MybookingsActivity.this); 
       LinearLayout trip_date_row= new LinearLayout(MybookingsActivity.this); 
       LinearLayout location_row= new LinearLayout(MybookingsActivity.this); 
       LinearLayout feedback_row= new LinearLayout(MybookingsActivity.this); 

       final TextView id_ = new TextView(MybookingsActivity.this); 
       final TextView book_date = new TextView(MybookingsActivity.this); 
       final TextView trip_date = new TextView(MybookingsActivity.this); 
       final TextView location = new TextView(MybookingsActivity.this); 
       final TextView sep = new TextView(MybookingsActivity.this); 
       final Button feedback = new Button(MybookingsActivity.this); 

       final String ID = allrows.getString(0); 
       String BOOK_DATE= allrows.getString(1); 
       String TRIP_DATE= allrows.getString(2); 
       String LOCATION= allrows.getString(3); 

       id_.setTextColor(Color.RED); 
       id_.setPadding(20, 5, 0, 5); 

       book_date.setTextColor(Color.RED); 
       book_date.setPadding(20, 5, 0, 5); 
       trip_date.setTextColor(Color.RED); 
       trip_date.setPadding(20, 5, 0, 5); 
       location.setTextColor(Color.RED); 
       location.setPadding(20, 5, 0, 5); 

       System.out.println("BOOK_DATE " + allrows.getString(cindex) + " TRIP_DATE : "+ allrows.getString(cindex1)+ " LOCATION : "+ allrows.getString(cindex2)); 
       System.out.println("ID : "+ ID + " || BOOK_DATE " + BOOK_DATE + "|| TRIP_DATE : "+ TRIP_DATE+ "|| LOCATION : "+LOCATION); 

       id_.setText("ID : " + ID); 
       id_row.addView(id_); 
       Linear.addView(id_row); 
       book_date.setText("BOOK_DATE : "+BOOK_DATE); 
       book_date_row.addView(book_date); 
       Linear.addView(book_date_row); 
       trip_date.setText("TRIP_DATE : " + TRIP_DATE); 
       trip_date_row.addView(trip_date); 
       Linear.addView(trip_date_row); 
       location.setText("LOCATION : " + LOCATION); 
       location_row.addView(location); 
       Linear.addView(location_row); 
       feedback.setText("Feedback"); 
       feedback.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         Intent intent=new Intent(MybookingsActivity.this,FeedbackActivity.class); 
         intent.putExtra("id", ID); 
         startActivity(intent); 
        } 
       }); 
       feedback_row.addView(feedback); 
       Linear.addView(feedback_row); 
       sep.setText("---------------------------------------------------------------"); 
       Linear.addView(sep); 
      } 
      while(allrows.moveToNext()); 
     } 
     mydb.close(); 
    }catch(Exception e){ 
     Toast.makeText(getApplicationContext(), "Error encountered."+e.toString(), Toast.LENGTH_LONG).show(); 
    } 

가 it..Dont가 DBNAME, TABLENAME 및 fielnames을 변경하는 것을 잊지 시도 그것은 목록으로있는 LinearLayout에서 테이블의 모든 값을 보여줍니다 ...이 일을보십시오

1

일반적으로, 당신은 항상 목록보기에 뭔가를 보여주기 위해 ArrayAdapter를 사용할 수 있습니다. DB에서 뭔가를

, 기본 ArrayAdapter 외에, 당신은 또한 동적로드 및 자동 새로 고침 같은 몇 가지 추가 혜택을 가지고 CursorAdapter을 사용할 수 있습니다 (이것은 당신이 인터넷에서 찾을 수있는 다른 많은 것들에 대한 good tutorial있다).

CursorAdapter을 사용하려면 ActivityLoaderCallback<Cursor> 및 필수 콜백을 구현하십시오.

CursorAdapter을 초기화하고 ListView으로 설정하십시오.

CreateLoader(...) 메서드에서 필요한 것을 쿼리하십시오.

newViewbindView을 올바르게 구현해야합니다.

public class TestActivity extends Activity implements LoaderCallbacks<Cursor>{ 
     ListView listview; 
     CursorAdapter cursorAdapter; 



     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     cursorAdapter = new CursorAdapter(this, null) { 

      @Override 
      public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      LayoutInflater inflater = 
       (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View rowView; 
      rowView = inflater.inflate(R.layout.device_list_item, parent, false); 
      bindView(rowView, context, cursor); 
      return rowView; 
      } 

      @Override 
      public void bindView(View view, Context context, Cursor cursor) { 
      TextView textView = view.findViewById(R.id.text); 
      textView.setText(cursor.getString(0)); 

      } 
     }; 
     } 

     @Override 
     public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
     return db.query(...); 
     } 

     @Override 
     public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 
     cursorAdapter.swapCursor(data); 

     } 

     @Override 
     public void onLoaderReset(Loader<Cursor> loader) { 
     cursorAdapter.swapCursor(null); 

     } 

    } 
관련 문제