-1

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

}
Arghavan
  • 1,125
  • 1
  • 11
  • 17
Goshoy
  • 1
  • 4
  • i changed the type several times,tried even other methods of doing it but i cant get it to work.The main target is to open(in third activity) just the info for the item selected in the second activity from the listview.Thank you anyways! – Goshoy Jul 04 '17 at 10:59

1 Answers1

1

you have wrote a lot of code in the question, stay focused on what you want to ask about

side note: why this

ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);

is in the while loop? this should be called once, when the loop ends and all the items have been added to the list theList

while(data.moveToNext()){
    theList.add(data.getString(1));
}//while loop
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);

Now, for your question, at onItemClick() use position to get the item from theList then do whatever you want with it:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String selected = theList.get(position);
    Intent intent = new Intent(ViewListContents.this, ElevatorInfo.class);
    intent.putExtra("SELECTED_ITEM",selected);
    startActivity(intent);
}

now you can add selected as an extra in the intent and start the next activity.

EDIT: For how to read the value from the 2nd activity. check this answer you need to use getIntent().getStringExtra("SELECTED_ITEM");.

Yazan
  • 6,074
  • 1
  • 19
  • 33
  • Okay i made this with the while loop and its fine.But i do not get how to use "selected" in the next class that is being opened, because i need it there to load the info for the clicked item.Thank you.Appreciated! – Goshoy Jul 04 '17 at 11:36
  • @Goshoy i haved updated the answer with a link to show how to read the value in next activity – Yazan Jul 04 '17 at 11:38
  • I am sorry..One more thing.This way i am getting whats in column 2, actually the name of the product, and in the last activity i want to display the info from column 3 with the same index as the clicked item. – Goshoy Jul 04 '17 at 11:49
  • change the **1** in this `data.getString(1)` to the column you wish to fetch it's content (try 2) – Yazan Jul 04 '17 at 12:38
  • Yes I tried this but it just shows the word I get from column 2 as many times as the number of items in the database. – Goshoy Jul 04 '17 at 14:28