3

Here are the background details of my app:

  • Using a FragmentPagerAdapter in my main activity to show couple of different fragments.
  • 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".
  • Using an ArrayList passed into the custom ArrayAdapter.

What I basically am stuck on: 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.

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?

DealsFragment

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<Deal> all_deals = new ArrayList<Deal>();
    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<?> parent, View view, int position, long id) {

            Intent intent = new Intent(getActivity(), DealPage.class);
            startActivity(intent);
        }
    });

    return view;
}

}

DealAdapter

public class DealAdapter extends ArrayAdapter<Deal> {

Context context; 
int layoutResourceId;    
ArrayList<Deal> data = null;

public DealAdapter(Context context, int layoutResourceId, ArrayList<Deal> 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;
}

}
The Nomad
  • 6,385
  • 12
  • 58
  • 98

2 Answers2

1

Make your deal class as setter and getter. You are setting the new object. In an arraylist there is an option to get the object from position. So once you get the position, try to get the information from that selected deal object. You cannot pass an object, but can pass individual information. Get the individual information from the Deal object and pass as putExtra. Try with bundle class to pass all information in a bundle.

check this out for Bundle

Community
  • 1
  • 1
User007
  • 397
  • 1
  • 10
1

Tyr this..

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

            Deal deal = all_deals.get(position);

                     Intent intent = new Intent(getActivity(), Deal1.class);
                     Bundle bundle = new Bundle();
                     bundle.putInt("CurrentSupporters", deal.getCurrentSupporters());
                     bundle.putInt("MaxSupporters", deal.getMaxSupporters());
                     bundle.putInt("RegularPrice", deal.getRegularPrice());
                     bundle.putInt("DiscountPrice", deal.getDiscountPrice());
                     intent.putExtras(bundle);
                     startActivity(intent);

        }
    });

and getting date in deal1.class

Bundle bundle = getIntent().getExtras();
String CurrentSupporters = bundle.getInt("CurrentSupporters"); 
Syste.out.println("CurrentSupporters : "+CurrentSupporters);
String MaxSupporters = bundle.getInt("MaxSupporters"); 
String RegularPrice = bundle.getInt("RegularPrice"); 
String DiscountPrice = bundle.getInt("DiscountPrice"); 
Hariharan
  • 28,002
  • 7
  • 50
  • 55
  • This seems like exactly what I want, but how could I make it dynamic? The `all_deals` ArrayList will have hundreds of objects later on, so obviously it would be redundant to keep making `if` statements for each one... I already have a separate activity that the layout will be used for every single deal so I won't need to create an individual activity for every deal. – The Nomad Dec 04 '13 at 05:30
  • @D-Rock sorry got it wrong.. updated my answer.. you can send the different objects to same individual activity.. – Hariharan Dec 04 '13 at 05:47