在自定义 BaseAdapter 中实现 onClick 监听器

Implement onClick Listener in Custom BaseAdapter

我有一个多列的工单项目列表视图。我正在尝试弄清楚如何使用我编写的内容实现 onclick 侦听器。

Fragment_OpenTickets.java:

public class Fragment_OpenTickets extends Fragment {

ArrayList<HashMap<String, String>> ticketList = new ArrayList<HashMap<String, String>>();

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    Context context = getActivity();
    DBController dbc = new DBController(context);
    View view = inflater.inflate(R.layout.fragment_opentickets, container, false);
    ListView listView = (ListView) view.findViewById(R.id.openList);
    Cursor cursor = dbc.getAllOpenTickets();

    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            int size = cursor.getColumnCount();
            for (int i = 0; i < size; i++) {
                hashMap.put(cursor.getColumnName(i), cursor.getString(i));
            }
            ticketList.add(hashMap);
        } while (cursor.moveToNext());
    }
    Adapter_OpenList adapter = new Adapter_OpenList(ticketList, context);
    listView.setAdapter(adapter);
    dbc.close();

    return view;
}
}

Adapter_OpenList.java:

public class Adapter_OpenList extends BaseAdapter {
ArrayList<HashMap<String, String>> ticketList;
Context context;
LayoutInflater mInflater;

public Adapter_OpenList(ArrayList<HashMap<String, String>> arr, Context c){
    ticketList = arr;
    context = c;
    mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return ticketList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return ticketList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if(convertView == null) {
        convertView = mInflater.inflate(R.layout.fragment_openticket_row, null);
        holder = new ViewHolder();
        holder.txtTicketNo = (TextView)convertView.findViewById(R.id.row_ticketNo);
        holder.txtTicketDate = (TextView)convertView.findViewById(R.id.row_ticketDate);
        holder.txtLease = (TextView)convertView.findViewById(R.id.row_lease);
        holder.txtDelivLoc = (TextView)convertView.findViewById(R.id.row_delivLoc);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    HashMap map = ticketList.get(position);
    holder.txtTicketNo.setText((CharSequence) map.get("ticketNo"));
    holder.txtTicketDate.setText((CharSequence) map.get("ticketDate"));
    holder.txtLease.setText((CharSequence) map.get("lease"));
    holder.txtDelivLoc.setText((CharSequence) map.get("delivLoc"));

    return convertView;
}

private class ViewHolder {
    TextView txtTicketNo;
    TextView txtTicketDate;
    TextView txtLease;
    TextView txtDelivLoc;
}
}

如果用户点击列表中的工单行,我需要获取工单的 SQLite ID 以查看它或启动工单。我知道有不同的方法可以做到这一点,但我只是想把基础知识记下来,这样我就可以玩了。谢谢!

更新: 我通过使用 Johan 提供的代码并删除适配器正在使用的 fragment_openticket_row.xml 文件中的按钮解决了这个问题。它与 onClick 冲突。

一种简单的方法是在您的 createView 方法中创建一个 ItemClickListener,如下所示:

listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
            //Here you can get the position and access your 
            //TicketList Object
        }
    });