1

我有一个 ArrayAdapter,我想从 Fragment 调用(创建),我知道如何从活动而不是从 Fragment 来做,也许你们中的一个人可以给我正确的语法?

无论如何,我尝试从片段中执行以下操作

ShareHoldingAdapter adapter = new ShareHoldingAdapter(this, R.layout.listview_item_row, allShareHoldings);

但是我的适配器有构造函数ShareHoldingAdapter(Context portfolioFragMent, int layoutResourceId, ArrayList<ShareHolding> data)IE 它是一个“上下文”而不是一个“PortolioFragment”或只是一个片段

您将如何更改我的适配器,以便从片段而不是现在(从活动?)

这是我的适配器代码

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ShareHoldingAdapter extends ArrayAdapter<ShareHolding>{

    Context context; 
    int layoutResourceId;    
    ArrayList<ShareHolding> data = null;
    private ShareHolder holder;
    private ShareHolding shareHold;


    private int buttonPressed = 0;
    public void setButtonPressed(int buttonPressed) {
        this.buttonPressed = buttonPressed;
        notifyDataSetChanged();
    }


    public ShareHoldingAdapter(Context portfolioFragMent, int layoutResourceId, ArrayList<ShareHolding> data) {//Need to change this constructor to accept a fragment instead of an activiy (context)
        super(portfolioFragMent, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = portfolioFragMent;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ShareHolder();
            holder.imgIcon = (TextView)row.findViewById(R.id.imgen);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (ShareHolder)row.getTag();
        }

        shareHold = data.get(position);
        holder.txtTitle.setText(shareHold.getName());

        //buttonPressed

        switch (buttonPressed) {
        case 0: 
            holder.imgIcon.setText(Double.toString(shareHold.getSharesChangeTodayPercent()));
          break;
        case 1: 
            holder.imgIcon.setText(Double.toString(shareHold.getCurrentRate()));
          break;
        case 2: 
            holder.imgIcon.setText(Double.toString(shareHold.getNrOfSharesInPortfolio()));
          break;
        case 3: 
            holder.imgIcon.setText(Double.toString(shareHold.getTotalPercentDifference()));
          break;
        case 4: 
            holder.imgIcon.setText(Double.toString(shareHold.getTotalDiffrenceKr()));
          break;
        case 5: 
            holder.imgIcon.setText(Double.toString(shareHold.getTotalKr()));
          break;
        default: 
            holder.imgIcon.setText(Double.toString(shareHold.getBuyingRate()));
      }

        //holder.imgIcon.setText(Double.toString(weather.getBuyingRate()));
        return row;
    }

    static class ShareHolder
    {
        TextView imgIcon;
        TextView txtTitle;
    }
}
4

1 回答 1

0

要从片段创建适配器,您应该调用:

ShareHoldingAdapter adapter = new ShareHoldingAdapter(getActivity(), R.layout.listview_item_row, allShareHoldings);

因为Fragment.getActivity()returnActivity是的子类Context

于 2012-12-05T15:56:21.670 回答