我需要在我的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;
}
}
非常感谢你,我一直在为此奋斗了好几个小时。另外,ViewHolder有什么问题?我在观看视频的同时构建了大部分代码,这就是这个人放置它的地方。我的代码正在做我想要的。 –
你永远不会调用'getTag()'或'setTag()',所以如果这段代码来自教程,那也是错误的。 https://dzone.com/articles/optimizing-your-listview –