如何在Android中以表格形式显示ListView

4
我希望展示一个表格格式的ListView
这个ListView应该像Excel表格一样有行和列。
请问有谁能提供实现方法?

你可以使用 BaseAdapter 来实现。 - Prashant Mishra
2个回答

1
    class CustomAdapterCoupons extends BaseAdapter {
    /* Variable Declaration */
    private Context context;

    private List<CouponBean> list;

    private CouponBean entry;
    public com.pocketJini.util.ImageLoader imageLoader;
    private LayoutInflater inflater;

    public CustomAdapterCoupons(Context context, List<CouponBean> list) {
        this.context = context;
        this.list = list;
        inflater = (LayoutInflater) CouponsActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new com.pocketJini.util.ImageLoader(context);
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class ViewHolder {
        public TextView couponName, couponCode, usageDescription,
                expirationDate;
        public ImageView couponImage;

    }

    public View getView(final int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;

        entry = list.get(position);

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.coupons_list_layout,
                    null);

            holder = new ViewHolder();

            holder.couponName = (TextView) convertView
                    .findViewById(R.id.CouponListCouponName);
            holder.couponCode = (TextView) convertView
                    .findViewById(R.id.CouponListCouponCode);
            holder.expirationDate = (TextView) convertView
                    .findViewById(R.id.CouponListDetailDate);
            holder.usageDescription = (TextView) convertView
                    .findViewById(R.id.CouponListUsageDescription);
            holder.couponImage = (ImageView) convertView
                    .findViewById(R.id.CouponListLeftImage);

            convertView.setTag(holder);
            // Set the display text
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.couponName.setText(entry.getCouponName());
        holder.expirationDate.setText(context
                .getString(R.string.Coupon_Expiration_Date)
                + "\n"
                + entry.getExpirationDate());
        holder.usageDescription.setText(entry.getUsageDescription());
        holder.couponCode.setText(entry.getCouponCode());

        holder.couponImage.setTag(Data.URL_BASE_2 + entry.getCouponImage());
        imageLoader.DisplayImage(Data.URL_BASE_2 + entry.getCouponImage(),
                (Activity) context, holder.couponImage);
        Log.v(Data.LOG3, "image" + entry.getCouponImage());

        final Button savedMyCoupons = (Button) convertView
                .findViewById(R.id.CouponListAddtoMyCouponButton);
        if (entry.getSavedMyCoupons().equalsIgnoreCase("N")) {
            savedMyCoupons.setText(context
                    .getString(R.string.Add_to_myCoupons));
            savedMyCoupons.setBackgroundResource(R.drawable.done_btn);
            savedMyCoupons.setTag(entry.getCouponId().toString());
            savedMyCoupons.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    createProgressDialog();

                    new Loader()
                            .execute(savedMyCoupons.getTag().toString());
                }
            });

        } else if (entry.getSavedMyCoupons().equalsIgnoreCase("Y")) {
            savedMyCoupons.setText(context
                    .getString(R.string.Already_Added_to_my_coupons));
            savedMyCoupons.setBackgroundColor(Color.WHITE);
            savedMyCoupons.setTextColor(Color.BLACK);

        }

        // display the view corresponding to data at specified position
        return convertView;

    }
}

adapter = new CustomAdapterCoupons(this, entry.getCouponsList()); couponsListLayout.setAdapter(adapter); - Prashant Mishra
请使用表格布局视图,在其行中添加文本视图,并在Viewholder类中为它们创建字段,然后在getView方法中通过id查找视图... 然后提供要显示的文本视图的值。 - Prashant Mishra
谢谢Prashant,它运行得很好。现在我们有一个具有行和列的表格。我们能改变单元格的形状吗?如果可以,怎么做? - Dilip Kumar Chaudhary

0
创建一个自定义ListView并提供所需的xml视图(这将是您的TableRow)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接