0

I'm new to android studios and am currently about to finish up on my first project. However, I'm stuck at the part where I have to pass the value selected from the listview to the next activity. I have tried researching on the codes but none seem to be able to work. Any help would be greatly appreciated.

DisplayProduct.java

public class DisplayProduct extends AppCompatActivity {

ListView listView;
SQLiteDatabase sqLiteDatabase;
DatabaseHelper databaseHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;

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

    listView = (ListView)findViewById(R.id.listView);
    listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.display_product_row);
    listView.setAdapter(listDataAdapter);

    databaseHelper = new DatabaseHelper(getApplicationContext());

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            //Object data = parent.getItemAtPosition(position);
            //String value = data.toString();



        }
    });
    sqLiteDatabase = databaseHelper.getReadableDatabase();
    cursor = databaseHelper.getInformations(sqLiteDatabase);
    if(cursor.moveToFirst())
    {
        do {

            String contact,location,issue;
            contact = cursor.getString(0);
            location = cursor.getString(1);
            issue = cursor.getString(2);
            Information information = new Information(contact,location,issue);
            listDataAdapter.add(information);


        } while (cursor.moveToNext());
    }
}

}

ListDataAdapter.java

public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
    super(context, resource);
}

static class LayoutHandler
{
    TextView Contact,Location,Issue;
}

@Override
public void add(Object object) {
    super.add(object);
    list.add(object);

}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    LayoutHandler layoutHandler;
    if (row == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.display_product_row, parent, false);
        layoutHandler = new LayoutHandler();
        layoutHandler.Contact = (TextView) row.findViewById(R.id.textView8);
        layoutHandler.Location = (TextView) row.findViewById(R.id.textView18);
        layoutHandler.Issue = (TextView) row.findViewById(R.id.textView90);
        row.setTag(layoutHandler);

    } else {
        layoutHandler = (LayoutHandler) row.getTag();


    }

    Information information = (Information) this.getItem(position);
    layoutHandler.Contact.setText(information.getContact());
    layoutHandler.Location.setText(information.getLocation());
    layoutHandler.Issue.setText(information.getIssue());

    return row;





}

}

LocationDetail.java

public class LocationDetail extends AppCompatActivity {


private TextView Textv;
DatabaseHelper databaseHelper;
SQLiteDatabase sqlitedatabase;

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

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Cedric Woo
  • 39
  • 6
  • 3
    Possible duplicate of [How to pass an object from one activity to another on Android](http://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android) – petey Feb 17 '17 at 14:52
  • just use Intent and put the data what you want to send using Intent extra.. – ZeroOne Feb 17 '17 at 15:27

1 Answers1

0

From DisplayProduct.java :

Intent intent = new Intent(this, LocationDetail.class);
    Object data = parent.getItemAtPosition(position);
    String message = data.toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);

From LocationDetail.java:

Intent intent = getIntent();
    String value = intent.getStringExtra(EXTRA_MESSAGE);
Cedric Franck
  • 526
  • 4
  • 9
  • Hey there, thanks for the reply. May i ask how do you verify the editText. I tried keying that in but can't resolve the symbol. – Cedric Woo Feb 17 '17 at 15:13
  • Okay, i have just tried it. There was no error whatsoever but the values still doesn't appear in the LocationDetail.java. Any idea why this might be the case? – Cedric Woo Feb 17 '17 at 15:28
  • Maybe you should use getItem(position) instead of getItemAtPosition(position) – Cedric Franck Feb 17 '17 at 15:45