I am facing a problem with Android app.So. I have 3 activities that are opening one after another with OnItemClickListener. From first to second activity I have no trouble since I just have to load ArrayList in listview in the second activity.The third activity is opening when I click an item from the listview in second activity and in a third activity I want to open data from my database for the clicked item. Here is some code:
1.The second activity:
DatabaseHelper myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewlistcontents_layout);
ListView listView = (ListView) findViewById(R.id.listView);
myDB = new DatabaseHelper(this);
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDB.getListContents();
if(data.getCount() == 0){
Toast.makeText(this, "There are no contents in this list!",Toast.LENGTH_LONG).show();
}else{
while(data.moveToNext()){
theList.add(data.getString(1));
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
}
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ViewListContents.this, ElevatorInfo.class);
startActivity(intent);
}
});
}
2.Third activity where I want to get the opened item id from a database and load the third column from the database but have no idea how to do it and that is my actual question.Also, im doing the same thing as in second activity just to try to load the info from the third column in a listview but its also not working.
DatabaseHelper myDB;
@Override
ListView listView = (ListView) findViewById(R.id.listView);
myDB = new DatabaseHelper(this);
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDB.getListContents();
if (data.getCount() == 0) {
Toast.makeText(this, "There are no contents in this list!", Toast.LENGTH_LONG).show();
} else {
while (data.moveToNext()) {
theList.add(data.getString(2));
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
listView.setAdapter(listAdapter);
}
}
}
public void AddData(String newEntry) {
boolean insertData = myDB.addData2(newEntry);
if(insertData == true){
Toast.makeText(this, "Data Successfully Inserted!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Something went wrong :(.", Toast.LENGTH_LONG).show();
}
}
}
3.My DatabaseHelper class.
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + MyDatabase.ElevatorEntry.TABLE_NAME + " ( " + MyDatabase.ElevatorEntry.COL1 + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
MyDatabase.ElevatorEntry.COL2 + "TEXT" + MyDatabase.ElevatorEntry.DATE + " TEXT " + " )";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS " + MyDatabase.ElevatorEntry.TABLE_NAME);
onCreate(db);
}
public boolean addData(String item1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(MyDatabase.ElevatorEntry.COL2, item1);
long result = db.insert(MyDatabase.ElevatorEntry.TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
public boolean addData2(String item2) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(MyDatabase.ElevatorEntry.DATE, item2);
public Cursor getListContents(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + MyDatabase.ElevatorEntry.TABLE_NAME, null );
return data;
}
}