Android_ListView简单例子

 ListView是Android软件开发中非常重要组件之一,基本上是个软件基本都会使用ListView ,今天我通过一个demo来教大家怎么样使用ListView组件

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.asynctask.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button 
         android:id="@+id/Button"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_below="@+id/textView1"
         android:text="按钮"
        />
    <!-- ListView 由item.xml里的TextView给值 。-->
    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Button"
   />
    

</RelativeLayout>

 效果图:

 

上下两个xml文件要结合使用。 item.xml

<?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"
    android:orientation="horizontal" >
    
    <!-- 下面两个是用来给  activity_main.xml里的ListView赋值,-->
 <TextView
     android:id="@+id/Textviewname"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
       android:text="1"
    />
 <TextView
     android:id="@+id/Textviewage"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
      android:layout_weight="1"
     android:text="2"
    />
</LinearLayout>

效果图:

 

 

 java代码:

MainActivity.java

package com.example.asynctask;

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


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity {
    private TextView tv1;//item.xml里的TextView:Textviewname
    private TextView tv2;//item.xml里的TextView:Textviewage
    private Button bt;//activity_main.xml里的Button
    private ListView lv;//activity_main.xml里的ListView
    private BaseAdapter adapter;//要实现的类
    private List<User> userList = new ArrayList<User>();//实体类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        bt = (Button) findViewById(R.id.Button);
        lv = (ListView) findViewById(R.id.listView1);
      
        //模拟数据库
        for (int i = 0; i < 5; i++) {
            User ue = new User();//给实体类赋值
            ue.setName("小米"+i);
            ue.setAge("18");
            userList.add(ue);
        }
      
        /*  bt.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
            
                
            }
        });*/
        
        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return userList.size();//数目
            }
            
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = MainActivity.this.getLayoutInflater();
                View view;
                 
                if (convertView==null) {
                    //因为getView()返回的对象,adapter会自动赋给ListView
                    view = inflater.inflate(R.layout.item, null);
                }else{
                    view=convertView;
                    Log.i("info","有缓存,不需要重新生成"+position);
                }
                tv1 = (TextView) view.findViewById(R.id.Textviewname);//找到Textviewname
                tv1.setText(userList.get(position).getName());//设置参数
                
                tv2 = (TextView) view.findViewById(R.id.Textviewage);//找到Textviewage
                tv2.setText(userList.get(position).getAge());//设置参数
                return view;
            }
            @Override
            public long getItemId(int position) {//取在列表中与指定索引对应的行id
                return 0;    
            }
            @Override
            public Object getItem(int position) {//获取数据集中与指定索引对应的数据项
                return null;
            }        
        };
        lv.setAdapter(adapter);
        
     
    //获取当前ListView点击的行数,并且得到该数据


    
} }

 

   //获取当前ListView点击的行数,并且得到该数据
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv1 = (TextView) view.findViewById(R.id.Textviewname);//找到Textviewname
                str = tv1.getText().toString();//得到数据
                Toast.makeText(MainActivity.this, "" + str, Toast.LENGTH_SHORT).show();//显示数据


                Intent it = new Intent(MainActivity.this, list0.class); //
                Bundle b = new Bundle();
                b.putString("we",str);  //string
                // b.putSerializable("dd",str);
                // it.putExtra("str_1",str);
                it.putExtras(b);
                startActivity(it);


            }
        });

 

 

 

 

实体类:

  

package com.example.asynctask;

public class User {
    private String name;
    private String age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + "]";
    }
    
    
}

 

 

最后结果:

 

posted @ 2016-12-05 19:16  小鱼干的梦  阅读(29329)  评论(1编辑  收藏  举报