Note that there are some explanatory texts on larger screens.

plurals
  1. POListView with custom ArrayAdapter and custom object
    text
    copied!<p>Here are the background details of my app:</p> <ul> <li>Using a FragmentPagerAdapter in my main activity to show couple of different fragments.</li> <li>For one of my fragments, I am using a ListView that is populated by a custom ArrayAdapter that is using a custom object class, in this case "Deal".</li> <li>Using an ArrayList passed into the custom ArrayAdapter.</li> </ul> <p>What I basically am stuck on: <strong>when I click any "deal" on the ListView fragment page, I want to go to another activity and pass in the info from the specific Deal object. So, if I click Deal 1, I want to pass in the Deal 1 object to the new activity for it's info. If I click on Deal 2, I want to pass in the Deal 2 object to the new activity for it's info. I am unsure what to put in the onItemClickListener.</strong></p> <p>Please note, the ArrayList will get it's object from an external source later, just adding in the test subjects for now. Will I need to use the ArrayAdapter to pass in the information to the new activity page?</p> <p><strong>DealsFragment</strong></p> <pre><code>public class DealsFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_show_deals, container, false); ListView listView = (ListView)view.findViewById(R.id.dealsListView); // Sample set of data passed to adapter for testing purposes ArrayList&lt;Deal&gt; all_deals = new ArrayList&lt;Deal&gt;(); all_deals.add(new Deal("Deal 1", R.drawable.test_image, 389, 700, 750, 500)); all_deals.add(new Deal("Deal 2", R.drawable.test_image, 20, 80, 1800, 1500)); all_deals.add(new Deal("Deal 3", R.drawable.test_image, 1932, 2000, 75, 60)); all_deals.add(new Deal("Deal 4", R.drawable.test_image, 198, 450, 450, 350)); all_deals.add(new Deal("Deal 5", R.drawable.test_image, 60, 70, 1500, 1100)); // Sets up adapter to pass data into XML DealAdapter adapter = new DealAdapter(getActivity(), R.layout.listview_item_row, all_deals); // TO ADD HEADER ROW BACK IN // View header = (View)inflater.inflate(R.layout.listview_header_row, null); // LV.addHeaderView(header); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { Intent intent = new Intent(getActivity(), DealPage.class); startActivity(intent); } }); return view; } } </code></pre> <p><strong>DealAdapter</strong></p> <pre><code>public class DealAdapter extends ArrayAdapter&lt;Deal&gt; { Context context; int layoutResourceId; ArrayList&lt;Deal&gt; data = null; public DealAdapter(Context context, int layoutResourceId, ArrayList&lt;Deal&gt; data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } static class ViewHolder { ImageView imgDealImage; TextView txtDescription; TextView txtSupporters; TextView txtRegularPrice; TextView txtDiscountPrice; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); convertView = inflater.inflate(layoutResourceId, parent, false); holder = new ViewHolder(); holder.imgDealImage = (ImageView)convertView.findViewById(R.id.imgDealImage); holder.txtDescription = (TextView)convertView.findViewById(R.id.txtDescription); holder.txtSupporters = (TextView)convertView.findViewById(R.id.txtSupporters); holder.txtRegularPrice = (TextView)convertView.findViewById(R.id.txtRegularPrice); holder.txtDiscountPrice = (TextView)convertView.findViewById(R.id.txtDiscountPrice); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } int image = data.get(position).getImage(); String description = data.get(position).getDescription(); int currentSupporters = data.get(position).getCurrentSupporters(); int maxSupporters = data.get(position).getMaxSupporters(); int regularPrice = data.get(position).getRegularPrice(); int discountPrice = data.get(position).getDiscountPrice(); holder.imgDealImage.setImageResource(image); holder.txtDescription.setText(description); holder.txtSupporters.setText(String.valueOf(currentSupporters + " / " + maxSupporters + " Supporters")); holder.txtRegularPrice.setText(String.valueOf("$" + regularPrice)); holder.txtRegularPrice.setPaintFlags(holder.txtRegularPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); holder.txtDiscountPrice.setText(String.valueOf("$" + discountPrice)); return convertView; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload