从listview获取列表项的计数

时间:2016-04-15 00:42:01

标签: android listview fragment

我试图从列表视图中获取列表项,但它没有用。

这是我的代码:

class Ambil_Buku_Keranjang extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            //list item (2 item)
        }

        @Override
        protected void onPostExecute(String s) {
            if (s != null && success == 1) {
                ListAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(), daftarBukuKeranjang,
                        R.layout.list_keranjang, new String[]{TAG_KERANJANG, TAG_INVENTARIS, TAG_JUDUL},
                        new int[]{R.id.id_keranjang, R.id.kd_inventaris_keranjang, R.id.judul_buku_keranjang});
                listPesan.setAdapter(adapter);
                count = listPesan.getAdapter().getCount();
            }
        }

onActivityCreated:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        new Ambil_Buku_Keranjang().execute();
        Toast.makeText(getActivity().getApplicationContext(),""+count,Toast.LENGTH_LONG).show();
}

我的代码有什么问题吗?请帮助谢谢......

1 个答案:

答案 0 :(得分:0)

@Override
protected void onPostExecute(String s) {
    if (s != null && success == 1) {
        ListAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(), daftarBukuKeranjang,
                R.layout.list_keranjang, new String[]{TAG_KERANJANG, TAG_INVENTARIS, TAG_JUDUL},
                new int[]{R.id.id_keranjang, R.id.kd_inventaris_keranjang, R.id.judul_buku_keranjang});
        listPesan.setAdapter(adapter);
        count = listPesan.getAdapter().getCount();
        Toast.makeText(getActivity().getApplicationContext(),count,Toast.LENGTH_LONG).show();
    }
}

你的任务是异步的。

Understand AsyncTask properly

你将获得onPostExecute的计数,这样你就无法计入onActivityCreated