ArrayAdapter简单实现 Listview

ArrayAdapter简单实现 Listview

你现在受的苦,未来都会照亮你的路,告诉所有正在路上奋斗的人,即使路途遥远,即使梦想遥遥无期,但只要坚持下去,总会得到最好的结果……

package com.crazyit.ui.adapterview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ArrayAdapterActivity extends AppCompatActivity {

    private ListView lv1,lv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_array_adapter);
        //初始化控件
        lv1 = (ListView) findViewById(R.id.lv1);
        lv2 = (ListView) findViewById(R.id.lv2);

        //定义一个数组
        String[] str1 = new String[]{"魏国","蜀国","吴国"};

        //将数组包装成ArrayAdapter
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplication(),R.layout.array_item,str1);
        //为listView设置Adapter
        lv1.setAdapter(adapter1);

        //定义一个数组
        String[] str2 = new String[]{"JAVA","Android","HTML","PHP"};
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,R.layout.checked_item,str2);
     //为listView设置Adapter
        lv2.setAdapter(adapter2);
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.crazyit.ui.adapterview.ArrayAdapterActivity">

    <!--使用红色分割线-->
    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#f00"  //设置分割线
        android:dividerHeight="3px"
        android:headerDividersEnabled="false" />

    <!--使用绿色分割线-->
    <ListView
        android:id="@+id/lv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#0f0"
        android:dividerHeight="3px"
        android:headerDividersEnabled="false" />

</LinearLayout>

下面是Adapter提供的列表项的数据

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:gravity="center_horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:padding="10dp"
    android:textColor="#409"
    android:shadowColor="#f0f"
    android:shadowDy="3"  //设置阴影的颜色和宽度
    android:shadowDx="3"
    android:shadowRadius="3"

    />
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:checkMark="@drawable/ok"
    android:shadowColor="#00f"
    android:shadowDx="3"
    android:shadowDy="3" //设置阴影的颜色和宽度
    android:shadowRadius="3"
    android:textSize="24sp">
</CheckedTextView>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值