在我的项目中有三个java类它们是:MainActivity.java,Activity2.java和DataBaseHelper.java。使用两个XML布局。现在我想把数据从mainactivity传递到Activity2,当我点击activity_main布局的listtiitem,这将显示在act2布局的TextViews上。在这里,我使用了一个sqlitedatase与ID, NAME和DESC作为列名,我的问题是,一旦我点击每个列表视图,我就无法在TextView上显示NAME和DESC,我只能显示ID。为了解决这个问题,我已经搜索了几天了,但我没有得到任何东西。我知道这是一个愚蠢而简单的问题,但如果你能分享任何信息和帮助,那将是非常伟大的。谢谢你!我使用了下面的代码:
MainActivity.java:
public final static String EXTRA_NAME="com.example.verena.NAME";
public final static String EXTRA_DESC="com.example.verena.DESC";
private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
private AdapterView listView;
public AdapterView getListView() {
return listView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* TextView viewName = (TextView)view.findViewById(R.id.passed);
TextView viewDes = (TextView)view.findViewById(R.id.passeddes);
String sName = viewName.getText().toString();
String sDesc = viewDes.getText().toString();
i.putExtra(EXTRA_NAME,sName);
i.putExtra(EXTRA_DESC, sDesc);*/
//2detail
Intent i = new Intent(MainActivity.this, Activity2.class);
i.putExtra(EXTRA_NAME, String.valueOf(id));
//
Toast.makeText(getApplicationContext(), "Saved Data", Toast.LENGTH_LONG).show();
int pos = this.getListView().getSelectedItemPosition();
Cursor c = (Cursor) this.getListView().getAdapter().getItem(pos);
Bundle b = new Bundle();
b.putString(EXTRA_DESC, c.getString(0));
b.putString(EXTRA_NAME, c.getString(1));
i.setClass(MainActivity.this, Activity2.class);
i.putExtras(b);
/* TextView desv= (TextView)findViewById(R.id.passeddes);
String desS = desv.getText().toString();
i.putExtra(EXTRA_DESC, desS);*/
/* TextView viewDes = (TextView)view.findViewById(R.id.passeddes);
String sDesc = viewDes.getText().toString();
i.putExtra(EXTRA_DESC, sDesc);*/
/*TextView viewDes = (TextView)view.findViewById(R.id.passeddes);
String sDesc = viewDes.getText().toString();
i.putExtra(EXTRA_DESC, sDesc);*/
startActivity(i);
}
Activity2.java
String passedVar=null;
private TextView passedNameView = null;
String passedVardes = null;
private TextView passedViewDescView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
passedVar = getIntent().getStringExtra(MainActivity.EXTRA_NAME);
passedVardes= getIntent().getStringExtra(MainActivity.EXTRA_DESC);
passedView=(TextView)findViewById(R.id.passed);
passedViewDesc=(TextView)findViewById(R.id.passeddes);
passedNameView.setText("you have click on:" + passedVar);
passedViewDescView.setText("you hv click on:"+passedVardes);
}
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static String DB_PATH = "/data/data/com.example.verena/databases/";
public static String DB_NAME = "dic.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;
}
/***
* Open database
* @throws SQLException
*/
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
//myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
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());
}
}
/***
* Check if the database doesn't exist on device, create new one
* @throws IOException
*/
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 METHODS
// ---------------------------------------------
/***
* Check if the database is exist on device or not
* @return
*/
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;
}
}
将id作为字符串、名称和描述传递到一个bundle中。你需要从intent中获取bundle并从那个bundle中获取你的名字和名称。另一种方法是,您可以将id、name和desc作为字符串传递,如下所示:
在你的MainActivity.java
:
// New Intent
Intent i = new Intent(MainActivity.this, Activity2.class);
// Put Extras
// Put your id
i.putExtra("id", String.valueOf(id));
int pos = this.getListView().getSelectedItemPosition();
Cursor c = (Cursor) this.getListView().getAdapter().getItem(pos);
// Put Name
i.putExtra(EXTRA_NAME, c.getString(0));
// Put Description
i.putExtra(EXTRA_DESC, c.getString(1));
i.setClass(MainActivity.this, Activity2.class);
startActivity(i);
要在Activity2.java
中获得id
,请在onCreate
方法中放入以下行:
String passedVarid = getIntent().getStringExtra("id");