외부 클래스의 내부 클래스 멤버 필드에 대한 Java Android 액세스

Dev

public class chapterIndexFragment extends Fragment는 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

}
잭 제이

알고 있어야합니다.

개인 멤버는 점 표기법을 통해 액세스 할 수 없습니다. 액세스하기 위해 getter / setter 메서드를 사용합니다.

그것들을 얻기 위해 공용 함수를 만들어야하고,이 메서드를 내부 클래스에 추가해야합니다.

public ArrayList<String> getDataSet() {

    return mDataSet;
}

또한 @android_hub가 가리키는대로 변경해야합니다.

private RecyclerView.Adapter mAdapter;

비공개 여야합니다

private MyAdapter mAdapter;

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

클래스 외부 클래스의 비공개 멤버에 액세스

분류에서Dev

내부 클래스 인터페이스 상수에 대한 Java 외부 클래스 액세스

분류에서Dev

내부 클래스 인터페이스 상수에 대한 Java 외부 클래스 액세스

분류에서Dev

외부 클래스 중첩 열거에 대한 내부 클래스 액세스

분류에서Dev

내부 클래스에서 외부 클래스의 필드에 액세스

분류에서Dev

중첩 클래스에 외부 클래스 템플릿의 멤버 func에 대한 포인터 전달

분류에서Dev

중첩 클래스에 외부 클래스 템플릿의 멤버 func에 대한 포인터 전달

분류에서Dev

중첩 클래스의 클래스 내 친구가 외부 클래스 멤버에 액세스 할 수 있습니까?

분류에서Dev

중첩 클래스의 클래스 내 친구가 외부 클래스 멤버에 액세스 할 수 있습니까?

분류에서Dev

Android의 내부 클래스 내 변수에 액세스

분류에서Dev

둘러싸는 클래스에서 내부 클래스 멤버의 액세스 가능성

분류에서Dev

Java Inner 클래스-외부 객체의 변수에 액세스

분류에서Dev

내부 중첩 클래스의 외부 클래스에 액세스

분류에서Dev

C ++ : 부모 클래스에서 자식 멤버 액세스

분류에서Dev

부모 클래스에 대한 참조를 통해 자식 클래스의 멤버 변수에 액세스 할 수 있습니까?

분류에서Dev

내부 클래스 상속 및 둘러싸는 클래스 메서드 / 필드에 대한 액세스

분류에서Dev

익명의 내부 클래스는 외부 클래스 멤버에 영향을주지 않습니다.

분류에서Dev

클래스의 비공개 멤버에 대한 액세스

분류에서Dev

cs 파일 외부의 클래스 멤버를 숨기지 만 파일 내의 클래스에 액세스 할 수 있음

분류에서Dev

지역 내부 클래스에서 외부 개체에 대한 모의 액세스

분류에서Dev

C ++의 부모 클래스에서 chid 클래스 데이터 멤버에 액세스하는 방법

분류에서Dev

내부 클래스에 대한 Java의 일반 유형

분류에서Dev

파생 (자식) 클래스에 의한 오버로딩이 가능한 기본 (부모) 클래스 메서드의 클래스 특성에 액세스

분류에서Dev

C #의 클래스 (부분 클래스) 뒤에있는 코드에서 다른 어셈블리의 클래스에서 보호 된 멤버에 액세스

분류에서Dev

클래스 정의 외부의 클래스에 대한 새 메서드

분류에서Dev

부모 클래스 생성자가 호출 한 메서드의 자식 클래스 필드에 액세스

분류에서Dev

클래스 내부에 동일한 클래스의 필드가 있음

분류에서Dev

struts.xml에서 Java 내부 클래스에 액세스

분류에서Dev

C ++ std :: for_each 내부 클래스의 개인 멤버에 액세스하는 방법

Related 관련 기사

  1. 1

    클래스 외부 클래스의 비공개 멤버에 액세스

  2. 2

    내부 클래스 인터페이스 상수에 대한 Java 외부 클래스 액세스

  3. 3

    내부 클래스 인터페이스 상수에 대한 Java 외부 클래스 액세스

  4. 4

    외부 클래스 중첩 열거에 대한 내부 클래스 액세스

  5. 5

    내부 클래스에서 외부 클래스의 필드에 액세스

  6. 6

    중첩 클래스에 외부 클래스 템플릿의 멤버 func에 대한 포인터 전달

  7. 7

    중첩 클래스에 외부 클래스 템플릿의 멤버 func에 대한 포인터 전달

  8. 8

    중첩 클래스의 클래스 내 친구가 외부 클래스 멤버에 액세스 할 수 있습니까?

  9. 9

    중첩 클래스의 클래스 내 친구가 외부 클래스 멤버에 액세스 할 수 있습니까?

  10. 10

    Android의 내부 클래스 내 변수에 액세스

  11. 11

    둘러싸는 클래스에서 내부 클래스 멤버의 액세스 가능성

  12. 12

    Java Inner 클래스-외부 객체의 변수에 액세스

  13. 13

    내부 중첩 클래스의 외부 클래스에 액세스

  14. 14

    C ++ : 부모 클래스에서 자식 멤버 액세스

  15. 15

    부모 클래스에 대한 참조를 통해 자식 클래스의 멤버 변수에 액세스 할 수 있습니까?

  16. 16

    내부 클래스 상속 및 둘러싸는 클래스 메서드 / 필드에 대한 액세스

  17. 17

    익명의 내부 클래스는 외부 클래스 멤버에 영향을주지 않습니다.

  18. 18

    클래스의 비공개 멤버에 대한 액세스

  19. 19

    cs 파일 외부의 클래스 멤버를 숨기지 만 파일 내의 클래스에 액세스 할 수 있음

  20. 20

    지역 내부 클래스에서 외부 개체에 대한 모의 액세스

  21. 21

    C ++의 부모 클래스에서 chid 클래스 데이터 멤버에 액세스하는 방법

  22. 22

    내부 클래스에 대한 Java의 일반 유형

  23. 23

    파생 (자식) 클래스에 의한 오버로딩이 가능한 기본 (부모) 클래스 메서드의 클래스 특성에 액세스

  24. 24

    C #의 클래스 (부분 클래스) 뒤에있는 코드에서 다른 어셈블리의 클래스에서 보호 된 멤버에 액세스

  25. 25

    클래스 정의 외부의 클래스에 대한 새 메서드

  26. 26

    부모 클래스 생성자가 호출 한 메서드의 자식 클래스 필드에 액세스

  27. 27

    클래스 내부에 동일한 클래스의 필드가 있음

  28. 28

    struts.xml에서 Java 내부 클래스에 액세스

  29. 29

    C ++ std :: for_each 내부 클래스의 개인 멤버에 액세스하는 방법

뜨겁다태그

보관