如何将所有CustomListView项目存储在数组中

Sachindra N. Pandey

我通过下面的代码通过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;

    }
}
用户名

对于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;
            }
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将所有字典存储在数组中

来自分类Dev

如何将所有选中的复选框类名称存储在数组中?

来自分类Dev

循环文件时如何将所有答案存储在数组中

来自分类Dev

如何将所有edittext值存储在数组列表中

来自分类Dev

如何将 CustomListView 中的所有复选框从 GONE 更改为 VISIBLE?

来自分类Dev

如何将表格值存储在数组中?

来自分类Dev

如何将值存储在数组中

来自分类Dev

如何将文件内容存储在数组中?

来自分类Dev

如何将引用存储在数组中?

来自分类Dev

如何将类型存储在数组中?

来自分类Dev

如何将解析后的数据存储在数组中?

来自分类Dev

如何将接收到的数据存储在数组中?

来自分类Dev

如何将文件内容存储在数组中?

来自分类Dev

如何将单选按钮的值存储在数组中

来自分类Dev

如何将值存储在数组 Jquery 中?

来自分类Dev

Java如何将JOptionPane值存储在数组中

来自分类Dev

如何将列表中所有选中的项目存储到本地存储中。(Javascript)

来自分类Dev

如何将项目顺序放置在数组中?(javascript)

来自分类Dev

如何将所有数组存储在 for 循环输出中

来自分类Dev

从.data文件读取到Java中,如何将信息存储在数组数组中?

来自分类Dev

从.data文件读取到Java中,如何将信息存储在数组数组中?

来自分类Dev

Scala如何将数组中的所有项目拆分为另一个不可变数组

来自分类Dev

如何将数据存储在数组(JavaScript)中的承诺中?

来自分类Dev

如何将值存储在数组中并将它们保存在文件中

来自分类Dev

如何将列表框中的所有选定数据存储在 VBA 中的数组中

来自分类Dev

如何将项目放在数组中的不同复选框中?

来自分类Dev

如何使用 $_SESSION 将项目存储在数组中并触发按钮交换?

来自分类Dev

在VBA中,如何将整个excel表存储到包含所有相关子信息的易于访问的数组中?

来自分类Dev

如何找出DOM中的所有角度选择器并存储在数组中?

Related 相关文章

  1. 1

    如何将所有字典存储在数组中

  2. 2

    如何将所有选中的复选框类名称存储在数组中?

  3. 3

    循环文件时如何将所有答案存储在数组中

  4. 4

    如何将所有edittext值存储在数组列表中

  5. 5

    如何将 CustomListView 中的所有复选框从 GONE 更改为 VISIBLE?

  6. 6

    如何将表格值存储在数组中?

  7. 7

    如何将值存储在数组中

  8. 8

    如何将文件内容存储在数组中?

  9. 9

    如何将引用存储在数组中?

  10. 10

    如何将类型存储在数组中?

  11. 11

    如何将解析后的数据存储在数组中?

  12. 12

    如何将接收到的数据存储在数组中?

  13. 13

    如何将文件内容存储在数组中?

  14. 14

    如何将单选按钮的值存储在数组中

  15. 15

    如何将值存储在数组 Jquery 中?

  16. 16

    Java如何将JOptionPane值存储在数组中

  17. 17

    如何将列表中所有选中的项目存储到本地存储中。(Javascript)

  18. 18

    如何将项目顺序放置在数组中?(javascript)

  19. 19

    如何将所有数组存储在 for 循环输出中

  20. 20

    从.data文件读取到Java中,如何将信息存储在数组数组中?

  21. 21

    从.data文件读取到Java中,如何将信息存储在数组数组中?

  22. 22

    Scala如何将数组中的所有项目拆分为另一个不可变数组

  23. 23

    如何将数据存储在数组(JavaScript)中的承诺中?

  24. 24

    如何将值存储在数组中并将它们保存在文件中

  25. 25

    如何将列表框中的所有选定数据存储在 VBA 中的数组中

  26. 26

    如何将项目放在数组中的不同复选框中?

  27. 27

    如何使用 $_SESSION 将项目存储在数组中并触发按钮交换?

  28. 28

    在VBA中,如何将整个excel表存储到包含所有相关子信息的易于访问的数组中?

  29. 29

    如何找出DOM中的所有角度选择器并存储在数组中?

热门标签

归档