底部导航与活动中的列表视图

时间:2017-12-01 07:48:46

标签: android listview android-fragments fragment

我已创建此代码以在我的活动中调用ListView并且它可以正常工作,这里是代码

public class PegawaiActivity extends AppCompatActivity {

    private TextView mTextMessage;
    String namapegawai;
    List<String> IdList = new ArrayList<>();
    ListView StudentListView;
    ProgressBar progressBar;
    String HttpUrl = "http://192.168.43.241/Android/MyKoperasi/SemuaMember.php";

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedFragment = null;

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    //mTextMessage.setText(namapegawai);
                    selectedFragment = PegawaiFragment.newInstance();
                    break;
                case R.id.navigation_dashboard:
                    //mTextMessage.setText(R.string.title_dashboard);

                    selectedFragment = MemberFragment.newInstance();
                    break;
                //case R.id.navigation_notifications:
                   // mTextMessage.setText(R.string.title_notifications);
                    //break;
            }
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.frame_layout, selectedFragment);
            transaction.commit();
            return true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pegawai);



        StudentListView = findViewById(R.id.listview1);
        progressBar = findViewById(R.id.progressBar);
        new GetHttpResponse(PegawaiActivity.this).execute();



        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        //Manually displaying the first fragment - one time only
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, PegawaiFragment.newInstance());
        transaction.commit();
    }

    // JSON parse class started from here.
    public class GetHttpResponse extends AsyncTask<Void, Void, Void>
    {
        public Context context;

        String JSonResult;

        List<Member> studentList;

        public GetHttpResponse(Context context)
        {
            this.context = context;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0)
        {
            // Passing HTTP URL to HttpServicesClass Class.
            HttpServicesClass httpServicesClass = new HttpServicesClass(HttpUrl);
            try
            {
                httpServicesClass.ExecutePostRequest();

                if(httpServicesClass.getResponseCode() == 200)
                {
                    JSonResult = httpServicesClass.getResponse();

                    if(JSonResult != null)
                    {
                        JSONArray jsonArray = null;

                        try {
                            jsonArray = new JSONArray(JSonResult);

                            JSONObject jsonObject;
                            Member member;
                            studentList = new ArrayList<Member>();

                            for(int i=0; i<jsonArray.length(); i++)
                            {
                                member = new Member();
                                jsonObject = jsonArray.getJSONObject(i);

                                // Adding Student Id TO IdList Array.
                                IdList.add(jsonObject.getString("id").toString());
                                //Adding Student Name.
                                member.nama = jsonObject.getString("nama").toString();
                                member.nrp = jsonObject.getString("nrp").toString();
                                studentList.add(member);

                            }
                        }
                        catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                else
                {
                    Toast.makeText(context, httpServicesClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
                }
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result)

        {
            progressBar.setVisibility(View.GONE);
            StudentListView.setVisibility(View.VISIBLE);

            if(studentList != null) {
                ListAdapterClass adapter = new ListAdapterClass(studentList, context);
                StudentListView.setAdapter(adapter);
            }else
            {
                Toast.makeText(context, "Tidak ada data ditampilkan", Toast.LENGTH_SHORT).show();
            }

        }
    }

}

是的,它显示了ListView项目,但它在我的活动布局中显示,而不是在片段布局中显示

如何在不同的底部导航中显示它

@Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        //mTextMessage.setText(namapegawai);
                        selectedFragment = PegawaiFragment.newInstance();
                        break;
                    case R.id.navigation_dashboard:
                        //mTextMessage.setText(R.string.title_dashboard);

                        selectedFragment = MemberFragment.newInstance();
                        break;
                    //case R.id.navigation_notifications:
                       // mTextMessage.setText(R.string.title_notifications);
                        //break;
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.frame_layout, selectedFragment);
                transaction.commit();
                return true;
            }
        };

我用什么代码调用ListView来显示底部导航中的不同片段 感谢

====================== UPDATE 我的成员片段

public class MemberFragment extends Fragment {
    public static MemberFragment newInstance() {
        MemberFragment fragment = new MemberFragment();
        return fragment;
    }
    TextView textViewNamaHolder;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_member, container, false);

        getActivity().setTitle("Daftar Member");
        setHasOptionsMenu(true);

        //List<Member> studentList = ((PegawaiActivity) getActivity());
        //PegawaiActivity a = new PegawaiActivity();
        //PegawaiActivity.GetHttpResponse mm = a.new GetHttpResponse(getActivity());
        //mm.execute();
        //Adding ListView Item click Listener.


        return v;
    }
}

还有我的PegawaiFragment

public class PegawaiFragment extends Fragment {
    public static PegawaiFragment newInstance() {
        PegawaiFragment fragment = new PegawaiFragment();
        return fragment;
    }
    TextView textViewNamaHolder;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pegawai, container, false);

        getActivity().setTitle("Daftar Member");
        setHasOptionsMenu(true);

        //List<Member> studentList = ((PegawaiActivity) getActivity());
        //PegawaiActivity a = new PegawaiActivity();
        //PegawaiActivity.GetHttpResponse mm = a.new GetHttpResponse(getActivity());
        //mm.execute();
        //Adding ListView Item click Listener.


        return v;
    }

1 个答案:

答案 0 :(得分:2)

  1. listviewActivity移至Fragment的{​​{1}}和(fragment_member)布局文件。
  2. (pegawai_fragment)上将ListViewFragment传递到PegawaiActivity 的{​​{1}}课程。
  3. 我已更新您的代码。可能会有效。

    PegawaiActivity .java上的GetHttpResponse

    GetHttpResponse

    MemberFragment .java

    Fragment