android控件ListView的一个上手小练习

1.在新建的android项目中的activity_main.xml页面中写ListView控件:(代码如下)




    
    

 2.在res目录下的layaut目录下新建一个xml文件的layout.xml文件,命名为listview_item.xml(代码如下)




    

    

 3.在java目录下新建一个java class命名为MyAdapter:(代码如下)方法主要用来实现对数据的渲染

package com.example.test;

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.List;

public class MyAdapter extends BaseAdapter {
    Listlist;
    Context context;
    public MyAdapter(List list,Context context){
        this.list=list;
        this.context=context;
    }
    @Override
    public int getCount() {
        return list.size();
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }
//根据list内容进行渲染,有多少行数据,自动调用多少次,每调用一次position改变一次,position计数
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.listview_item,null);
        }
        TextView textViewNo=(TextView)convertView.findViewById(R.id.listview_item_textview_no);
        textViewNo.setText(list.get(position).getNo());
        TextView textViewName=(TextView)convertView.findViewById(R.id.listview_item_textview_name);
        textViewName.setText(list.get(position).getName());
        return convertView;
    }
}

 4.同样在java目录下新建一个java class命名为Student:(代码如下)

package com.example.test;

public class Student {
    private String no;
    private String name;

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 5.在主类MainActivity中写入实现和监听函数:(代码如下)

package com.example.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=(ListView)findViewById(R.id.activity_main_listview_show);
        final Listlist=new ArrayList<>();
        Student s1=new Student();
       s1.setNo("1000");
        s1.setName("张珊");
        list.add(s1);
        Student s2=new Student();
        s2.setNo("2000");
        s2.setName("李斯");
        list.add(s2);
        Student s3=new Student();
        s3.setNo("3000");
        s3.setName("王舞");
        list.add(s3);
        MyAdapter myAdapter=new MyAdapter(list,MainActivity.this);
        listView.setAdapter(myAdapter);
        //给listview添加一个监听
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,list.get(position).getName(),Toast.LENGTH_LONG).show();
            }
        });
    }
}

 

 6.保存运行即可(如下图)

android控件ListView的一个上手小练习_第1张图片 

 

 

 

 

 

你可能感兴趣的:(android控件ListView的一个上手小练习)