2017-03-04 71 views
-2

我需要在我的portfolioActivityFragment中调用来自类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()的错误出现。
它说“这个”参数有问题。

这里是我的其他类:

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

回答

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
可能想回头看看如何做到这一点(或使用RecyclerView)

+0

非常感谢你,我一直在为此奋斗了好几个小时。另外,ViewHolder有什么问题?我在观看视频的同时构建了大部分代码,这就是这个人放置它的地方。我的代码正在做我想要的。 –

+0

你永远不会调用'getTag()'或'setTag()',所以如果这段代码来自教程,那也是错误的。 https://dzone.com/articles/optimizing-your-listview –

0

1)返回后不能放置代码。

2)适配器的构造函数需要一个“上下文”对象。什么你逝去的是一个片段对象(this) 添加该代码段

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

而且在调用适配器直通的,而不是这个mContext。

相关问题