本文介绍了如何存储在阵列中的所有CustomListView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义的ListView通过底座适配器如下code。其实我需要得到CustomListView的长度。感谢你的帮助提前

 公共类ListViewWithBaseAdapter延伸活动{

    公共类codeLeanChapter {
        字符串chapterName;
        字符串chapterDescription;
    }
    codeLearnAdapter chapterListAdapter;
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_list_view_with_simple_adapter);


        chapterListAdapter =新的codeLearnAdapter();

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

        codeLearnLessons.setOnItemClickListener(新OnItemClickListener(){

            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,
                    长ARG3){

                codeLeanChapter章= chapterListAdapter.get codeLearnChapter(ARG2);

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

            }
        });
    }

// --------------------------------------------------适配器类开始从这里 -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - --------------
    公共类codeLearnAdapter延伸BaseAdapter {

        名单< codeLeanChapter> codeLeanChapterList = getDataForListView();
        @覆盖
        公众诠释getCount将(){
            // TODO自动生成方法存根
            返回codeLeanChapterList.size();
        }

        @覆盖
        公共codeLeanChapter的getItem(INT为arg0){
            // TODO自动生成方法存根
            返回codeLeanChapterList.get(为arg0);
        }

        @覆盖
        众长getItemId(INT为arg0){
            // TODO自动生成方法存根
            返回将arg0;
        }

        @覆盖
        公共查看getView(INT为arg0,查看ARG1,ARG2的ViewGroup){

            如果(ARG1 == NULL)
            {
                LayoutInflater充气=(LayoutInflater)ListViewWithBaseAdapter.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ARG1 = inflater.inflate(R.layout.listitem,ARG2,假);
            }

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

            codeLeanChapter章= codeLeanChapterList.get(为arg0);

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

            返回ARG1;
        }

        公共codeLeanChapter得到codeLearnChapter(INT位置)
        {
            返回codeLeanChapterList.get(位置);
        }

    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.list_view_with_simple_adapter,菜单);
        返回true;
    }

    公开名单< codeLeanChapter> getDataForListView()
    {
        名单< codeLeanChapter> codeLeanChaptersList =新的ArrayList< codeLeanChapter>();

        的for(int i = 0;我小于10;我++)
        {

            codeLeanChapter章=新的codeLeanChapter();
            chapter.chapterName =章+我;
            chapter.chapterDescription =这是说明章+我;
            codeLeanChaptersList.add(章);
        }

        返回codeLeanChaptersList;

    }
}
 

解决方案

有关BaseAdapter我的建议是使用的模式为行项目创建。

如果你想获得列表视图的总长度超过设定的数据后,或基于列表的集合大小,你可以得到的。

在下面贴codeI刚才在getview类并在评论其所需

 公共类ListViewWithBaseAdapter延伸活动{

        ListView控件的ListView;
        公共类codeLeanChapter {
            字符串chapterName;
            字符串chapterDescription;
        }

        @覆盖
        保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_list_view_with_simple_adapter);
            ListView的=(的ListView)findViewById(R.id.listView1);
            ListViewCustomAdapter适配器=新ListViewCustomAdapter(这一点,
                    getDataForListView());
            listView.setAdapter(适配器);
            listView.setOnItemClickListener(新OnItemClickListener(){

                @覆盖
                公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,
                        长ARG3){

                    codeLeanChapter章= adapter.getItem(ARG2);

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

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

        公开名单< codeLeanChapter> getDataForListView(){
            名单< codeLeanChapter> codeLeanChaptersList =新的ArrayList< codeLeanChapter>();

            的for(int i = 0;我小于10;我++){

                codeLeanChapter章=新的codeLeanChapter();
                chapter.chapterName =章+我;
                chapter.chapterDescription =这是说明章+我;
                codeLeanChaptersList.add(章);
            }

            返回codeLeanChaptersList;

        }

        私有类ListViewCustomAdapter扩展了BaseAdapter {
            上下文语境;
            INT totalDisplayDatasize = 0;
            名单< codeLeanChapter> codeLeanChapterList;

            公共ListViewCustomAdapter(上下文的背景下,
                    名单< codeLeanChapter> codeLeanChapterList){
                this.context =背景;
                这codeLeanChapterList = codeLeanChapterList。
                如果(这一点。codeLeanChapterList!= NULL)
                    totalDisplayDatasize =这个codeLeanChapterList.size()。
                的System.out.println(内部ListViewCustomAdapter);
            }

            @覆盖
            公众诠释getCount将(){
                //这可能是原因之一不显示listview.set
                //总数据长度计数
                返回totalDisplayDatasize;
            }

            @覆盖
            公共codeLeanChapter的getItem(int i)以{
                返回此codeLeanChapterList.get(我)。
            }

            @覆盖
            众长getItemId(int i)以{
                返回我;
            }

            私有类持有人{
                TextView的textView1,textView2;
            }

            @覆盖
            公共查看getView(INT位置,查看convertView,ViewGroup中父){
                持有人440 = NULL;
                查看查看= convertView;
                / *
                 *第一次行,如果不创建视图,而不是膨胀的看法
                 *并创建行视图实例使用铸造控制
                 * findview通过标识和使用holder类存放在视图标签
                 * /
                如果(查看== NULL){
                    持有人=新的持有人();
                    // /无需在创建LayoutInflater实例
                    //构造

                    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(保持器);
                } 其他 {
                    / *
                     *这里查看下一次它不会b null作为其创建
                     *膨胀一次,并在上面的if语句的创建。和
                     *其存储在视图标签。获取从视图中标记的holder类
                     * /
                    支架=(座)convertView.getTag();

                }
                holder.textView1.setText(chapterDescription:
                        + codeLeanChapterList.get(位置).chapterDescription);
                holder.textView2.setText(chapterName:
                        + codeLeanChapterList.get(位置).chapterName);
                返回convertView;
            }
        }
    }
 

I create a Custom ListView through Base Adapter as code below.Actually i need to get length of CustomListView. Thanks for your kind help in 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;

    }
}
解决方案

For BaseAdapter my suggestion is to use ViewHolder pattern for row item creating.

If you wants to get total length of the listview than after setting data or based on the list collections size you can get.

In below posted code i have mentioned comments in getview class and where its required

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

这篇关于如何存储在阵列中的所有CustomListView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 02:07