Android - pass values from clicked item on ListView and display them on a new Actvity

kmub

New Member
#1
I am an intern at this company and I have a task assigned to me by my team leader where I need to make an app that displays a list of items that I can add to and edit/delete the items on the list. I am following the requirements given to me on what the app needs to do.
The problem I'm having is that I need to pass values from an item when clicked on a ListViewwhich uses a custom adapter, and have them sent to a new activity and displayed on the new activity's textviews and imageview. I have tried using putExtras() methods in the list click method and getting the values using getExtras() methods but they didn't work and I've already deleted those codes, so they are no longer there. If you need more of the classes/activities I'm using please let me know. I am using Android Studio 3.1.4
ItemListView.java

Mã:
public class ItemListView extends AppCompatActivity {

DatabaseHelper myDB;

ArrayList<Item> itemList;
ListView listView;

Item item;

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

    listView = (ListView) findViewById(R.id.listView);
    myDB = new DatabaseHelper(this);
    itemList = new ArrayList<>();
    Cursor data = myDB.getListContents();

    int numRows = data.getCount();
    if(numRows == 0){
        Toast.makeText(ItemListView.this, "There is nothing in the database.", Toast.LENGTH_LONG).show();
    } else {
        while(data.moveToNext()){
            item = new Item(data.getString(1), data.getString(2), data.getString(3));
            itemList.add(item);
        }
        Row_ListAdapter adapter = new Row_ListAdapter(this, R.layout.list_adapter_view, itemList);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);

            startActivity(intent);
        }
    });
}
ViewItemClicked.java I want the values displayed onto the layout of this activity when a row is clicked.
Mã:
public class ViewItemClicked extends AppCompatActivity {

    ImageView image;
    TextView name, desc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_item_clicked);
    }
}
Other classes I used:
Row_ListAdapter.java
Mã:
public class Row_ListAdapter extends ArrayAdapter<Item> {

private LayoutInflater mInflater;
private ArrayList<Item> items;
private int mViewResourceId;
ImageView image;
TextView name;
TextView description;

public Row_ListAdapter(Context context, int textViewResourceId, ArrayList<Item> items){
    super(context, textViewResourceId, items);
    this.items = items;

    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mViewResourceId = textViewResourceId;
}

public View getView(int position, View convertView, ViewGroup parents){

    convertView = mInflater.inflate(mViewResourceId, null);

    Item item = items.get(position);

    if(item != null){

        image = (ImageView) convertView.findViewById(R.id.iconIV);
        name = (TextView) convertView.findViewById(R.id.nameTV);
        description = (TextView) convertView.findViewById(R.id.descTV);

        if(image != null){
            image.setImageBitmap(item.getImage());
        }
        if(name != null){
            name.setText(item.getName());
        }
        if(description != null){
            description.setText(item.getDescription());
        }
    }
    return convertView;
}
}
Link to GUI:
 

Admin

Administrator
Thành viên BQT
#2
Before you pass the data, implement your Item Class with Serializable like this:
Mã:
public class Item implements Serializable{
     /*your class code here*/
}
Then pass the data in the listView.setOnItemClicklistener like this
Mã:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Item passItem = itemList.get(position);
            Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);
            Bundle itemBundle = new Bundle();
            itemBundle.putSerializable("dataItem",passItem)// put the data and define the key
            intent.putExtras(itemBundle)
            startActivity(intent);
        }
    });
and to open the data in the ViewItemClicked.Class
Mã:
public class ViewItemClicked extends AppCompatActivity {

    ImageView image;
    TextView name, desc;
    Item item;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        item = (Item) getIntent().getSerializableExtra("dataItem"); // use the key
        setContentView(R.layout.activity_view_item_clicked);
        /*now u can use the item data*/
    }
}
 
OP
OP
K

kmub

New Member
#3
Thank you, your codes worked! I had to switch itemBundle.putSerializable(passItem,"dataItem") to itemBundle.putSerializable("dataItem", passItem), and added the missing codes and it ran
 
Top