简体   繁体   English

从片段创建新的listadapter。 知道如何通过活动而不是片段来做到这一点

[英]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.

相关问题 试图通过按下按钮来打开活动片段。 什么都没发生 - Trying to open activity from button press in fragment. Nothing happens 如何从ListAdapter更改片段的Lis​​tView中的数据? - How do I change data in a ListView of a Fragment from a ListAdapter? 将数据从片段传递到另一个片段。 片段未附加到上下文 - Passing data from fragment to another fragment. Fragment not attached to a context 我想将数据从活动转移到片段,然后在片段中显示数据。 我的代码不起作用 - I want to transfer data from activity to fragment and then display data in the fragment. My code doesn't work 如何从对话框片段中打开新活动 - How to open a new activity from a dialog fragment 从一个片段转移到一个新的活动 - Moving from a fragment to a new activity Android-DialogFragment从片段显示。 取消DialogFragment时如何显示Fragment中的Toast? - Android - DialogFragment is shown from a Fragment. How to show Toast from Fragment when DialogFragment is dismissed? 如何从片段开始向活动添加片段? - How to add a fragment to activity, started from a fragment? 最大化后,如何创建一个新片段来打开前一个片段的全屏视频视图? - How do I create a new fragment that opens a full screen video view from the previous fragment when maximized? 如何从活动开始片段 - How to start a fragment from an activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM