试图从另一个类调用Fragment中的函数

时间:2017-03-04 20:03:27

标签: java android class android-fragments

我需要调用我的portfolioActivity Fragment中的一个函数,它来自类portfolioListAdapter 它说我的参数不正确。

当我不使用片段时,此功能正常工作,但是当我将片段链接到导航抽屉时,有必要保持这种方式。

这是我的片段:

public class portfolioActivity extends Fragment {

    ListView portfolioList;
    String[] stockTicker={"AAPL", "GOOG", "MSFT"};
    double[] stockPrice={138.96, 830.63, 64.01};
    int[] shares={5, 2, 10};
    double[] percentChange={0.59, 0.55, 1.43};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.portfolio_layout, container, false);

        portfolioList = (ListView) getView().findViewById(R.id.portfolioListView);
        //ADAPTER
        ListAdapter adapter = new portfolioListAdapter(this, stockTicker, stockPrice, shares, percentChange);
        portfolioList.setAdapter(adapter);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getActivity().setTitle("Portfolio");
    }
}    

当我尝试调用portfolioListAdapter()时出现错误 它说“this”参数存在问题。

这是我的另一堂课:

public class portfolioListAdapter extends ArrayAdapter<String> {

//DECLARATIONS
String[] stockTicker={};
double[] stockPrice={};
int[] shares={};
double[] percentChange={};
Context c;
LayoutInflater inflater;

public portfolioListAdapter(Context context, String[] stockTicker,
                            double[] stockPrice, int[] shares, double[] percentChange) {

    super(context, R.layout.portfolio_row_model, stockTicker);

    this.c=context;
    this.stockTicker=stockTicker;
    this.stockPrice=stockPrice;
    this.shares=shares;
    this.percentChange=percentChange;
}

public class ViewHolder
{
    TextView stockTicker;
    TextView stockPrice;
    TextView shares;
    TextView totalValue;
    TextView percentChange;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView==null)
    {
        inflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView=inflater.inflate(R.layout.portfolio_row_model, null);
    }

    // OUR VIEWHOLDER OBJECT
    final ViewHolder holder = new ViewHolder();

    //INITIALIZE VIEWS
    holder.stockTicker= (TextView) convertView.findViewById(R.id.list_portfolio_ticker);
    holder.stockPrice= (TextView) convertView.findViewById(R.id.list_portfolio_price);
    holder.shares= (TextView) convertView.findViewById(R.id.list_portfolio_shares);
    holder.totalValue= (TextView) convertView.findViewById(R.id.list_portfolio_value);
    holder.percentChange= (TextView) convertView.findViewById(R.id.list_portfolio_change);

    //ASSIGN VIEWS
    holder.stockTicker.setText(stockTicker[position]);
    holder.stockPrice.setText(String.valueOf("$"+stockPrice[position]));
    holder.shares.setText(String.valueOf(shares[position]));
    holder.totalValue.setText(String.valueOf("$"+(stockPrice[position]*shares[position])));
    holder.percentChange.setText(String.valueOf(percentChange[position]+"%"));


    //return super.getView(position, convertView, parent);
    return convertView;
}
}

2 个答案:

答案 0 :(得分:1)

您无法在return之后输入代码。

您的参数 错误。 this是片段,而不是上下文,您需要在getActivity()或(最好)onAttach使用private ListView portfolioList; private PortfolioListAdapter adapter; @Override public void onAttach(Context context) { super.onAttach(activity); ( (Activity) context).setTitle("Portfolio"); adapter = new PortfolioListAdapter(context, stockTicker, stockPrice, shares, percentChange); portfolioList.setAdapter(adapter); } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.portfolio_layout, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); portfolioList = (ListView) view.findViewById(R.id.portfolioListView); }

ViewHolder

注意:您也没有正确使用cookImage = PhotoImage(file=r"C:\Users\terimaa\AppData\Local\Programs\Python\Python36-32\cook.gif") 可能想要回顾一下如何做到这一点(或使用RecyclerView)

答案 1 :(得分:0)

1)返回后无法输入代码。

2)适配器的构造函数需要一个“Context”对象。你传递的是一个片段对象(这个) 添加此代码片段

private Context mContext;
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mContext = context;
}

在调用适配器时传递mContext而不是这个。