关闭

Android 使用adapter的两种回调数据方式

标签: listviewadapter数据回
292人阅读 评论(0) 收藏 举报
分类:

1、首先创建一个listView列表,用于显示数据:

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

item布局使用简单的一行文字显示,主要是将点击的位置回调回使用listView的类文件之中,item.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">

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="10dp" />

</RelativeLayout>

2、回调数据分为两种方式,第一种是在adapter之中写一个回调接口,在点击item的时候使用接口回调,将点击的position传递给activity之中使用的adapter,在adapter中使用定义的回调方法获取到传递的position。第二种是在activity之中传递数据到adapter的时候将handler一并传进去,使用handler获取到点listView点击之时的position传递。

(1)使用接口回调:

i、自定义适配器MyAdapter:

public class MyAdapter extends BaseAdapter {

private List<String> list;
private Context context;
private LayoutInflater inflater;
private onClickMyTextView onClickMyTextView;

public MyAdapter(Context context, List<String> list) {
    super();
    inflater = LayoutInflater.from(context);
    this.list = list;
    this.context = context;
}

//接口回调
public interface onClickMyTextView{
    public void myTextViewClick(int id);
}
public void setOnClickMyTextView(onClickMyTextView onClickMyTextView) {
    this.onClickMyTextView = onClickMyTextView;
}


@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder = null;
    if (viewHolder==null) {
        viewHolder = new ViewHolder();
        convertView = inflater.inflate(R.layout.item, null);
        viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);

        convertView.setTag(viewHolder);
    }else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.textView.setText(list.get(position).toString());

    if (onClickMyTextView!=null) {
        viewHolder.textView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onClickMyTextView.myTextViewClick(position);

            }
        });
    }


    return convertView;
}

    class ViewHolder{
        TextView textView;
    }

}

使用自定义接口设置:

public interface onClickMyTextView{
    public void myTextViewClick(int id);
}
public void setOnClickMyTextView(onClickMyTextView onClickMyTextView) {
    this.onClickMyTextView = onClickMyTextView;
}

点击的时候使用接口传递数据:

if (onClickMyTextView!=null) {
        viewHolder.textView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onClickMyTextView.myTextViewClick(position);

            }
        });
    }

ii、在MainActivity之中使用adapter:

public class MainActivity extends Activity {

private ListView listView;
private List<String> list;
private MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.listView);

    initData();

    adapter = new MyAdapter(MainActivity.this, list);
    listView.setAdapter(adapter);
    //接口的调用,获取传递的id
    adapter.setOnClickMyTextView(new onClickMyTextView() {

        @Override
        public void myTextViewClick(int id) {
            Toast.makeText(MainActivity.this, list.get(id).toString(), Toast.LENGTH_SHORT).show();

        }
    });
}

    // 列表模拟数据
    private void initData() {
        list = new ArrayList<String>();
        for (int i = 0; i < 20; i++) {
            list.add("添加的数据条目=" + i);
        }

    }

}

(2)、使用Handler回调数据:
i、自定义适配器MyAdapter:

public class MyAdapter extends BaseAdapter {

public static final String POSITION = "POSITION";
public static final int DELETE = 1;

private List<String> list;
private Context context;
private LayoutInflater inflater;
private Handler handler;

public MyAdapter(Context context, List<String> list, Handler handler) {
    super();
    inflater = LayoutInflater.from(context);
    this.list = list;
    this.context = context;
    this.handler = handler;
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder = null;
    if (viewHolder == null) {
        viewHolder = new ViewHolder();
        convertView = inflater.inflate(R.layout.item, null);
        viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.textView.setOnClickListener(new OnItemChildClickListener(DELETE, position));

    return convertView;
}

class ViewHolder {
    TextView textView;
}

// handler的点击事件
private class OnItemChildClickListener implements View.OnClickListener {
    // 点击类型索引,对应前面的CLICK_INDEX
    private int clickIndex;
    // 点击列表位置
    private int position;

    public OnItemChildClickListener(int clickIndex, int position) {
        this.clickIndex = clickIndex;
        this.position = position;
    }

    @Override
    public void onClick(View v) {
        // 创建Message并填充数据,通过handle联系Activity接收处理
        Message msg = new Message();
        msg.what = clickIndex;
        msg.arg1 = position;
        Bundle b = new Bundle();
        b.putInt(POSITION, position);
        msg.setData(b);
        handler.sendMessage(msg);
        }

    }

}

点击事件的使用:
viewHolder.textView.setOnClickListener(new OnItemChildClickListener(DELETE, position));

点击方法,将position传递到handler之中:

// handler的点击事件
private class OnItemChildClickListener implements View.OnClickListener {
    // 点击类型索引,对应前面的CLICK_INDEX
    private int clickIndex;
    // 点击列表位置
    private int position;

    public OnItemChildClickListener(int clickIndex, int position) {
        this.clickIndex = clickIndex;
        this.position = position;
    }

    @Override
    public void onClick(View v) {
        // 创建Message并填充数据,通过handle联系Activity接收处理
        Message msg = new Message();
        msg.what = clickIndex;
        msg.arg1 = position;
        Bundle b = new Bundle();
        b.putInt(POSITION, position);
        msg.setData(b);
        handler.sendMessage(msg);
    }

}

ii、在MainActivity之中使用adapter:

public class MainActivity extends Activity {

/**
 * 显示数据的列表
 */
private ListView listView;
/**
 * 模拟数据的集合
 */
private List<String> list;
/**
 * 自定义的适配器
 */
private MyAdapter adapter;
/**
 * 回传回来的列表位置
 */
private int mPosition;

private Handler handler = new Handler() {
    public void handleMessage(Message msg) {

        // 回传的是此item的bean,对当前bean中的数据能够进行操作,位置传过来之后还能将数据进行操作
        mPosition = msg.getData().getInt(MyAdapter.POSITION);

        switch (msg.what) {
        case MyAdapter.DELETE:
            Toast.makeText(MainActivity.this, "点击回传的是:" + list.get(mPosition), Toast.LENGTH_SHORT).show();
            break;

        default:
            break;
        }
    };
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.listView);

    initData();

    adapter = new MyAdapter(MainActivity.this, list, handler);
    listView.setAdapter(adapter);
}

private void initData() {
    list = new ArrayList<String>();
    for (int i = 0; i < 20; i++) {
        list.add("添加的数据条目=" + i);
        }

    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19508次
    • 积分:825
    • 等级:
    • 排名:千里之外
    • 原创:65篇
    • 转载:2篇
    • 译文:1篇
    • 评论:3条
    最新评论