0

I am trying to click on a list entry. My List is a custom list view, with the data pulled from an ArrayAdapter. The list shows 3 parts of the Array entry, and a picture, but i want to send those items and the rest of the array entry to the next activity to show more information about the selected item.

Below is my Class with the Adapter in it. You will see a commented section /** **/ that i have been trying to figure out how to putExtra these items, but i just can't seem to figure it out. Each Book has 12 attributes to them, but only 4 are shown in the listView prior to clicking for more information.

I am using a Viewholder, so i am not sure i that is affecting me or not, but i do plan on having a fairly long list in the listview before the item click.

public class Novels extends ListActivity{

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

        setListAdapter(new NovelAdapter(this, R.layout.list_item, NovelsStore.getNovels()));

        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public Novels() {
    }

    public class NovelAdapter extends ArrayAdapter<Book> {

        LayoutInflater layoutInflater;

        public NovelAdapter(Context context, int textViewResourceId, List<Book> novels) {
            super(context, textViewResourceId, novels);
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent){
            ViewHolder holder;
            Book novels = getItem(position);

            // if no references in the view
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.list_item, null);

                // put them into a holder
                holder = new ViewHolder();

                holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
                holder.tvSeries = (TextView) convertView.findViewById(R.id.tvSeries);
                holder.tvEra = (TextView) convertView.findViewById(R.id.tvEra);
                holder.ivBookImage = (ImageView) convertView.findViewById(R.id.ivBookImage);

                //set the view tag
                convertView.setTag(holder);
            } else {
                // in case we have a recycled view, just get the Tag
                holder = (ViewHolder) convertView.getTag();
            }

            //Assign Property to the TextViews
            holder.tvTitle.setText(novels.getTitle());
            holder.tvSeries.setText(novels.getSeries());
            holder.tvEra.setText(novels.getEra());
            holder.ivBookImage.setImageResource(novels.getBookImage());

            return convertView;
        }

        public class ViewHolder {
            public TextView tvTitle;
            public TextView tvSeries;
            public TextView tvEra;
            public ImageView ivBookImage;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // If app icon in the action bar gets clicked; go back
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

Any help on this would be great!

jww
  • 97,681
  • 90
  • 411
  • 885
rogue5_
  • 13
  • 2

1 Answers1

0

You actually can send the Book object at the position if you can make Book to serializable, you can put the whole object into the Intent. Something like this

@Override 
protected void onListItemClick(ListView listView, View view, int position, long id) { 
    super.onListItemClick(listView, view, position, id); 

    Intent i = new Intent(getApplicationContext(), SingleItemView.class); 
    i.putExtra("my_book", NovelsStore.getNovels().get(position)); 
    startActivity(i); 
}

And You can get that object in your SingleItemView like below,

Book b = getIntent().getExtra("my_book");

You should do null check though.

Ye Lin Aung
  • 11,234
  • 8
  • 45
  • 51
  • So if this puts all the items for that row in the array into "my_book" how do i separate them in the SingleItemView.class? if (b == null) { item1=?? item2=?? } or something like that? – rogue5_ Sep 25 '14 at 02:05
  • So i am getting values b/c i was able to use the following code to verify my "my_book" had something in it. ---------------- tv = (TextView) findViewById(R.id.iv_tvTitle); Intent myIntent = getIntent(); Bundle b = myIntent.getExtras(); if (b == null) { tv.setText("Null Values"); } else { tv.setText("Values"); } ----------- but as i asked in the previous comment, how do i break this out into the 12 attributes of the array item? Thanks! – rogue5_ Sep 25 '14 at 02:15
  • I am passing a book object not an array of book objects. You can get the attributes like `b.getTitle()` , `b.getSeries()` – Ye Lin Aung Sep 25 '14 at 07:11