Salesforce – How to create onItemClickListener for List Adapter

As a beginner in Salesforce Mobile App development, I am learning new stuff everyday. Recently I had a situation where I wanted to attach onItemClickListener to items in my list that were fetched dynamically from Salesforce.
I tried to look for a solution on Google, but 5 pages in I still couldn’t find the solution.

Finally, I found what I was looking for on a blog.

But it still wasn’t exactly what I wanted. Since I didn’t have much knowledge about ListView or List Adapters at that time, it took me a little while to make it fit to what I was looking for. So, I decided to write this blog to help out anyone who is in the same situation as I was.
I assume you have a little knowledge about Salesforce REST API and little Android development knowledge.

Here’s a code snippet:
private RestClient client;
private ArrayAdapter listAdapter;
private ListView timeList;

// onCreate method

public void onResume() {

// Create list adapter
listAdapter = new ArrayAdapter

(this, android.R.layout.simple_list_item_1, new ArrayList


timeList = (ListView) findViewById(;


public void onResume(RestClient client) {
// Keeping reference to rest client
this.client = client;
// Show everything
try {
sendRequest(“SELECT Name, Date__c, FROM MyObject__c WHERE User__c=‘00111222a333444’ ORDER BY Date__c ASC”);
} catch (UnsupportedEncodingException e) {


private void sendRequest(String soql) throws UnsupportedEncodingException {
RestRequest restRequest = RestRequest.getRequestForQuery(getString(R.string.api_version), soql);

client.sendAsync(restRequest, new AsyncRequestCallback() {
public void onSuccess(RestRequest request, RestResponse result) {
try {
JSONArray records = result.asJSONObject().getJSONArray(“records”);
for (int i = 0; i

< records.length(); i++) {
String oldDate = records.getJSONObject(i).getString(

String[] formatDate = oldDate.split(”-”);
String formatMonth = null;

if(formatDate[1].contains(“01”)) formatMonth = “January”;
else if(formatDate[1].contains(“02”)) formatMonth = “February”;
else if(formatDate[1].contains(“03”)) formatMonth = “March”;
else if(formatDate[1].contains(“04”)) formatMonth = “April”;
else if(formatDate[1].contains(“05”)) formatMonth = “May”;
else if(formatDate[1].contains(“06”)) formatMonth = “June”;
else if(formatDate[1].contains(“07”)) formatMonth = “July”;
else if(formatDate[1].contains(“08”)) formatMonth = “August”;
else if(formatDate[1].contains(“09”)) formatMonth = “September”;
else if(formatDate[1].contains(“10”)) formatMonth = “October”;
else if(formatDate[1].contains(“11”)) formatMonth = “November”;
else if(formatDate[1].contains(“12”)) formatMonth = “December”;
else formatMonth = “None”;

String formattedDate = formatMonth + ” ” + formatDate[2] + “, ” + formatDate[0];

timeList.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0,
View v, int position, long id) {
AlertDialog.Builder adb = new AlertDialog.Builder(
adb.setTitle(“ListView OnClick”);
adb.setMessage(“Selected Item is = “
+ timeList.getItemAtPosition(position));
adb.setPositiveButton(“Ok”, null);;


} catch (Exception e) {

public void onError(Exception exception) {
MainActivity.this.getString(ForceApp.APP.getSalesforceR().stringGenericError(), exception.toString()),

Source: Android Helper Blog

This entry was posted in Technology and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>