外部类中内部类的成员字段的Java android访问

时间:2017-02-04 12:29:00

标签: java android

public class chapterIndexFragment extends Fragment implements view.OnClickListener,parsexml.AsyncResponse {

// TODO:重命名参数参数,选择匹配的名称     //片段初始化参数,例如ARG_ITEM_NUMBER

private static final String ARG_PARAM1 = "xml";
//private static final String ARG_PARAM2 = "param2";
private XmlResourceParser xrp;
private View rootview;
private OnFragmentInteractionListener mListener;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

public chapterIndexFragment() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment chapterIndexFragment.
 */
// TODO: Rename and change types and number of parameters
public static chapterIndexFragment newInstance(int xmlid) {
    chapterIndexFragment fragment = new chapterIndexFragment();
    Bundle args = new Bundle();

    args.putInt(ARG_PARAM1,xmlid);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        xrp = getResources().getXml(getArguments().getInt(ARG_PARAM1));

        //Async Task.
        parsexml pxml = new parsexml(this);
        pxml.execute(xrp);
    }
}

@Override
public void postProgress(String progress) {

   // Why this does not resolve ????
   mAdapter.mDataset

}

@Override
public void onFinish(Boolean result) {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootview = inflater.inflate(R.layout.fragment_recycler_view, container, false);

    mRecyclerView = (RecyclerView) rootview.findViewById(R.id.recycler_view);

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);

    // When coming back to this fragment from another fragment with back button. Although view hierarchy is to
    //be built again but Adapter will have its data preserved so dont initialize again.

    //String[] TempData = new String[]{"One","Two","three","four"};

    if(mAdapter == null)
        mAdapter = new MyAdapter();

    mRecyclerView.setAdapter(mAdapter);
    return  rootview;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
    toolbar.setTitle("Title");
    toolbar.setNavigationIcon(R.drawable.ic_menu_hamburger);
    //toolbar navigation button (Home Button\Hamburger button) at the start of the toolbar.
    toolbar.setNavigationOnClickListener(this);
    //Toolbar menu item click listener.
    //toolbar.setOnMenuItemClickListener(this);
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

@Override
public void onClick(View view) {

    int id = view.getId();
    switch(id)
    {
        case -1: // Toolbar's Navigation button click retruns -1. Hamburger icon.

            if (view instanceof ImageView)
        {

            if (((MainActivity)getActivity()).drawer.isDrawerOpen(GravityCompat.START))
            {
                ((MainActivity)getActivity()).drawer.closeDrawer(GravityCompat.START);
            }
            else
            {
                ((MainActivity)getActivity()).drawer.openDrawer(GravityCompat.START);
            }
        }
            break;
    }
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}


class MyAdapter extends RecyclerView.Adapter implements MyViewHolder.IItemClickListener{
    private ArrayList<String> mDataset;

    // Provide a suitable constructor (depends on the kind of dataset)
    MyAdapter(){
        if(mDataset == null)
        {
            mDataset = new ArrayList<String>();
        }

    }


    // Create new views (invoked by the layout manager)
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_viewholder,parent,false);
        // set the view's size, margins, paddings and layout parameters
        MyViewHolder vh = new MyViewHolder(v,this);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        MyViewHolder vh = (MyViewHolder)holder;
        vh.mtv_Title.setText(mDataset.get(position));

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    //MyViewHolder.IItemClickListener
    @Override
    public void onItemClick(View view, int position) {

    }
}

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    // each data item is just a string in this case
    TextView mtv_Title;
    IItemClickListener mItemClickListener;

    //View.OnClickListener
    @Override
    public void onClick(View view) {
     mItemClickListener.onItemClick(view,getAdapterPosition());
    }

    interface IItemClickListener
    {
        void onItemClick(View view, int position);
    }

    MyViewHolder(View v,IItemClickListener itemClickListener) {
        super(v);
        mtv_Title = (TextView)v.findViewById(R.id.tv_title);
        mItemClickListener = itemClickListener;
        v.setOnClickListener(this);
    }
}

}

在@Override中     public void postProgress(String progress){

   // Why this does not resolve ????
   mAdapter.mDataset

}

1 个答案:

答案 0 :(得分:0)

你应该知道,

  不能通过点表示法访问私人会员。要访问它们,我们使用getter / setter方法。

你必须创建一个公共函数来获取它们,在你的内部类中添加这个方法,

public ArrayList<String> getDataSet() {

    return mDataSet;
}

此外,您应该按照@android_hub

的指示进行更改
private RecyclerView.Adapter mAdapter;

应该是私人的

private MyAdapter mAdapter;