In my project there three java classes they are: MainActivity.java,Activity2.java and DataBaseHelper.java. with two xml layouts. now i want to pass datas from MainAcitivity to Activity2 when i click on ListItem of activity_main layout and which is going to display on TextViews of act2 layout.Here i have used a sqliteDatabase with ID, NAME and DESC as a column name, my problem is i was not able to display NAME and DESC on TextView once i Clicked on each listview, i was only able to display ID. I have searched for few days for solving this problem but i didn't get anything. i know its a silly and easy question but it would be very great of you if you are able to share any information and help. Thank you! i have used below code:
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;
}
}