简体   繁体   中英

How to pass value from listview to the next activity

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM