ListView的自定义ArrayAdapter和自定义对象 [英] ListView with custom ArrayAdapter and custom object

查看:179
本文介绍了ListView的自定义ArrayAdapter和自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的应用程序的背景细节:


  • 在我的主要活动使用FragmentPagerAdapter展示几个不同的片段。

  • 对于我的片段中的一个,我使用的是正在使用的自定义对象类的自定义ArrayAdapter填充一个ListView,在这种情况下,新政。

  • 使用传递到自定义ArrayAdapter一个ArrayList

我基本上是停留在:当我点击的ListView片段页面上的任何交易,我要到另一个活动,并通过从特定交易对象的信息。所以,如果我点击成交1笔,我想在新政1对象传递给新的活动,它的信息。如果我点击成交2笔,我想在新政2物体传递到新的活动,它的信息。我不确定要放什么东西在onItemClickListener。

请注意,该ArrayList会得到它是从后外部源对象,只是在考试科目增加为现在。我将需要使用一个ArrayAdapter中的信息传递给新的活动页面?

DealsFragment

 公共类DealsFragment扩展片段{@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){    查看查看= inflater.inflate(R.layout.fragment_show_deals,集装箱,FALSE);
    ListView控件的ListView =(ListView控件)view.findViewById(R.id.dealsListView);    //样品组数据传递到适配器,用于测试目的
    ArrayList的<&应对GT; all_deals =新的ArrayList<&应对GT;();
    all_deals.add(新政(成交1,R.drawable.test_image,389,700,750,500));
    all_deals.add(新政(成交2,R.drawable.test_image,20,80,1800,1500));
    all_deals.add(新政(成交3,R.drawable.test_image,1932年,2000年,75,60));
    all_deals.add(新政(新政4,R.drawable.test_image,198,450,450,350));
    all_deals.add(新政(成交5,R.drawable.test_image,60,70,1500,1100));    //设置适配器将数据传递到XML
    DealAdapter适配器=新DealAdapter(getActivity(),R.layout.listview_item_row,all_deals);    //添加回标题行中
    //浏览标题=(查看)inflater.inflate(R.layout.listview_header_row,NULL);
    // LV.addHeaderView(头);    listView.setAdapter(适配器);    listView.setOnItemClickListener(新OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){            意向意图=新意图(getActivity(),DealPage.class);
            startActivity(意向);
        }
    });    返回视图。
}}

DealAdapter

 公共类DealAdapter扩展ArrayAdapter<&应对GT; {上下文语境;
INT layoutResourceId;
ArrayList的<&应对GT;数据= NULL;公共DealAdapter(上下文的背景下,INT layoutResourceId,ArrayList的<&新政GT;数据){
    超级(上下文,layoutResourceId,数据);
    this.layoutResourceId = layoutResourceId;
    this.context =背景;
    this.data =数据;
}静态类ViewHolder
{
    ImageView的imgDealImage;
    TextView的txtDescription;
    TextView的txtSupporters;
    TextView的txtRegularPrice;
    TextView的txtDiscountPrice;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    ViewHolder支架=无效;    如果(convertView == NULL)
    {
        LayoutInflater充气=((活动)上下文).getLayoutInflater();
        convertView = inflater.inflate(layoutResourceId,父母,假);        持有人=新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(保持器);
    }
    其他
    {
        支架=(ViewHolder)convertView.getTag();
    }    INT图像= data.get(位置).getImage();
    字符串描述= data.get(位置).getDescription();
    INT currentSupporters = data.get(位置).getCurrentSupporters();
    INT maxSupporters = data.get(位置).getMaxSupporters();
    INT regularPrice = data.get(位置).getRegularPrice();
    INT discountPrice = data.get(位置).getDiscountPrice();    holder.imgDealImage.setImageResource(图片);
    holder.txtDescription.setText(介绍);
    holder.txtSupporters.setText(将String.valueOf(currentSupporters +/+ maxSupporters +支持者));
    holder.txtRegularPrice.setText(将String.valueOf($+ regularPrice));
    holder.txtRegularPrice.setPaintFlags(holder.txtRegularPrice.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
    holder.txtDiscountPrice.setText(将String.valueOf($+ discountPrice));    返回convertView;
}}


解决方案

酪氨酸这个..

  listView.setOnItemClickListener(新OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){            这笔交易的交易= all_deals.get(位置);                     意向意图=新意图(getActivity(),Deal1.class);
                     束束=新包();
                     bundle.putInt(CurrentSupporters,deal.getCurrentSupporters());
                     bundle.putInt(MaxSupporters,deal.getMaxSupporters());
                     bundle.putInt(RegularPrice,deal.getRegularPrice());
                     bundle.putInt(DiscountPrice,deal.getDiscountPrice());
                     intent.putExtras(包);
                     startActivity(意向);        }
    });

和获取deal1.class日期

 捆绑包= getIntent()getExtras()。
串CurrentSupporters = bundle.getInt(CurrentSupporters);
Syste.out.println(CurrentSupporters:+ CurrentSupporters);
串MaxSupporters = bundle.getInt(MaxSupporters);
串RegularPrice = bundle.getInt(RegularPrice);
串DiscountPrice = bundle.getInt(DiscountPrice);

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

}

解决方案

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

这篇关于ListView的自定义ArrayAdapter和自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆