[英]Create new listadapter from a fragment. Know how to do it from an activity but not from a fragment
I have an ArrayAdapter that I would like to call (create) from a Fragment, I know how to do it from an activity but not from a fragment, perhaps one of you guys could give me the right syntax? 我有一个ArrayAdapter,我想从一个Fragment调用(创建),我知道如何从一个活动中而不是从一个片段中进行创建,也许你们中的一个可以给我正确的语法?
Anyway I try to do the following from a fragment 无论如何,我尝试从片段中执行以下操作
ShareHoldingAdapter adapter = new ShareHoldingAdapter(this, R.layout.listview_item_row, allShareHoldings);
But my Adapter has the constructor ShareHoldingAdapter(Context portfolioFragMent, int layoutResourceId, ArrayList<ShareHolding> data)
IE its a "context" and not a "PortolioFragment" or simply a fragment 但是我的适配器具有构造函数
ShareHoldingAdapter(Context portfolioFragMent, int layoutResourceId, ArrayList<ShareHolding> data)
IE,它是一个“上下文”,而不是“ PortolioFragment”或一个片段
How would you change my Adapter so that it would work to call it from a fragment instead of now (from an activity?) 您将如何更改我的适配器,以便可以从片段而不是现在(从活动中)调用它?
Here is my adapter code 这是我的适配器代码
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;
}
}
to create your adapter from fragment you should call: 从片段创建适配器,您应该调用:
ShareHoldingAdapter adapter = new ShareHoldingAdapter(getActivity(), R.layout.listview_item_row, allShareHoldings);
since Fragment.getActivity()
return Activity
which is subclass of Context
因为
Fragment.getActivity()
返回Activity
,它是Context
子类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.