フラグメントから呼び出し(作成)したいArrayAdapterがあります。フラグメントからではなく、アクティビティからそれを行う方法を知っています。おそらく、皆さんの1人が正しい構文を教えてくれますか?
とにかく私は断片から次のことをしようとします
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;
}
}