想实现在ListFragment里面的ListView的item里面的Button的点击事件

来源:互联网 发布:mac上的三维软件 编辑:程序博客网 时间:2024/05/24 05:14
想实现在ListFragment里面的ListView的item里面的Button的点击事件
重写了BaseAdapter 然后再ListFragment中的onCreated()方法中setAdapter一直出错  求大神
package com.example.cms.lizixiaoyuan.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cms.lizixiaoyuan.Activity.Function_Biaoqian;
import com.example.cms.lizixiaoyuan.R;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by CMS on 2016/8/6.
 */
public class FunctionClassAdapter extends BaseAdapter {
    private class ViewHolder{
        ImageView f_c_rmnr_photo;
        TextView f_c_rmnr_title;
        TextView f_c_rmnr_bq;
        TextView f_c_rmnr_text1;
        TextView f_c_rmnr_text2;
        TextView f_c_rmnr_text3;
        TextView f_c_rmnr_num_comment;
        TextView f_c_rmnr_num_like;
        TextView f_c_rmnr_pf;
        TextView f_c_rmnr_dbq;
        TextView f_c_rmnr_author;
    }

    private List<Map<String, Object>> mAppList;
    private LayoutInflater mInflater;
    private Context mContext;
    private String[] keyString;
    private int[] valueViewID;
    private ViewHolder holder=null;

    public FunctionClassAdapter(Context c, List<Map<String, Object>> appList, int resource,
                                String[] from, int[] to) {
        mContext = c;
        mAppList = appList;
        mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        keyString = new String[from.length];
        valueViewID = new int[to.length];
        System.arraycopy(from, 0, keyString, 0, from.length);
        System.arraycopy(to, 0, valueViewID, 0, to.length);
    }
    @Override
    public int getCount() {
        return mAppList.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.f_c_list_item, null);
            holder = new ViewHolder();
            holder.f_c_rmnr_photo= (ImageView) convertView.findViewById(valueViewID[0]);
            holder.f_c_rmnr_title= (TextView) convertView.findViewById(valueViewID[1]);
            holder.f_c_rmnr_bq= (TextView) convertView.findViewById(valueViewID[2]);
            holder.f_c_rmnr_text1= (TextView) convertView.findViewById(valueViewID[3]);
            holder.f_c_rmnr_text2= (TextView) convertView.findViewById(valueViewID[4]);
            holder.f_c_rmnr_text3= (TextView) convertView.findViewById(valueViewID[5]);
            holder.f_c_rmnr_num_comment= (TextView) convertView.findViewById(valueViewID[6]);
            holder.f_c_rmnr_num_like= (TextView) convertView.findViewById(valueViewID[7]);
            holder.f_c_rmnr_pf= (TextView) convertView.findViewById(valueViewID[8]);
            holder.f_c_rmnr_dbq= (TextView) convertView.findViewById(valueViewID[9]);
            holder.f_c_rmnr_author= (TextView) convertView.findViewById(valueViewID[10]);
            convertView.setTag(holder);
        }
        HashMap<String, Object> appInfo = (HashMap<String, Object>) mAppList.get(position);
        if (appInfo != null) {
            int f_c_rmnr_photo= (Integer) appInfo.get(keyString[0]);
            String f_c_rmnr_title= (String) appInfo.get(keyString[1]);
            String f_c_rmnr_bq= (String) appInfo.get(keyString[2]);
            String f_c_rmnr_text1= (String) appInfo.get(keyString[3]);
            String f_c_rmnr_text2= (String) appInfo.get(keyString[4]);
            String f_c_rmnr_text3= (String) appInfo.get(keyString[5]);
            String f_c_rmnr_num_comment= (String) appInfo.get(keyString[6]);
            String f_c_rmnr_num_like= (String) appInfo.get(keyString[7]);
            String f_c_rmnr_pf= (String) appInfo.get(keyString[8]);
            String f_c_rmnr_dbq= (String) appInfo.get(keyString[9]);
            String f_c_rmnr_author= (String) appInfo.get(keyString[10]);


            holder.f_c_rmnr_photo.setImageResource(f_c_rmnr_photo);    ///??????????????????????/
            holder.f_c_rmnr_title.setText(f_c_rmnr_title);
            holder.f_c_rmnr_bq.setText(f_c_rmnr_bq);
            holder.f_c_rmnr_text1.setText(f_c_rmnr_text1);
            holder.f_c_rmnr_text2.setText(f_c_rmnr_text2);
            holder.f_c_rmnr_text3.setText(f_c_rmnr_text3);
            holder.f_c_rmnr_num_comment.setText(f_c_rmnr_num_comment);
            holder.f_c_rmnr_num_like.setText(f_c_rmnr_num_like);
            holder.f_c_rmnr_pf.setText(f_c_rmnr_pf);
            holder.f_c_rmnr_dbq.setText(f_c_rmnr_dbq);
            holder.f_c_rmnr_author.setText(f_c_rmnr_author);
            holder.f_c_rmnr_dbq.setOnClickListener(new bqListener(position));

        }

        return convertView;
    }

    class bqListener implements View.OnClickListener {
        private int position;

        bqListener(int pos) {
            position = pos;
        }

        @Override
        public void onClick(View v) {
            Toast.makeText(mContext, "position"+position, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 移动ListView中某一项的方法
     * @param position
     */
    public void removeItem(int position){
        mAppList.remove(position);
        this.notifyDataSetChanged();
    }
}

0 0
原创粉丝点击