adapter

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}"
    xmlns:android="http://schemas.android.com/apk/res/android">
 <Button
        android:id="@+id/lv_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="chaxun"
        android:text="查询"/>
    <ListView
        android:id="@+id/lv_arrayAdapter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

//子布局

<?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="vertical" >
<TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#00A600"
    android:text="TextView" />

</LinearLayout>

package com.bw.thirdListArrayadapter;

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

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
  ListView lv;
 
 
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView)findViewById(R.id.lv_arrayAdapter);
        //第一个参数:上下文
        //第二步不
        //String objects[]=new String[]{"张三","李四","王五","赵六"};
       // ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.lv_item,objects );
     
    }
   
    public void chaxun(View view) {
     Integer objects[]=new Integer[]{1223,333,1222,444};
        //ArrayAdapter<Integer> adapter=new ArrayAdapter<Integer>(getApplicationContext(), R.layout.lv_item,objects );
     //context
     //resource
     //textViewResourceId:子布局里面,用哪个textView显示条目;
     //objects
     
     ArrayAdapter<Integer> adapter=new ArrayAdapter<Integer>(this, R.layout.lv_item, R.id.tv_name,objects);
     
     /*List<Integer> objects=new ArrayList<Integer>();
     objects.add(1000);
     objects.add(1001);
     objects.add(1002);
     objects.add(1003);*/
     //ArrayAdapter<Integer> adapter=new ArrayAdapter<Integer>(this, R.layout.lv_item, R.id.tv_name,objects);
       
        lv.setAdapter(adapter);
 }
}

 

posted @ 2017-10-31 09:50  安琪儿。流苏  阅读(80)  评论(0)    收藏  举报