4.9

所花时间(包括上课):2.5

打码量(行):300

博客量(篇):1

了解到知识点:学习列表视图Listview

 

 <!-- activity_main.xml -->

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

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

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <ListView

        android:id="@+id/listView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

</RelativeLayout>

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;

import java.util.ArrayList;

 

public class CustomAdapter extends BaseAdapter {

 

    private ArrayList<String> mData;

    private LayoutInflater mInflater;

 

    public CustomAdapter(Context context, ArrayList<String> data) {

        mData = data;

        mInflater = LayoutInflater.from(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) {

        ViewHolder holder;

 

        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.list_item, parent, false);

            holder = new ViewHolder();

            holder.textView = convertView.findViewById(R.id.textView);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }

 

        String item = mData.get(position);

        holder.textView.setText(item);

 

        return convertView;

    }

 

    private static class ViewHolder {

        TextView textView;

    }

}

<!-- list_item.xml -->

<TextView 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="18sp"

    android:padding="10dp"

    android:textColor="@android:color/black"/>

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.widget.ListView;

import java.util.ArrayList;

import java.util.Arrays;

 

public class MainActivity extends AppCompatActivity {

 

    private ListView mListView;

    private CustomAdapter mAdapter;

    private ArrayList<String> mData;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        mListView = findViewById(R.id.listView);

        mData = new ArrayList<>(Arrays.asList("Item 1", "Item 2", "Item 3", "Item 4", "Item 5"));

        mAdapter = new CustomAdapter(this, mData);

 

        mListView.setAdapter(mAdapter);

    }

}

posted @ 2024-04-09 16:25  赵千万  阅读(3)  评论(0编辑  收藏  举报