0

我有一个自定义列表视图适配器类,listview正在对话框中加载。我想要做的是单击一个按钮并删除该行,但是我nullpointerexceptioncustomlistview适配器上得到了一个,这是customlistview适配器类的代码:

@SuppressWarnings("rawtypes")
public class CustomListViewVoidAdapter extends ArrayAdapter{
    String url_delete=  "http://192.168.1.70:80/delete_from_orders.php";
    String food_name, food_add, food_remove,tab_num;
    LayoutInflater inflater;
    Context context;
List<ListViewItemVoid> items;
private static final String TAG_SUCCESS = "success";
ListViewItemVoid holder = null;
CustomListViewVoidAdapter adapter;
ListViewItemVoid item;
ListViewItemVoid item_selected;
ListView lv;
JSONParser jsonParser = new JSONParser();


public CustomListViewVoidAdapter(Context context, int textViewResourceId, List<ListViewItemVoid> items) {
super(context, textViewResourceId);
Log.i("","const");
        this.items = items;
this.context = context;
inflater = (LayoutInflater)   context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.i("CLVOA","Constructor");
}

@Override  
    public int getCount() {  
        Log.i("CLVOA","getCount()");
        return items.size();  
        }  

    @Override  
    public ListViewItemVoid getItem(int position) {
        Log.i("Item","getItem");
        return ((CustomListViewVoidAdapter) items).getItem(position);
        }  

    @Override  
    public long getItemId(int position) {  
        Log.i("CLVOA","getItemId");
        return ((CustomListViewVoidAdapter) items).getItemId(position);
        }


    @Override  
    public View getView(final int position, final View convertView, ViewGroup parent) {
         item = items.get(position);
        View vi = convertView;


        Log.i("akhada","adapter"+adapter);
        if(vi==null)
            {
            item_selected= items.get(position);
            vi= inflater.inflate(R.layout.void_food, null);
            holder = new ListViewItemVoid();
            holder.table_number=(TextView)vi.findViewById(R.id.tablenumber);
            holder.food_ordered=(TextView)vi.findViewById(R.id.ordered);
            holder.food_added=(TextView)vi.findViewById(R.id.foodadded);
            holder.food_removed=(TextView)vi.findViewById(R.id.foodremoved);
            holder.void_button=(Button)vi.findViewById(R.id.voidbutton);
            lv= (ListView) vi.findViewById(R.id.Mytemp);
            holder.void_button.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
    adapter = new  CustomListViewVoidAdapter(context,R.layout.mytemp, items);
                if (adapter!=null)
                {
                Log.i("","ata3 hon");
                item_selected= items.get(position);
                items.remove(item_selected);
                Log.i("wosil ", " hon");
                lv.setAdapter(adapter);
                new DeleteOrder().execute();
                }
                Log.i("","selected item position"+item_selected);
               }});
            vi.setTag(holder);
            }
    else{
        holder = (ListViewItemVoid) vi.getTag();
    }

        holder.food_ordered.setText(""+item.food_name);
        holder.food_added.setText(""+item.add);
        holder.food_removed.setText(""+item.remove);
        holder.table_number.setText(""+item.table_num);
        food_name= holder.food_ordered.getText().toString();
        food_add=  holder.food_added.getText().toString();
        food_remove=  holder.food_removed.getText().toString();
        tab_num= holder.table_number.getText().toString();
        Log.i ("food name deleting ", " deleting food name: " + food_name);
        return vi;  
    }
  class DeleteOrder extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        /**
         * Deleting product
         * */
        @Override
        protected String doInBackground(String... args) {

            // Check for success tag
            int success;
            try {
                // Building Parameters
                //String food_name, food_add, food_remove,tab_num;
                Log.i ("woslit l","wosil l food "+food_name);
                Log.i ("woslit l","wosil l add "+food_add);
                Log.i ("woslit l","wosil l remove "+food_remove);
                Log.i ("woslit l","wosil l table number "+tab_num);
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("table_id", tab_num));
                params.add(new BasicNameValuePair("add", food_add));
                params.add(new BasicNameValuePair("remove", food_remove));
                params.add(new BasicNameValuePair("food", food_name));
                Log.i("","honee");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(
                        url_delete, "POST", params);


                // check your log for json response
                Log.d("Delete Product", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.i("del", "deleted");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }


        //}

     }}

在我的主要活动中:

 static class ListViewItemVoid{
    public TextView table_number;
    public TextView food_added;
    public TextView food_removed;
    public TextView food_ordered;
    public String food_name;
    public String add;
    public String remove;
    public String table_num;
    public Button void_button;
}

mytemp 是 listview xml 文件, items 是列表:

   List<ListViewItemVoid> items;

我知道上下文有问题,但我不知道该怎么办......

我想将主要活动的上下文传递到 customlistview 活动类中。

来自 logcat 的错误:

        08-31 13:16:45.140: E/AndroidRuntime(1736): FATAL EXCEPTION: main

       08-31 13:16:45.140: E/AndroidRuntime(1736): java.lang.NullPointerException

         08-31 13:16:45.140: E/AndroidRuntime(1736):    at      
                  com.primeco.IOrderRes.CustomListViewVoidAdapter$1.onClick(CustomListViewVoidAdapter.java:94           )
       08-31 13:16:45.140: E/AndroidRuntime(1736):  at  
          android.view.View.performClick(View.java:4202)
       08-31 13:16:45.140: E/AndroidRuntime(1736):  at 

       android.view.View$PerformClick.run(View.java:17340)
       08-31 13:16:45.140: E/AndroidRuntime(1736):  at        

       android.os.Handler.handleCallback(Handler.java:725)
      08-31 13:16:45.140: E/AndroidRuntime(1736):   at   

       android.os.Handler.dispatchMessage(Handler.java:92)
        08-31 13:16:45.140: E/AndroidRuntime(1736):     at 

        android.os.Looper.loop(Looper.java:137)
      08-31 13:16:45.140: E/AndroidRuntime(1736):   at 

       android.app.ActivityThread.main(ActivityThread.java:5039)

      08-31 13:16:45.140: E/AndroidRuntime(1736):   at 

       java.lang.reflect.Method.invokeNative(Native Method)

      08-31 13:16:45.140: E/AndroidRuntime(1736):   at 
     java.lang.reflect.Method.invoke(Method.java:511)

      08-31 13:16:45.140: E/AndroidRuntime(1736):   at 
      com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

     08-31 13:16:45.140: E/AndroidRuntime(1736):    at 
     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

        08-31 13:16:45.140: E/AndroidRuntime(1736): 
at  dalvik.system.NativeStart.main(Native Method)
4

0 回答 0