2014-11-28 2 views
-2

목록보기 항목을 클릭하면 데이터베이스의 해당 데이터가 두 번째 활동에 표시되어야합니다. textview 대답이 있지만 한 가지 문제가 있습니다. 각 범주의 마지막 데이터 만 가져옵니다. 특정 카테고리의 모든 데이터가 필요합니다.sqlitedatabase에서 데이터 가져 오기

이 문제는이 두 번째 활성

Bundle bundle = getIntent().getExtras(); 
String message = bundle.getString("title"); 
String message1 = bundle.getString("title1"); 

TextView txtView = (TextView) findViewById(R.id.textView1);  
txtView.setText(message); 

TextView txtView1 = (TextView) findViewById(R.id.TextView2);  
txtView1.setText(message1); 

이 내 코드 주요 활동 방법

public class MainActivity extends ActionBarActivity { 

DatabaseHelper dbHeplper; 

ListAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    dbHeplper = new DatabaseHelper(getApplicationContext()); 
    try { 
     dbHeplper.createDataBase(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    ListView lvUsers= (ListView)findViewById(R.id.lvUsers); 
    final List<String> listUsers = dbHeplper.getAllUsers(); 

    if(listUsers != null){ 
     adapter = new ArrayAdapter<String>(getApplicationContext(), 
       android.R.layout.simple_list_item_1, android.R.id.text1, 
       listUsers); 
     lvUsers.setAdapter(adapter); 


    } 
    lvUsers.setOnItemClickListener(new OnItemClickListener() { 

     @Override 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

       String Desc =(String) dbHeplper.getPostDesc(listUsers.get(position)); 
       String title1 =(String) dbHeplper.getPostdesc(listUsers.get(position)); 

       Toast.makeText(getApplicationContext(), 
        "Desc is: " + Desc, Toast.LENGTH_LONG) 
        .show(); 
       Toast.makeText(getApplicationContext(), 
          "Desc is: " + title1, Toast.LENGTH_LONG) 
          .show(); 
       Intent newActivity0 = new Intent(MainActivity.this, Second.class); 
       newActivity0.putExtra("title", Desc); 
       newActivity0.putExtra("title1", title1); 
       startActivity(newActivity0); 
       } 
       }); 


} 

내 코드 데이터베이스

public class DatabaseHelper extends SQLiteOpenHelper { 

public static String DB_PATH = "/data/data/com.example.freshdatabase/databases/"; 
public static String DB_NAME = "Android.sqlite"; 
public static final int DB_VERSION = 1; 

public static final String TB_USER = "Users"; 
private SQLiteDatabase myDB; 
private Context context; 

public DatabaseHelper(Context context) { 
    super(context, DB_NAME, null, DB_VERSION);  
    this.context = context; 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 

} 

@Override 
public synchronized void close(){ 
    if(myDB!=null){ 
     myDB.close(); 
    } 
    super.close(); 

} 
private boolean checkDataBase() { 
    SQLiteDatabase tempDB = null; 
    try { 
     String myPath = DB_PATH + DB_NAME; 
     tempDB = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READWRITE); 
    } catch (SQLiteException e) { 
     Log.e("tle99 - check", e.getMessage()); 
    } 
    if (tempDB != null) 
     tempDB.close(); 
    return tempDB != null ? true : false; 
} 
public void copyDataBase() throws IOException{ 
    try { 
     InputStream myInput = context.getAssets().open(DB_NAME); 
     String outputFileName = DB_PATH + DB_NAME; 
     OutputStream myOutput = new FileOutputStream(outputFileName); 

     byte[] buffer = new byte[1024]; 
     int length; 

     while((length = myInput.read(buffer))>0){ 
      myOutput.write(buffer, 0, length); 
     } 

     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 
    } catch (Exception e) { 
     Log.e("tle99 - copyDatabase", e.getMessage()); 
    } 

} 
public void openDataBase() throws SQLException{ 
    String myPath = DB_PATH + DB_NAME; 
    myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
} 
public void createDataBase() throws IOException { 
    boolean dbExist = checkDataBase();   

    if (dbExist) { 

    } else { 
     this.getReadableDatabase(); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      Log.e("tle99 - create", e.getMessage()); 
     } 
    } 

} 

public List<String> getAllUsers(){ 
    List<String> listUsers = new ArrayList<String>(); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor c; 

    try { 
     c = db.rawQuery("SELECT * FROM " + TB_USER , null); 
     if(c == null) return null; 
     String id; 
     String name ; 
     c.moveToFirst(); 
     do { 
      name = c.getString(1);    
      listUsers.add(name); 
     } while (c.moveToNext()); 
     c.close(); 
    } catch (Exception e) { 
     Log.e("tle99", e.getMessage()); 
    } 

    db.close();   

    return listUsers; 
} 
public String getPostDesc(String Name){ 

String desc = ""; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor c; 

    try { 
     c = db.rawQuery("Select * from Post INNER JOIN Users ON Users.ID=Post.ID WHERE NAME='"+Name+"'" , null); 

     if(c == null) return null; 
     while (c.moveToNext()) { 
      desc=c.getString(1); 

     System.out.println(desc); 

     } 
     c.close(); 

    } catch (Exception e) { 
     Log.e("tle99", e.getMessage()); 
    } 

    db.close(); 
    return desc;   



    } 

public String getPostdesc(String name){ 
    String title=""; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor c; 

    try { 
     c = db.rawQuery("Select * from Post INNER JOIN Users ON Users.ID=Post.ID WHERE NAME='"+name+"'" , null); 

     if(c == null) return null; 


     if(c.moveToFirst()){ 
      title= c.getString(2); 

     } 

      { 
} 
      c.close(); 
    } catch (Exception e) { 
     Log.e("tle99", e.getMessage()); 
    } 

    db.close();   

    return title; 
    } 
} 

로부터 데이터를 검색하기위한 제 코드 내 데이터베이스 테이블입니다

enter image description here

+0

무엇이'listU sers'? –

+0

왜 1 차 데이터베이스에서 충돌 ID를 사용 했습니까 ?? –

+0

Desc는 무엇을 의미합니까? 제목은 무엇입니까? 내가 틀리면'getPostDesc'를 호출합니다. 왜 같은 사용자에 대해 다른 값을 반환합니까? 코드가 명확하지 않습니다. –

답변

0

enter image description here

는 내가 .. 이런 식으로 DB에서 데이터를 불러 오는에 대한

public ArrayList<String> getPostname(String Name) { 

    ArrayList<String> List = new ArrayList<String>(); 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor c = db.rawQuery("Select * from Post INNER JOIN Users ON Users.ID=Post.ID WHERE NAME='"+Name+"'" , null);; 

    try { 
     if (c != null) { 
      if (c.moveToFirst()) { 

       do { 
          String levelData = c.getString(c.getColumnIndex("postname")); 
        List.add("" + levelData); 

         } 
          while (c.moveToNext()); 
        } 
       } 
      } catch (SQLiteException e) { 

        Log.e("Retrieve Data", "Unable to get Data " + e); 
      } 

      return List; 
     } 

을 내 코드를 변경하고 지금

public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      ArrayList<String> post_name = new ArrayList<String>(); 
      ArrayList<String> post_desc = new ArrayList<String>(); 

      post_name =(ArrayList<String>) dbHeplper.getPostname(listUsers.get(position)); 
      post_desc =(ArrayList<String>) dbHeplper.getPostdesc(listUsers.get(position)); 

와의 onclick에있는 그것의 완벽 ..

작업