I have an activity to populate a listview. I have created the listview inside the onCreate method of the activity. Now I want to implement an onclick listner for the list. How to achieve this?
This is my activity class with the listview:
public class PollStationActivity extends Activity {
static String response_str=null;
static String response_code=null;
String ac_code;
String ac_id;
String ac_lat;
String ac_lon;
String ac_name;
// Hashmap for ListView
ArrayList<HashMap<String, String>> PSList = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// no more this
setContentView(R.layout.activity_poll_station);
//sendPostRequest();
//Log.i("I am here","1");
final String status_code_from_prev= getIntent().getExtras().getString("status");
//Log.i("ss",status_code_from_prev);
ArrayList<PSItem> PSList = new ArrayList<PSItem>();
DatabaseHelper db = new DatabaseHelper(PollStationActivity.this);
ContentValues values = new ContentValues();
try {
db.createDataBase();
PSList = db.select(status_code_from_prev);
//Log.i("Count : ", " " + PSList.isEmpty());
} catch (IOException e) {
e.printStackTrace();
}
db.close();
String[] fromColumns = {"name"};
int[] toViews = {R.id.list_label_name}; // The TextView in simple_list_item_1
ListView listView1 = (ListView) findViewById(R.id.poll_list_listView);
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
Iterator<PSItem> i = PSList.iterator();
while(i.hasNext())
{
HashMap<String, String> map = new HashMap<String, String>();
PSItem objPSItem = i.next();
map.put("name", objPSItem.PS_NAME);
map.put("ps_Id", objPSItem.PS_ID);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_layout, fromColumns, toViews);
listView1.setAdapter(adapter);
}
}
Where should I put the listview onclick listner?
You can set the listener right after you’ve initialized, for instance:
Even though you have not set the adapter for the list yet, meaning there are no items displayed, it doesn’t matter where you set it, as long as you have already initialized the listview via
findViewById