当前位置:操作系统 > 安卓/Android >>

android之ListView与自定义adapter简单实例


 <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=".MainActivity" >  
    <ListView   
        android:id="@+id/lt1"  
        android:layout_height="wrap_content"  
        android:layout_width="match_parent"  
        android:divider="#f00"  
        android:dividerHeight="2px"  
        android:headerDividersEnabled="true"  
        android:footerDividersEnabled="true"></ListView>  
    <ListView   
        android:id="@+id/lt2"  
        android:layout_marginTop="20px"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:divider="#0f0"  
        android:dividerHeight="2px"  
        android:headerDividersEnabled="true"  
        android:footerDividersEnabled="true"></ListView>  
      
       <ListView   
        android:id="@+id/lt3"  
        android:layout_marginTop="20px"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"></ListView>  
  
  
</LinearLayout>  


 

package com.android.xiong.arrayadaptertest;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup;  
import android.widget.ArrayAdapter;  
import android.widget.BaseAdapter;  
import android.widget.Button;  
import android.widget.LinearLayout;  
import android.widget.ListView;  
import android.widget.TextView;  
  
public class MainActivity extends Activity {  
  
    private ListView lt1;  
    private ListView lt2;  
    private ListView lt3;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
  
        setContentView(R.layout.activity_main);  
        lt1 = (ListView) findViewById(R.id.lt1);  
        lt2 = (ListView) findViewById(R.id.lt2);  
        lt3 = (ListView) findViewById(R.id.lt3);  
        String[] list1 = { "张三", "李四啊", "王五" };  
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,  
                R.layout.texton, list1);  
        lt1.setAdapter(adapter1);  
        String[] list2 = { "java", "C#", "C" };  
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,  
                R.layout.checkon, list2);  
        lt2.setAdapter(adapter2);  
        // 自定义列表项 实现BaseAdapter接口 重写方法  
        BaseAdapter adapter3 = new BaseAdapter() {  
  
            @Override  
            public View getView(int position, View convertView, ViewGroup parent) {  
                // TODO Auto-generated method stub  
                LinearLayout linear = new LinearLayout(MainActivity.this);  
                linear.setOrientation(0);  
                final TextView txt1 = new TextView(MainActivity.this);  
                txt1.setText(String.valueOf(position));  
                final String str = String.valueOf(position);  
                final Button bt1 = new Button(MainActivity.this);  
                bt1.setText("详情");  
                bt1.setOnClickListener(new OnClickListener() {  
  
                    @Override  
                    public void onClick(View v) {  
                        // TODO Auto-generated method stub  
                        // 控制单击了Button按钮  
                        if (!str.equals("2"))  
                            txt1.setText("赋值给:" + str);  
  
                    }  
                });  
                linear.addView(txt1);  
                linear.addView(bt1);  
                return linear;  
            }  
  
            @Override  
            public long getItemId(int position) {  
                // TODO Auto-generated method stub  
                return position;  
            }  
  
            @Override  
            public Object getItem(int position) {  
                // TODO Auto-generated method stub  
                return null;  
            }  
  
            @Override  
            public int getCount() {  
                // TODO Auto-generated method stub  
                return 4;  
            }  
        };  
        lt3.setAdapter(adapter3);  
  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);  
        return true;  
    }  
  
}  

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,