【问题标题】:ListView with custom ArrayAdapter and custom object具有自定义 ArrayAdapter 和自定义对象的 ListView
【发布时间】:2013-12-04 04:41:40
【问题描述】:

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

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

我基本上被卡住了:当我单击 ListView 片段页面上的任何“交易”时,我想转到另一个活动并传递来自特定交易对象的信息。因此,如果我单击 Deal 1,我想将 Deal 1 对象传递给新活动以获取它的信息。如果我单击 Deal 2,我想将 Deal 2 对象传递给新活动以获取它的信息。我不确定要在 onItemClickListener 中添加什么。

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

交易片段

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

}

【问题讨论】:

  • 在此处创建另一个类 bean.class 并从中获取值

标签: java android listview android-arrayadapter onclicklistener


【解决方案1】:

Tyr 这个..

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

【讨论】:

  • 这似乎正是我想要的,但我怎样才能让它动态呢? all_deals ArrayList 稍后将有数百个对象,因此显然继续为每个对象制作 if 语句是多余的......我已经有一个单独的活动,布局将用于每笔交易,所以我无需为每笔交易创建单独的活动。
  • @D-Rock 抱歉弄错了.. 更新了我的答案.. 您可以将不同的对象发送到同一个单独的活动..
【解决方案2】:

将您的交易类设为 setter 和 getter。您正在设置新对象。在数组列表中有一个选项可以从位置获取对象。因此,一旦您获得头寸,请尝试从选定的交易对象中获取信息。您不能传递对象,但可以传递个人信息。从 Deal 对象中获取个人信息并作为 putExtra 传递。尝试使用 bundle 类来传递 bundle 中的所有信息。

查看Bundle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    相关资源
    最近更新 更多