My requirement is : When I click the list row , it need to go other activity. When I run this code, I got NullPointerException ; (I marked error place in comments)
This is my code:
ArrayList<Object> routeList = getWmRoute();
ArrayList<String> routhPath = new ArrayList<String>();
ArrayList<HashMap<String,String>> alist=new ArrayList<HashMap<String,String>>();
for(int i = 0; i<routeList.size();i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("RetailerCode", ((WMRoute) routeList.get(i)).getDescription());
map.put("RetailerName", ((WMRoute) routeList.get(i)).getBusinessUnit());
alist.add(map);
}
ListView list=getListView();
sd = new SimpleAdapter(this,alist,R.layout.retalier_rows,new String[]{"RetailerCode","RetailerName"},new int[]{R.id.retailerCode,R.id.retailerName});
list.setAdapter(sd);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setSelected(true);
list.setSelection(0);
list.setTextFilterEnabled(true);
list.setItemsCanFocus(true);
list.setTextFilterEnabled(true);
list.setItemChecked(positions,true);
keyword = ((WMRoute) routeList.get(0)).getBusinessUnit(); // here Problem 1 place
//keyword = (String) list.getItemAtPosition(0); // here Problem 1 place
This is my listener part:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position); //java.lang.NullPointerException - problem here 2nd place
keyword = o.toString(); // have to get first column value
positions = position;
if(position != 0 ){
Bundle bundle = new Bundle();
Intent showContent = new Intent(getApplicationContext(),RetailerOptionActivity.class);
int postion = position;
String aString = Integer.toString(postion);
bundle.putString("positon", aString);
startActivity(showContent);
}
}
When I click “OK” from menu option , I am setting parameters;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
Intent showContent = new Intent(getApplicationContext(),RetailerOptionActivity.class);
Bundle bundle = new Bundle();
bundle.putString("RetailerName", keyword);
showContent.putExtras(bundle);
startActivity(showContent);
return true;
case 1:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is my another activity:
Bundle bundle = this.getIntent().getExtras();
String RetailerName = bundle.getString("RetailerName");
setTitle(RetailerName + " - Retailer Option"); // want to get the parameter & set the title name
I have to set the title name , which is picked from previous list option clicked(selected first column name) .
Please help me..
Thanks in advance.
In your onListItemClick() event, instead of using
this.getListAdapter().getItem(position)to get the value of the first column in the item, you should use theView v, given to you as an argument to the event, to get the view that represents the first column. For example, if the first column is a textview, you should do the following: