在Custom BaseAdapter中实现onClick Listener

时间:2015-02-14 22:53:39

标签: java android android-sqlite

我有一个包含多列的故障单项目的列表视图。我试图弄清楚如何使用我所写的内容来实现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相矛盾。

1 个答案:

答案 0 :(得分:1)

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

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
        }
    });