旺仔练习,Listview(一)

package com.hanqi.bluetooth;

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

import java.util.List;

public class TestListView extends AppCompatActivity {
    ListView lv_1;
    ArrayAdapter<Listdata> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_list_view);
        lv_1=(ListView)findViewById(R.id.lv_1);
       // adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1);
       adapter=new ArrayAdapter<Listdata>(this,R.layout.listview);
        adapter.add(new Listdata("张三","男",10));
        adapter.add(new Listdata("李四","女",11));
        lv_1.setAdapter(adapter);
        lv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Listdata listdata=adapter.getItem(position);
                Toast.makeText(TestListView.this, String.format("名字:"+listdata.getName()+
                                " 性别:"+listdata.getSex()+" 年龄:"+listdata.getAge()),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}
package com.hanqi.bluetooth;

/**
 * Created by Administrator on 2016/7/18.
 */
public class Listdata  {
    private String name;
    private String sex;
    private int age;

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Listdata(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    @Override
    public String toString() {
      //  return "Listdata{" +
       //         "name='" + name + '\'' +
         //       '}';
        return getName();
    }
}
<?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.hanqi.bluetooth.TestListView">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_1"></ListView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

</TextView>

 

posted @ 2016-07-18 11:52  什么玩楞啊,我叫旺仔  阅读(120)  评论(0)    收藏  举报