title: how to implement an adapter for a listview in my specific case?
body: <p>I have a list of Claim objects, each claim object is initialized with a name and an expense list.</p>
  
  <p>i made an adapter for the listview for the claims.  what I am trying to do now is create an adaptor for the expenses.  Since each claim has its own expense list, i cannot just make a general adapter. i need to make an adapter for a specific Claim, or am  thinking of this wrong?  here is what I did..</p>
  
  <p>JUST TO BE CLEAR&lt; MY QUESTION IS:  How do I make an adaptor for the list initializd with the expenses, and open that list-view to see the expenses of a specific claim when I click on it?</p>
  
  <pre><code>    package app.zioueche_travelexpense;
  
  import java.util.ArrayList;
  import java.util.Collection;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.app.AlertDialog.Builder;
  import android.content.DialogInterface;
  import android.content.DialogInterface.OnClickListener;
  import android.content.Intent;
  import android.os.Bundle;
  import android.text.TextUtils;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.AdapterView.OnItemLongClickListener;
  import android.widget.ArrayAdapter;
  import android.widget.EditText;
  import android.widget.ListView;
  import android.widget.PopupMenu;
  import android.widget.Toast;
  
  //figure out how to add claim in different page.  so we can add date range.
  public class AddClaim extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.add_claim);
  
          //adapter for claim view this one works. it is the expenses one that i do not know how to implement
  
          ListView listView = (ListView) findViewById(R.id.claimListView);
          Collection&lt;Claim&gt; claims = ClaimListController.getClaimList().getClaim();
          final ArrayList&lt;Claim&gt; list = new ArrayList&lt;Claim&gt;(claims);
          final ArrayAdapter&lt;Claim&gt; claimAdapter = new ArrayAdapter&lt;Claim&gt;(this, android.R.layout.simple_list_item_1, list);
          listView.setAdapter(claimAdapter);
  
  
  
          //Added observer pattern
          ClaimListController.getClaimList().addListener(new Listener(){
              @Override
              public void update(){
              list.clear();
              Collection&lt;Claim&gt; claims = ClaimListController.getClaimList().getClaim();
              list.addAll(claims);
              claimAdapter.notifyDataSetChanged();
              }
          });
  
  
          //SINGLE TAP FUNCTION
          listView.setOnItemClickListener(new OnItemClickListener(){
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
  
                  //adapter expenses I DO NOT KNOW HOW TO IMPLEMENT THIS
                  ListView expView = (ListView) findViewById(R.id.ExpenseListView);
                  Collection&lt;Expense&gt; expenses = list.get(position).getExpenses();
                  final ArrayList&lt;Expense&gt; expense = new ArrayList&lt;Expense&gt;(expenses);
                  final ArrayAdapter&lt;Expense&gt; expAdap = new ArrayAdapter&lt;Expense&gt;(AddClaim.this, android.R.layout.simple_list_item_1, expense);
                  expView.setAdapter(expAdap);    //THIS IS MY LINE 64            
                  }
  
          });
  
          //LONG CLICK FUNCTIONS
          listView.setOnItemLongClickListener(new OnItemLongClickListener(){
              @Override
              public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
                  final int finalPosition = position;
                  PopupMenu popup = new PopupMenu(AddClaim.this, view);
                  popup.getMenuInflater().inflate(R.menu.add_claim, popup.getMenu());
                  popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
                      public boolean onMenuItemClick(MenuItem item) {  
                      //DELETE button check.  
                        if (item.getTitle().equals("Delete")){
                          AlertDialog.Builder adb = new AlertDialog.Builder(AddClaim.this);
                          adb.setMessage("Delete "+ list.get(finalPosition).toString()+"?");
                          adb.setCancelable(true);
                          adb.setPositiveButton("Delete",new OnClickListener(){
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  Claim claim = list.get(finalPosition);
                                  ClaimListController.getClaimList().deleteClaim(claim);
                              }
                          });
                          adb.setNegativeButton("Cancel",new OnClickListener(){
  
                              @Override
                              public void onClick(DialogInterface dialog, int which) {                        
                              }
  
                          });
                          adb.show();
                        }//end of delete button check
                        //START of ADD EXPENSE check
                        if (item.getTitle().equals("Add Expense")){                     
                            Intent intent = new Intent(AddClaim.this, ExpenseAdd.class);
                            intent.putExtra("somename", finalPosition); 
                            startActivity(intent);
                        }
                        //end of add expense check
                        return true;  
                       }  
                      });  
                  popup.show();
              return false;
              }
  
          });
  
      }
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.add_claim, menu);
          return true;
      }
  
      protected void onDeleteClick(final int position, final ArrayList&lt;Claim&gt; list){
          AlertDialog.Builder adb = new AlertDialog.Builder(AddClaim.this);
          adb.setMessage("Delete "+ list.get(position).toString()+"?");
          adb.setCancelable(true);
  
          adb.setPositiveButton("Delete",new OnClickListener(){
  
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  Claim claim = list.get(position);
                  ClaimListController.getClaimList().deleteClaim(claim);
              }
  
          });
          adb.setNegativeButton("Cancel",new OnClickListener(){
  
              @Override
              public void onClick(DialogInterface dialog, int which) {                        
              }
  
          });
          adb.show();
      }
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
  
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      public void addClaimView(MenuItem item){
          Toast.makeText(this, "going to claim creation page", Toast.LENGTH_SHORT).show();
          setContentView(R.layout.claim_add_page);
      }
  
      public void addClaims(View v){
          ClaimListController ct = new ClaimListController();
          EditText textView = (EditText) findViewById(R.id.add_claim_field);
          String added = textView.getText().toString();
  
          if (!TextUtils.isEmpty(added)){
              final String t = format("added",added);
              ct.addClaim(new Claim(added));
              Toast.makeText(this, t, Toast.LENGTH_SHORT).show();
              textView.setText("");
              setContentView(R.layout.claim_list);
              //Intent intent = new Intent(MainActivity.this, AddClaim.class);
              //startActivity(intent);
          }else{
              Toast.makeText(AddClaim.this,"Please type something before adding", Toast.LENGTH_SHORT).show();
          }
      }
  
      private String format(String string, String added) {
          String formats = string +" "+ added; 
          return formats;
      }
  }
  </code></pre>
  
  <p>As you can see, i made my listview adapter for the expenses inside the onclick method, since i need the position to get the expense list out of the specific claim.</p>
  
  <p>How can I do this/  when I run this code I get null pointer exceptions on line 64.</p>
  
  <p>here is the log cat.</p>
  
  <pre><code>01-24 02:01:15.434: E/AndroidRuntime(1099): FATAL EXCEPTION: main
  01-24 02:01:15.434: E/AndroidRuntime(1099): java.lang.NullPointerException
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at app.zioueche_travelexpense.AddClaim$2.onItemClick(AddClaim.java:64)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at android.os.Handler.handleCallback(Handler.java:725)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at android.os.Handler.dispatchMessage(Handler.java:92)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at android.os.Looper.loop(Looper.java:137)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at android.app.ActivityThread.main(ActivityThread.java:5041)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at java.lang.reflect.Method.invoke(Method.java:511)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  01-24 02:01:15.434: E/AndroidRuntime(1099):     at dalvik.system.NativeStart.main(Native Method)
  01-24 02:01:17.814: E/Trace(1117): error opening trace file: No such file or directory (2)
  </code></pre>