如何在我的特定情况下为列表视图实现适配器?

时间:2015-01-24 02:10:32

标签: java android listview android-listview

我有一个Claim对象列表,每个声明对象都使用名称和费用列表进行初始化。

我为索赔的列表视图制作了一个适配器。我现在要做的是为费用创建一个适配器。由于每个声明都有自己的费用列表,我不能只是制作一般的适配器。我需要为特定的索赔制作适配器,或者我想到这个错误?这就是我做的..

必须清除<我的问题是:如何使用费用为初始化列表创建一个适配器,并打开该列表视图以查看特定声明的费用,当我点击它时?

    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<Claim> claims = ClaimListController.getClaimList().getClaim();
        final ArrayList<Claim> list = new ArrayList<Claim>(claims);
        final ArrayAdapter<Claim> claimAdapter = new ArrayAdapter<Claim>(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<Claim> claims = ClaimListController.getClaimList().getClaim();
            list.addAll(claims);
            claimAdapter.notifyDataSetChanged();
            }
        });


        //SINGLE TAP FUNCTION
        listView.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> 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<Expense> expenses = list.get(position).getExpenses();
                final ArrayList<Expense> expense = new ArrayList<Expense>(expenses);
                final ArrayAdapter<Expense> expAdap = new ArrayAdapter<Expense>(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<?> 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<Claim> 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;
    }
}

正如你所看到的,我为onclick方法中的费用制作了listview适配器,因为我需要这个位置来从特定索赔中获取费用清单。

如何执行此操作/当我运行此代码时,我在第64行获得空指针异常。

这是log cat。

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)

0 个答案:

没有答案