ListView包含自定义ArrayAdapter和自定义对象

时间:2013-12-04 04:41:41

标签: java android listview android-arrayadapter onclicklistener

以下是我的应用的背景详情:

  • 在我的主要活动中使用FragmentPagerAdapter来显示几个不同的片段。
  • 对于我的一个片段,我使用的ListView是由使用自定义对象类的自定义ArrayAdapter填充的,在本例中为“Deal”。
  • 使用传递给自定义ArrayAdapter的ArrayList。

我基本上坚持:当我点击ListView片段页面上的任何“交易”时,我想转到另一个活动并从特定的Deal对象传递信息。所以,如果我点击Deal 1,我想将Deal 1对象传递给新活动以获取它的信息。如果我点击Deal 2,我想将Deal 2对象传递给新活动以获取它的信息。我不确定要放在onItemClickListener中的内容。

请注意,ArrayList稍后会从外部源获取它的对象,现在只需添加测试主题。我是否需要使用ArrayAdapter将信息传递到新的活动页面?

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

}

2 个答案:

答案 0 :(得分:1)

让你的交易类成为setter和getter。您正在设置新对象。在一个arraylist中,有一个选项可以从位置获取对象。因此,一旦获得该职位,请尝试从所选交易对象中获取信息。您不能传递对象,但可以传递个人信息。从Deal对象获取个人信息并传递为putExtra。尝试使用bundle类来传递捆绑包中的所有信息。

检查Bundle

答案 1 :(得分: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);

        }
    });

并在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");