Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView with custom ArrayAdapter and custom object

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

}
like image 558
The Nomad Avatar asked Jun 07 '26 04:06

The Nomad


2 Answers

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"); 
like image 190
Hariharan Avatar answered Jun 08 '26 18:06

Hariharan


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

like image 45
Vikas B L Avatar answered Jun 08 '26 17:06

Vikas B L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!