如何在Array中存储所有CustomListView项

时间:2015-01-23 05:41:00

标签: android

我通过Base Adapter创建一个Custom ListView,如下面的代码所示。 其实我需要获得CustomListView的长度。感谢您在Advance中的帮助

public class ListViewWithBaseAdapter extends Activity {

    public class codeLeanChapter {
        String chapterName;
        String chapterDescription;
    }
    CodeLearnAdapter chapterListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_with_simple_adapter);


        chapterListAdapter = new CodeLearnAdapter();

        ListView codeLearnLessons = (ListView)findViewById(R.id.listView1);
        codeLearnLessons.setAdapter(chapterListAdapter);

        codeLearnLessons.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                codeLeanChapter chapter = chapterListAdapter.getCodeLearnChapter(arg2);

                Toast.makeText(ListViewWithBaseAdapter.this, chapter.chapterName,Toast.LENGTH_LONG).show();

            }
        });
    }

// ---------------------------------------------Adapter class Start from Here--------------------------------------------------------------
    public class CodeLearnAdapter extends BaseAdapter {

        List<codeLeanChapter> codeLeanChapterList = getDataForListView();
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return codeLeanChapterList.size();
        }

        @Override
        public codeLeanChapter getItem(int arg0) {
            // TODO Auto-generated method stub
            return codeLeanChapterList.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {

            if(arg1==null)
            {
                LayoutInflater inflater = (LayoutInflater) ListViewWithBaseAdapter.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                arg1 = inflater.inflate(R.layout.listitem, arg2,false);
            }

            TextView chapterName = (TextView)arg1.findViewById(R.id.textView1);
            TextView chapterDesc = (TextView)arg1.findViewById(R.id.textView2);

            codeLeanChapter chapter = codeLeanChapterList.get(arg0);

            chapterName.setText(chapter.chapterName);
            chapterDesc.setText(chapter.chapterDescription);

            return arg1;
        }

        public codeLeanChapter getCodeLearnChapter(int position)
        {
            return codeLeanChapterList.get(position);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.list_view_with_simple_adapter, menu);
        return true;
    }

    public List<codeLeanChapter> getDataForListView()
    {
        List<codeLeanChapter> codeLeanChaptersList = new ArrayList<codeLeanChapter>();

        for(int i=0;i<10;i++)
        {

            codeLeanChapter chapter = new codeLeanChapter();
            chapter.chapterName = "Chapter "+i;
            chapter.chapterDescription = "This is description for chapter "+i;
            codeLeanChaptersList.add(chapter);
        }

        return codeLeanChaptersList;

    }
}

1 个答案:

答案 0 :(得分:1)

对于BaseAdapter,我的建议是使用ViewHolder模式进行行项目创建。

如果您希望得到列表视图的总长度,而不是设置数据后的总长度,或者根据列表集合大小得到。

  

int totalListViewsize = adapter.getCount();

在下面发布的代码中,我提到了getview类中的注释及其所需的位置

    public class ListViewWithBaseAdapter extends Activity {

        ListView listView;
        public class codeLeanChapter {
            String chapterName;
            String chapterDescription;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_view_with_simple_adapter);
            listView = (ListView) findViewById(R.id.listView1);
            ListViewCustomAdapter adapter = new ListViewCustomAdapter(this,
                    getDataForListView());
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {

                    codeLeanChapter chapter = adapter.getItem(arg2);

                    Toast.makeText(ListViewWithBaseAdapter.this, chapter.chapterName,Toast.LENGTH_LONG).show();

                }
            });
        int totalListViewsize = adapter.getCount();
}

        public List<codeLeanChapter> getDataForListView() {
            List<codeLeanChapter> codeLeanChaptersList = new ArrayList<codeLeanChapter>();

            for (int i = 0; i < 10; i++) {

                codeLeanChapter chapter = new codeLeanChapter();
                chapter.chapterName = "Chapter " + i;
                chapter.chapterDescription = "This is description for chapter " + i;
                codeLeanChaptersList.add(chapter);
            }

            return codeLeanChaptersList;

        }

        private class ListViewCustomAdapter extends BaseAdapter {
            Context context;
            int totalDisplayDatasize = 0;
            List<codeLeanChapter> codeLeanChapterList;

            public ListViewCustomAdapter(Context context,
                    List<codeLeanChapter> codeLeanChapterList) {
                this.context = context;
                this.codeLeanChapterList = codeLeanChapterList;
                if (this.codeLeanChapterList != null)
                    totalDisplayDatasize = this.codeLeanChapterList.size();
                System.out.println("Inside ListViewCustomAdapter ");
            }

            @Override
            public int getCount() {
                // this could be one of the reason for not showing listview.set
                // total data length for count
                return totalDisplayDatasize;
            }

            @Override
            public codeLeanChapter getItem(int i) {
                return this.codeLeanChapterList.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            private class Holder {
                TextView textView1, textView2;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                Holder holder = null;
                View view = convertView;
                /*
                 * First time for row if view is not created than inflate the view
                 * and create instance of the row view Cast the control by using
                 * findview by id and store it in view tag using holder class
                 */
                if (view == null) {
                    holder = new Holder();
                    // / No need to create LayoutInflater instance in
                    // constructor

                    convertView = LayoutInflater.from(this.context).inflate(
                            R.layout.listitem, null);

                    holder.textView1 = (TextView) convertView
                            .findViewById(R.id.textView1);
                    holder.textView2 = (TextView) convertView
                            .findViewById(R.id.textView2);

                    convertView.setTag(holder);
                } else {
                    /*
                     * Here view next time it wont b null as its created and
                     * inflated once and in above if statement its created. And
                     * stored it in view tag. Get the holder class from view tag
                     */
                    holder = (Holder) convertView.getTag();

                }
                holder.textView1.setText("chapterDescription : "
                        + codeLeanChapterList.get(position).chapterDescription);
                holder.textView2.setText("chapterName : "
                        + codeLeanChapterList.get(position).chapterName);
                return convertView;
            }
        }
    }