1. 程式人生 > >BaseAdapter實現ListView中item上每個控制元件的獲取以及點選

BaseAdapter實現ListView中item上每個控制元件的獲取以及點選

借鑑這位大神的程式碼,自己稍微修改了一下(其實也沒改多少可憐),把原始碼出來大家看看,剛開始學習android,有很多不懂的地方,望大家多多指教

先來一張效果圖,點選任何一個button,獲取button的值,具體動作,大家可以自行修改

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

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1">

        </ListView>

</RelativeLayout>

layout_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"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        />

</LinearLayout>

MainAcitvity.java

package com.example.listview_test_1;

import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends ActionBarActivity {
    public static final String[] s1_string=new String[]{"第一個","第二個","第三個","第四個"};
    public static final String[] button_string=new String[]{"B1","B2","B3","B4"};
    private ListView lv;
    private List<Map<String,Object>> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView)findViewById(R.id.listView1);
        data=getData();
        MyAdapter adapter=new MyAdapter(this);
        lv.setAdapter(adapter);

    }
    private List<Map<String,Object>> getData(){
        List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
        Map<String,Object> map;
        for(int i=0;i<4;i++){
            map=new HashMap<String, Object>();
            map.put("title",s1_string[i]);
            map.put("button",button_string[i]);
            list.add(map);
        }
        return list;
    }
    static class ViewHolder{
        public TextView title;
        public Button button;
    }
    public class MyAdapter extends BaseAdapter{
        private LayoutInflater mInflater=null;
        private MyAdapter(Context context){
            this.mInflater=LayoutInflater.from(context);
        }
        public int getCount(){
            return data.size();
        }
        public Object getItem(int position){
            return position;
        }
        public long getItemId(int position){
            return position;
        }
        public  View getView(int position,View convertView,ViewGroup viewGroup){
            final ViewHolder viewHolder;
            if(convertView==null){
                viewHolder=new ViewHolder();
                convertView=mInflater.inflate(R.layout.layout,null);//載入佈局檔案
                viewHolder.title=(TextView)convertView.findViewById(R.id.text);//佈局中的TextView
                viewHolder.button=(Button)convertView.findViewById(R.id.button1);//佈局中的Button
                convertView.setTag(viewHolder);

            }else {
                viewHolder=(ViewHolder)convertView.getTag();
            }
            //傳入data資料
            viewHolder.title.setText((String)data.get(position).get("title"));
            viewHolder.button.setText((String)data.get(position).get("button"));

            viewHolder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this,viewHolder.button.getText().toString(),Toast.LENGTH_SHORT).show();
                }
            });
            return convertView;
        }


    }






    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}