Reputation: 61
I have a list view of ticket items with multiple columns. I'm trying to figure out how to implement an onclick listener with what I've written.
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;
}
}
If the user taps on a ticket row in the list I need to get the SQLite id of the ticket to either view it or start the ticket. I know there are different ways to do this, but I'm just trying to get the basics down so I can play with it. Thanks!
UPDATE: I solved this by using code provided by Johan and removing a button I had in the fragment_openticket_row.xml file the adapter was using. It was conflicting with the onClick.
Upvotes: 1
Views: 6781
Reputation: 2292
An easy approach is to create an ItemClickListener in your createView method like so :
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
}
});
Upvotes: 2