2017-05-25 52 views
-1

林在Java/Android的新的, IM試圖使片段活動和實施適配器插入我的片段 'TAB2',但得到eror代碼行(adapter.java):Android的 - 無法找到符號方法getSystemService(字符串)

inflater =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

即時通訊已經在嘗試解決。但仍不能解決這個問題。那麼,我的代碼有什麼問題? 這是我完整的代碼adapter.java:`

package com.yoga1215051164.fragment2.adapter; 
 

 
/** 
 
* Created by Yatogami on 24/05/2017. 
 
*/ 
 

 
import android.content.Context; 
 
import android.view.LayoutInflater; 
 
import android.view.View; 
 
import android.view.ViewGroup; 
 
import android.widget.BaseAdapter; 
 
import android.widget.TextView; 
 
import com.yoga1215051164.fragment2.R; 
 
import com.yoga1215051164.fragment2.data.Data; 
 
import com.yoga1215051164.fragment2.tab2; 
 
import java.util.List; 
 

 

 
public class Adapter extends BaseAdapter { 
 
    private tab2 activity; 
 
    private LayoutInflater inflater; 
 
    private List<Data> items; 
 

 
    public Adapter(tab2 activity, List<Data> items) { 
 
     this.activity = activity; 
 
     this.items = items; 
 
    } 
 

 
    @Override 
 
    public int getCount() { 
 
     return items.size(); 
 
    } 
 

 
    @Override 
 
    public Object getItem(int location) { 
 
     return items.get(location); 
 
    } 
 

 
    @Override 
 
    public long getItemId(int position) { 
 
     return position; 
 
    } 
 

 
    @Override 
 
    public View getView(int position, View convertView, ViewGroup parent) { 
 

 
     if (inflater == null) 
 
      inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 

 
     if (convertView == null) 
 
      convertView = inflater.inflate(R.layout.list_row, null); 
 

 
     TextView id = (TextView) convertView.findViewById(R.id.id); 
 
     TextView nama = (TextView) convertView.findViewById(R.id.nama); 
 
     TextView alamat = (TextView) convertView.findViewById(R.id.alamat); 
 

 
     Data data = items.get(position); 
 

 
     id.setText(data.getId()); 
 
     nama.setText(data.getNama()); 
 
     alamat.setText(data.getAlamat()); 
 

 
     return convertView; 
 
    } 
 

 
}

和下面的代碼段tab2.java:

package com.yoga1215051164.fragment2; 
 

 
/** 
 
* Created by Yatogami on 25/05/2017. 
 
*/ 
 

 

 
import android.app.AlertDialog; 
 
import android.content.DialogInterface; 
 
import android.os.Bundle; 
 
import android.support.annotation.Nullable; 
 
import android.support.design.widget.FloatingActionButton; 
 
import android.support.v4.app.Fragment; 
 
import android.support.v4.widget.SwipeRefreshLayout; 
 
import android.util.Log; 
 
import android.view.LayoutInflater; 
 
import android.view.View; 
 
import android.view.ViewGroup; 
 
import android.widget.AdapterView; 
 
import android.widget.EditText; 
 
import android.widget.ListView; 
 
import android.widget.Toast; 
 
import org.json.JSONArray; 
 
import org.json.JSONException; 
 
import org.json.JSONObject; 
 
import com.yoga1215051164.fragment2.adapter.Adapter; 
 
import com.yoga1215051164.fragment2.app.AppController; 
 
import com.yoga1215051164.fragment2.data.Data; 
 
import com.yoga1215051164.fragment2.util.Server; 
 
import com.android.volley.Request; 
 
import com.android.volley.Response; 
 
import com.android.volley.VolleyError; 
 
import com.android.volley.VolleyLog; 
 
import com.android.volley.toolbox.JsonArrayRequest; 
 
import com.android.volley.toolbox.StringRequest; 
 
import java.util.ArrayList; 
 
import java.util.HashMap; 
 
import java.util.List; 
 
import java.util.Map; 
 

 

 
public class tab2 extends Fragment implements SwipeRefreshLayout.OnRefreshListener{ 
 

 
    FloatingActionButton fab; 
 
    ListView list; 
 
    SwipeRefreshLayout swipe; 
 
    List<Data> itemList = new ArrayList<Data>(); 
 
    Adapter adapter; 
 
    int success; 
 
    AlertDialog.Builder dialog; 
 
    LayoutInflater inflater; 
 
    View dialogView; 
 
    EditText txt_id, txt_nama, txt_alamat; 
 
    String id, nama, alamat; 
 

 
    private static final String TAG = tab2.class.getSimpleName(); 
 

 
    private static String url_select \t = Server.URL + "select.php"; 
 
    private static String url_insert \t = Server.URL + "insert.php"; 
 
    private static String url_edit \t  = Server.URL + "edit.php"; 
 
    private static String url_update \t = Server.URL + "update.php"; 
 
    private static String url_delete \t = Server.URL + "delete.php"; 
 

 
    public static final String TAG_ID  = "id"; 
 
    public static final String TAG_NAMA  = "nama"; 
 
    public static final String TAG_ALAMAT = "alamat"; 
 
    private static final String TAG_SUCCESS = "success"; 
 
    private static final String TAG_MESSAGE = "message"; 
 
    String tag_json_obj = "json_obj_req"; 
 

 
    @Override 
 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
 
     View v =inflater.inflate(R.layout.tabmenu2,container,false); 
 
      super.onCreate(savedInstanceState); 
 

 
      fab  = (FloatingActionButton) v.findViewById(R.id.fab_add); 
 
      swipe = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_layout); 
 
      list = (ListView) v.findViewById(R.id.list); 
 

 
      adapter = new Adapter(tab2.this, itemList); 
 
      list.setAdapter(adapter); 
 

 
      swipe.setOnRefreshListener(this); 
 

 
      swipe.post(new Runnable() { 
 
          @Override 
 
          public void run() { 
 
           swipe.setRefreshing(true); 
 
           itemList.clear(); 
 
           adapter.notifyDataSetChanged(); 
 
           callVolley(); 
 
          } 
 
         } 
 
      ); 
 

 
      fab.setOnClickListener(new View.OnClickListener() { 
 
       @Override 
 
       public void onClick(View view) { 
 
        DialogForm("", "", "", "SIMPAN"); 
 
       } 
 
      }); 
 

 
      list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
 

 
       @Override 
 
       public boolean onItemLongClick(final AdapterView<?> parent, View view, 
 
               final int position, long id) { 
 
        // TODO Auto-generated method stub 
 
        final String idx = itemList.get(position).getId(); 
 

 
        final CharSequence[] dialogitem = {"Edit", "Delete"}; 
 
        dialog = new AlertDialog.Builder(tab2.this); 
 
        dialog.setCancelable(true); 
 
        dialog.setItems(dialogitem, new DialogInterface.OnClickListener() { 
 

 
         @Override 
 
         public void onClick(DialogInterface dialog, int which) { 
 
          // TODO Auto-generated method stub 
 
          switch (which) { 
 
           case 0: 
 
            edit(idx); 
 
            break; 
 
           case 1: 
 
            delete(idx); 
 
            break; 
 
          } 
 
         } 
 
        }).show(); 
 
        return false; 
 
       } 
 
      }); 
 
     return v; 
 
     } 
 

 
     @Override 
 
     public void onRefresh() { 
 
      itemList.clear(); 
 
      adapter.notifyDataSetChanged(); 
 
      callVolley(); 
 
     } 
 

 
    private void kosong(){ 
 
     txt_id.setText(null); 
 
     txt_nama.setText(null); 
 
     txt_alamat.setText(null); 
 
    } 
 

 

 
    private void DialogForm(String idx, String namax, String alamatx, String button) { 
 
     dialog = new AlertDialog.Builder(tab2.this); 
 
     inflater = getLayoutInflater(); 
 
     dialogView = inflater.inflate(R.layout.form_biodata, null); 
 
     dialog.setView(dialogView); 
 
     dialog.setCancelable(true); 
 
     dialog.setIcon(R.mipmap.ic_launcher); 
 
     dialog.setTitle("Form Biodata"); 
 

 
     txt_id  = (EditText) dialogView.findViewById(R.id.txt_id); 
 
     txt_nama = (EditText) dialogView.findViewById(R.id.txt_nama); 
 
     txt_alamat = (EditText) dialogView.findViewById(R.id.txt_alamat); 
 

 
     if (!idx.isEmpty()){ 
 
      txt_id.setText(idx); 
 
      txt_nama.setText(namax); 
 
      txt_alamat.setText(alamatx); 
 
     } else { 
 
      kosong(); 
 
     } 
 

 
     dialog.setPositiveButton(button, new DialogInterface.OnClickListener() { 
 

 
      @Override 
 
      public void onClick(DialogInterface dialog, int which) { 
 
       id  = txt_id.getText().toString(); 
 
       nama = txt_nama.getText().toString(); 
 
       alamat = txt_alamat.getText().toString(); 
 

 
       simpan_update(); 
 
       dialog.dismiss(); 
 
      } 
 
     }); 
 

 
     dialog.setNegativeButton("BATAL", new DialogInterface.OnClickListener() { 
 

 
      @Override 
 
      public void onClick(DialogInterface dialog, int which) { 
 
       dialog.dismiss(); 
 
       kosong(); 
 
      } 
 
     }); 
 

 
     dialog.show(); 
 
    } 
 
    
 
    private void callVolley(){ 
 
     itemList.clear(); 
 
     adapter.notifyDataSetChanged(); 
 
     swipe.setRefreshing(true); 
 
     
 
     JsonArrayRequest jArr = new JsonArrayRequest(url_select, new Response.Listener<JSONArray>() { 
 
      @Override 
 
      public void onResponse(JSONArray response) { 
 
       Log.d(TAG, response.toString()); 
 
       
 
       for (int i = 0; i < response.length(); i++) { 
 
        try { 
 
         JSONObject obj = response.getJSONObject(i); 
 

 
         Data item = new Data(); 
 

 
         item.setId(obj.getString(TAG_ID)); 
 
         item.setNama(obj.getString(TAG_NAMA)); 
 
         item.setAlamat(obj.getString(TAG_ALAMAT)); 
 
         
 
         itemList.add(item); 
 
        } catch (JSONException e) { 
 
         e.printStackTrace(); 
 
        } 
 
       } 
 
       
 
       adapter.notifyDataSetChanged(); 
 

 
       swipe.setRefreshing(false); 
 
      } 
 
     }, new Response.ErrorListener() { 
 

 
      @Override 
 
      public void onErrorResponse(VolleyError error) { 
 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
 
       swipe.setRefreshing(false); 
 
      } 
 
     }); 
 

 
    
 
     AppController.getInstance().addToRequestQueue(jArr); 
 
    } 
 

 
    
 
    private void simpan_update() { 
 
     String url; 
 
     if (id.isEmpty()){ 
 
      url = url_insert; 
 
     } else { 
 
      url = url_update; 
 
     } 
 

 
     StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { 
 

 
      @Override 
 
      public void onResponse(String response) { 
 
       Log.d(TAG, "Response: " + response.toString()); 
 

 
       try { 
 
        JSONObject jObj = new JSONObject(response); 
 
        success = jObj.getInt(TAG_SUCCESS); 
 

 
        
 
        if (success == 1) { 
 
         Log.d("Add/update", jObj.toString()); 
 

 
         callVolley(); 
 
         kosong(); 
 

 
         Toast.makeText(tab2.this, jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show(); 
 
         adapter.notifyDataSetChanged(); 
 

 
        } else { 
 
         Toast.makeText(tab2.this, jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show(); 
 
        } 
 
       } catch (JSONException e) { 
 
        e.printStackTrace(); 
 
       } 
 

 
      } 
 
     }, new Response.ErrorListener() { 
 

 
      @Override 
 
      public void onErrorResponse(VolleyError error) { 
 
       Log.e(TAG, "Error: " + error.getMessage()); 
 
       Toast.makeText(tab2.this, error.getMessage(), Toast.LENGTH_LONG).show(); 
 
      } 
 
     }) { 
 

 
      @Override 
 
      protected Map<String, String> getParams() { 
 
      
 
       Map<String, String> params = new HashMap<String, String>(); 
 
       if (id.isEmpty()){ 
 
        params.put("nama", nama); 
 
        params.put("alamat", alamat); 
 
       } else { 
 
        params.put("id", id); 
 
        params.put("nama", nama); 
 
        params.put("alamat", alamat); 
 
       } 
 

 
       return params; 
 
      } 
 

 
     }; 
 

 
     AppController.getInstance().addToRequestQueue(strReq, tag_json_obj); 
 
    } 
 
    
 
    private void edit(final String idx){ 
 
     StringRequest strReq = new StringRequest(Request.Method.POST, url_edit, new Response.Listener<String>() { 
 

 
      @Override 
 
      public void onResponse(String response) { 
 
       Log.d(TAG, "Response: " + response.toString()); 
 

 
       try { 
 
        JSONObject jObj = new JSONObject(response); 
 
        success = jObj.getInt(TAG_SUCCESS); 
 
        
 
        if (success == 1) { 
 
         Log.d("get edit data", jObj.toString()); 
 
         String idx  = jObj.getString(TAG_ID); 
 
         String namax = jObj.getString(TAG_NAMA); 
 
         String alamatx = jObj.getString(TAG_ALAMAT); 
 

 
         DialogForm(idx, namax, alamatx, "UPDATE"); 
 

 
         adapter.notifyDataSetChanged(); 
 

 
        } else { 
 
         Toast.makeText(tab2.this, jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show(); 
 
        } 
 
       } catch (JSONException e) { 
 
        
 
        e.printStackTrace(); 
 
       } 
 

 
      } 
 
     }, new Response.ErrorListener() { 
 

 
      @Override 
 
      public void onErrorResponse(VolleyError error) { 
 
       Log.e(TAG, "Error: " + error.getMessage()); 
 
       Toast.makeText(tab2.this, error.getMessage(), Toast.LENGTH_LONG).show(); 
 
      } 
 
     }) { 
 

 
      @Override 
 
      protected Map<String, String> getParams() { 
 
       
 
       Map<String, String> params = new HashMap<String, String>(); 
 
       params.put("id", idx); 
 

 
       return params; 
 
      } 
 

 
     }; 
 

 
     AppController.getInstance().addToRequestQueue(strReq, tag_json_obj); 
 
    } 
 
    
 
    private void delete(final String idx){ 
 
     StringRequest strReq = new StringRequest(Request.Method.POST, url_delete, new Response.Listener<String>() { 
 

 
      @Override 
 
      public void onResponse(String response) { 
 
       Log.d(TAG, "Response: " + response.toString()); 
 

 
       try { 
 
        JSONObject jObj = new JSONObject(response); 
 
        success = jObj.getInt(TAG_SUCCESS); 
 

 
       
 
        if (success == 1) { 
 
         Log.d("delete", jObj.toString()); 
 

 
         callVolley(); 
 

 
         Toast.makeText(tab2.this, jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show(); 
 

 
         adapter.notifyDataSetChanged(); 
 

 
        } else { 
 
         Toast.makeText(tab2.this, jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show(); 
 
        } 
 
       } catch (JSONException e) { 
 
        
 
        e.printStackTrace(); 
 
       } 
 

 
      } 
 
     }, new Response.ErrorListener() { 
 

 
      @Override 
 
      public void onErrorResponse(VolleyError error) { 
 
       Log.e(TAG, "Error: " + error.getMessage()); 
 
       Toast.makeText(tab2.this, error.getMessage(), Toast.LENGTH_LONG).show(); 
 
      } 
 
     }) { 
 

 
      @Override 
 
      protected Map<String, String> getParams() { 
 
       
 
       Map<String, String> params = new HashMap<String, String>(); 
 
       params.put("id", idx); 
 

 
       return params; 
 
      } 
 

 
     }; 
 

 
     AppController.getInstance().addToRequestQueue(strReq, tag_json_obj); 
 
    } 
 

 
}

三江源

+0

爲什麼不使用ArrayAdapter? –

回答

3

你必須上下文傳遞給你的適配器,這樣你的適配器應該是這樣的

private Context context; 
public Adapter(Context context, List<Data> items) { 
     this.context=context 
     this.items = items; 
    } 

,並使用此背景下這樣

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

,並從你的片段像這樣

發送上下文
Adapter adapter=new Adapter(getActivity(),listItems); 
+0

其工作,謝謝先生.. –

0

Fragment不延伸Context這就是爲什麼你不能撥打getSystemService。不要傳遞片段實例,而要傳遞片段主機活動的上下文。由於您確定上下文有效,建議在Fragment.onAttach()中實例化適配器。還可以使用

LayoutInflater.from(context); 

如果您想獲得充氣器的實例。