如何在活动中显示数据库?

时间:2015-09-11 21:20:00

标签: android mysql listview

我创建的应用程序包含三个活动和数据库以及两个表,第一个表在listview中修复,但是如何在showdb中设置第二个表,当点击项目listview在showdb活动中显示详细的itme

例如,

第一个表ID(1,2,3),nameitem(US,AU,IQ)。

第二个表ID(1,2,3),infoNI(US:详细...,AU:详细...,IQ:详细...)。

点击美国时,在showdb活动中显示信息美国,怎么做?!!!

SQLiteOpenHelper

public class DatabaseHelper extends SQLiteOpenHelper {

public static String DB_PATH = "/data/data/com.example.dbtest/databases/";
public static String DB_NAME = "ex4.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();
}

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 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 void openDataBase() throws SQLException{
    String myPath = DB_PATH + DB_NAME;
    myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}


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 createDataBase() throws IOException {
    boolean dbExist = checkDataBase();      

    if (dbExist) {

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


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;
}}

MainActivity

public class MainActivity extends Activity {

DatabaseHelper dbHeplper;
ListView lvUsers;
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();
    }

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

    if(listUsers != null){
        adapter = new ArrayAdapter<String>(getApplicationContext(),
                R.layout.row,R.id.textView1,
                listUsers);
        lvUsers.setAdapter(adapter);
    }
    lvUsers.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                            int position,long id) {
             Intent intent = new Intent(MainActivity.this, showdb.class);
                intent.putExtra("position",position);    
                startActivity(intent);
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}

showdbActivity

活动三showdb(在此活动中,显示文件数据库中的数据)。

public class showdbActivity extends Activity {

TextView tv;

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

tv1 = (TextView) findViewById(R.id.title);
tv2= (TextView) findViewById(R.id.detaile);

 }}

1 个答案:

答案 0 :(得分:0)

将您的代码更改为

lvUsers.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                            int position,long id) {
             Intent intent = new Intent(MainActivity.this, showdbActivity.class);
                intent.putExtra("position",listUsers.get(position));    
                startActivity(intent);
        }
    });

进入您的DBActivity.java,在onCreate()

Intent intent = getIntent();
    String user = intent.getStringExtra("position");
    if(user!=null){
        // fetch the corresponding deatils of user from database                
    }