Select to view content in your preferred language

Identify Task - NullPointerException

455
1
04-27-2012 02:01 PM
RichardJarvis
Emerging Contributor
Does anybody have any luck whatsoever with the IdentifyResultSpinnerAdapter?  Specifically with actually doing something with the results returned, like displaying the details of a selected feature in the spinner in a new activity.  The sample is ok but stops short of doing anything substantive with the Identify Results.  When trying to use an OnItemSelectedListener, I constantly get a NullPointerException thrown.  Any ideas would be helpful.  My code looks just like the Identify Result sample code except it adds the OnItemSelectedListener method to the IdentifyResultSpinnerAdapter class and calls that method from the createIdentifyContent ViewGroup.  Again, any guidance is appreciated.
0 Kudos
1 Reply
RichardJarvis
Emerging Contributor
Does anybody have any luck whatsoever with the IdentifyResultSpinnerAdapter?  Specifically with actually doing something with the results returned, like displaying the details of a selected feature in the spinner in a new activity.  The sample is ok but stops short of doing anything substantive with the Identify Results.  When trying to use an OnItemSelectedListener, I constantly get a NullPointerException thrown.  Any ideas would be helpful.  My code looks just like the Identify Result sample code except it adds the OnItemSelectedListener method to the IdentifyResultSpinnerAdapter class and calls that method from the createIdentifyContent ViewGroup.  Again, any guidance is appreciated.



private ViewGroup createIdentifyContent(final List<IdentifyResult> results){

        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        layout.setOrientation(LinearLayout.HORIZONTAL);
       
        IdentifyResultSpinner spinner = new IdentifyResultSpinner(this, (List<IdentifyResult>) results);
        spinner.setClickable(true);
        MyIdentifyAdapter adapter = new MyIdentifyAdapter(this, results);

//        adapter.OnItemClickListener(spinner.getRootView(), spinner.getSelectedView(), spinner.getSelectedItemPosition(), spinner.getSelectedItemId());
        adapter.OnItemSelectedListener(spinner, spinner.getSelectedView(), spinner.getSelectedItemPosition(), spinner.getSelectedItemId());

        spinner.setAdapter(adapter);
        spinner.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));         
        layout.addView(spinner);
       
       return layout;
}

public class MyIdentifyAdapter extends IdentifyResultSpinnerAdapter{
  String m_show = null;
  List<IdentifyResult> resultList;
  int currentDataViewed = -1;
  Context m_context;

  public MyIdentifyAdapter(Context context, List<IdentifyResult> results) {
   super(context,results);
   this.resultList = results;
   this.m_context = context;
  }
 
  public void OnItemSelectedListener(View parent, View view, int position, long id){
   IdentifyResult curResult = this.resultList.get(position);
   Intent details = new Intent(AndroidDemoActivity.this, detailsActivity.getClass());

   startActivity(details);
   detailsActivity.result = curResult;
  }
 
//  public void OnItemClickListener(View parent, View view, int position, long id){
//   IdentifyResult curResult = this.resultList.get(position);
//   Intent details = new Intent(AndroidDemoActivity.this, detailsActivity.getClass());
//
//   startActivity(details);
//   detailsActivity.result = curResult;
//  }
 
  //This is the view that will get added to the callout
  //Create a text view and assign the text that should be visible in the callout 
  public View getView(int position, View convertView, ViewGroup parent) {
   String outputVal = null;
   TextView txtView;
   IdentifyResult curResult = this.resultList.get(position);

   if(curResult.getAttributes().containsKey("Bldg_Name")){  
    outputVal = curResult.getAttributes().get("Bldg_Name").toString();
   }
  
   if(curResult.getAttributes().containsKey("Site_name")){  
    outputVal = curResult.getAttributes().get("Site_name").toString();
   }
  
   txtView = new TextView(this.m_context);
   txtView.setText(outputVal);
   txtView.setBackgroundColor(Color.WHITE);
   txtView.setTextColor(Color.BLACK);
   txtView.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
   txtView.setGravity(Gravity.CENTER_VERTICAL);
    
   return txtView;
  }
}

private class MyIdentifyTask extends AsyncTask<IdentifyParameters, Void, IdentifyResult[]> {

  IdentifyTask mIdentifyTask;
  Point mAnchor;
  MyIdentifyTask(Point anchorPoint) {
   mAnchor = anchorPoint;
  }
 
  @Override
  protected IdentifyResult[] doInBackground(IdentifyParameters... params) {
   IdentifyResult[] mResult = null;
   if (params != null && params.length > 0) {
    IdentifyParameters mParams = params[0];
    try {
     mResult = mIdentifyTask.execute(mParams);
    } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   
   }
   return mResult;
  }
 
  @Override
  protected void onPostExecute(IdentifyResult[] results) {
   // TODO Auto-generated method stub
  
   ArrayList<IdentifyResult> resultList = new ArrayList<IdentifyResult>();
   for (int index=0; index < results.length; index++){
   
    if(results[index].getAttributes().get(results[index].getDisplayFieldName())!=null)
     resultList.add(results[index]);
   }
  
   //map.getCallout().show(map.toMapPoint(x,y), createIdentifyContent(resultList));
   map.getCallout().show(mAnchor, createIdentifyContent(resultList));
  
  }

  @Override
  protected void onPreExecute() {
  
   mIdentifyTask = new IdentifyTask("<<MyServiceURL>>");
  }

}
0 Kudos