ListView和BaseAdapter

 参考资料:http://www.runoob.com/w3cnote/android-tutorial-listview.html

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.expample.myapplication.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/mainView_id"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:id="@+id/subView_id"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#000000"/>

</LinearLayout>
复制代码

 

复制代码
public class MainActivity extends Activity {
    ListView lv;
    List ls;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView)findViewById(R.id.listView_id);
        ls = new LinkedList<animals>();
        ls.add(new animals("猪说", "我是猪吗?"));
        ls.add(new animals("猫说", "我是猫吗?"));
        ls.add(new animals("狗说", "我是狗吗?"));
        ls.add(new animals("熊说", "我是熊吗?"));
        ls.add(new animals("兔说", "我是兔吗?"));
        ls.add(new animals("虎说", "我是虎吗?"));
        ls.add(new animals("鼠说", "我是鼠吗?"));
        ls.add(new animals("鸡说", "我是鸡吗?"));
        ls.add(new animals("鱼说", "我是鱼吗?"));
        MyAdapter adapter = new MyAdapter((LinkedList<animals>)ls, MainActivity.this);

        lv.setAdapter(adapter);
    }

    class animals{
        String main;
        String sub;

        animals(String main, String sub){
            this.main = main;
            this.sub = sub;
        }

        String getMain(){
            return main;
        }

        String getSub(){
            return sub;
        }
    }
    class MyAdapter extends BaseAdapter{
        LinkedList<animals> mdata;
        Context mcontext;
        MyAdapter(LinkedList<animals> data, Context context){
            this.mdata = data;
            this.mcontext = context;
        }
        @Override
        public int getCount() {
            return mdata.size();
        }

        @Override
        public Object getItem(int position) {
            return mdata.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView main;
            TextView sub;

            LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
            convertView = layoutInflater.inflate(R.layout.item_list_animal, null, false);
            main = (TextView) convertView.findViewById(R.id.mainView_id);
            sub = (TextView)convertView.findViewById(R.id.subView_id);

            main.setText(mdata.get(position).getMain());
            sub.setText(mdata.get(position).getSub());
            return convertView;
        }
    }
}
复制代码

 

 

posted on   maogefff  阅读(144)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示