Android学习随笔ListView

首先创建一个简单的类

public class Xx {
    private String title;

    private int imageId;

    public Xx(String title, int imageId) {
        this.title = title;
        this.imageId = imageId;
    }

    public String getTitle() {
        return title;
    }

    public int getImageId() {
        return imageId;
    }
}

-----------------------------------------------------------

       创建父项活动布局 同时为启动活动main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
 

--子活动布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/xx_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/xx_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp" />
</LinearLayout>
--------------------------------------------------------

主活动

public class XX_Activity extends AppCompatActivity {
    private List<Xx> xx=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xx_layout);
        initXX(xx);//init
        ((ListView)findViewById(R.id.list_view)).setAdapter(new XXAdapter(XX_Activity.this,R.layout.XX_item_layout,xx));
    }
    private static void initXX(List<Xx> xxList){
        for (int i = 0; i < 10; i++){
            xx.add(new Xx("1",R.drawable.xx_pic));
        }
    }
}
--------------适配器------------------------

 

public class XXAdapter extends ArrayAdapter<Xx>{
private int resourceId;

public XXAdapter(Context context, int textViewResourceId, List<Xx> objects) {
    super(context, textViewResourceId,objects);
    resourceId=textViewResourceId;
}

private static final String TAG = "XxAdapter";
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
    ((ImageView)view.findViewById(R.id.xx_image)).setImageResource(getItem(position).getImageId());
    ((TextView)view.findViewById(R.id.xx_name)).setText(getItem(position).getName());
    return view;
}
-------------------------代码照抄运行估计不对,错误就看天份咯

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值