【问题标题】:How to store All the CustomListView item in an Array如何将所有 CustomListView 项存储在数组中
【发布时间】:2015-01-23 05:40:59
【问题描述】:

我通过基本适配器创建了一个自定义 ListView,如下代码所示。 实际上我需要获取 CustomListView 的长度。感谢您提前提供的帮助

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;

    }
}

【问题讨论】:

  • 自定义列表视图的长度?
  • 如果我正确理解您的问题,这将返回您的列表视图长度。 codeLeanChapterList.size().
  • 其实我做了一个CustomListView如上。我需要知道我的 ListView 中有多少项。所以问题是“如何获取 ListView 中的项目数并在 Toast 中显示”。就是这样
  • 您的适配器中有一个 getCount() 方法,它应该会告诉您这一点。
  • 列表的大小将是列表视图的长度

标签: android


【解决方案1】:

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

如果您想获取列表视图的总长度,而不是设置数据后或根据您可以获得的列表集合大小。

int totalListViewsize = adapter.getCount();

在下面发布的代码中,我提到了 getview 类中的 cmets 以及它需要的位置

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

【讨论】:

  • 我无法回答,因为我没有足够的声誉:(
猜你喜欢
  • 2013-09-02
  • 2021-02-15
  • 2014-03-31
  • 2022-11-03
  • 1970-01-01
  • 1970-01-01
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多