2014-10-18 213 views
0

我是android新手。我想把我的應用程序的選項卡(該選項卡是片段)之一下的活動之一,當我將我的代碼粘貼到片段,有很多錯誤...android從活動到片段

有新的JSONParse()錯誤。執行();它顯示JSONParse類型不可見。

在這一行私有類JSONParse擴展AsyncTask有錯誤 顯示類JSONParse.only的非法修飾符允許public,abstract和final。

this line pDialog = new ProgressDialog(TabActivityQueue.this);它顯示構造函數progressDialog是未定義的。

所有的變量phonenumber,peoplenumber,remark,status,table2,url都不會被解析爲變量。

我應該改變什麼?我很困難。

這裏的活動代碼:

public class MainActivity extends Activity { 
ListView list; 
TextView number; 
TextView info; 
TextView remark; 
TextView statuss; 
Button Btngetdata; 
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>(); 

//URL to get JSON Array 
private static String url = "http://172.22.85.235:8080/Qproject/servlet/Qaction?action_flag=find"; 

//JSON Node Names 
private static final String Table2 = "table2"; 
private static final String phonenumber = "phonenumber"; 
private static final String peoplenumber = "peoplenumber"; 
private static final String remarks = "remarks"; 
private static final String status = "status"; 

JSONArray table2 = null; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 
    oslist = new ArrayList<HashMap<String, String>>(); 



    Btngetdata = (Button)findViewById(R.id.getdata); 
    Btngetdata.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      new JSONParse().execute(); 


     } 
    }); 


} 



private class JSONParse extends AsyncTask<String, String, JSONObject> { 
    private ProgressDialog pDialog; 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     number = (TextView)findViewById(R.id.number); 
     info = (TextView)findViewById(R.id.info); 
     remark = (TextView)findViewById(R.id.remark); 
     statuss = (TextView)findViewById(R.id.statuss); 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Getting Data ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 



    } 

    @Override 
    protected JSONObject doInBackground(String... args) { 

     JSONParser jParser = new JSONParser(); 

     // Getting JSON from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 
     return json; 
    } 
    @Override 
    protected void onPostExecute(JSONObject json) { 
     pDialog.dismiss(); 
     try { 
       // Getting JSON Array from URL 
       table2 = json.getJSONArray(Table2); 
       for(int i = 0; i < table2.length(); i++){ 
       JSONObject c = table2.getJSONObject(i); 

       // Storing JSON item in a Variable 
       String number = c.getString(phonenumber); 
       String info = c.getString(peoplenumber); 
       String remark = c.getString(remarks); 
       String statuss = c.getString(status); 




       // Adding value HashMap key => value 


       HashMap<String, String> map = new HashMap<String, String>(); 

       map.put(phonenumber, number); 
       map.put(peoplenumber, info); 
       map.put(remarks, remark); 
       map.put(status, statuss); 

       oslist.add(map); 
       list=(ListView)findViewById(R.id.list); 





       ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist, 
         R.layout.list_item, 
         new String[] { phonenumber,peoplenumber, remarks,status }, new int[] { 
           R.id.number,R.id.info, R.id.remark,R.id.statuss}); 

       list.setAdapter(adapter); 
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 
         Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show(); 

        } 
       }); 

       } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 


    } 
} 

這裏就是我貼的活動代碼片段:

public class TabActivityQueue extends Fragment { 

ListView list; 
TextView number; 
TextView info; 
TextView remark; 
TextView statuss; 
Button Btngetdata; 
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>(); 

//URL to get JSON Array 
public static String url = "http://172.22.85.235:8080/Qproject/servlet/Qaction?action_flag=find"; 

//JSON Node Names 
public static final String Table2 = "table2"; 
public static final String phonenumber = "phonenumber"; 
public static final String peoplenumber = "peoplenumber"; 
public static final String remarks = "remarks"; 
public static final String status = "status"; 

JSONArray table2 = null; 



public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
      // Inflate the layout for this fragment 
      //This layout contains your list view 
       View view = inflater.inflate(R.layout.activity_tab_activity_queue, container, false); 

       oslist = new ArrayList<HashMap<String, String>>(); 
       number = (TextView)view.findViewById(R.id.number); 
      info = (TextView)view.findViewById(R.id.info); 
      remark = (TextView)view.findViewById(R.id.remark); 
      statuss = (TextView)view.findViewById(R.id.statuss); 


       Btngetdata = (Button)view.findViewById(R.id.getdata); 
       Btngetdata.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View view) { 
         new JSONParse().execute(); 


        } 
       }); 

      return view; 
     } 
     } 
private class JSONParse extends AsyncTask<String, String, JSONObject> { 
private ProgressDialog pDialog; 
@Override 
    protected void onPreExecute() { 
    super.onPreExecute(); 

    pDialog = new ProgressDialog(TabActivityQueue.this); 
    pDialog.setMessage("Getting Data ..."); 
    pDialog.setIndeterminate(false); 
    pDialog.setCancelable(true); 
    pDialog.show(); 



} 

@Override 
    protected JSONObject doInBackground(String... args) { 

    JSONParser jParser = new JSONParser(); 

    // Getting JSON from URL 
    JSONObject json = jParser.getJSONFromUrl(url); 
    return json; 
} 
@Override 
protected void onPostExecute(JSONObject json) { 
    pDialog.dismiss(); 
    try { 
      // Getting JSON Array from URL 
      table2 = json.getJSONArray(Table2); 
      for(int i = 0; i < table2.length(); i++){ 
      JSONObject c = table2.getJSONObject(i); 

      // Storing JSON item in a Variable 
      String number = c.getString(phonenumber); 
      String info = c.getString(peoplenumber); 
      String remark = c.getString(remarks); 
      String statuss = c.getString(status); 




      // Adding value HashMap key => value 


      HashMap<String, String> map = new HashMap<String, String>(); 

      map.put(phonenumber, number); 
      map.put(peoplenumber, info); 
      map.put(remarks, remark); 
      map.put(status, statuss); 

      oslist.add(map); 
      list=(ListView)findViewById(R.id.list); 





      ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist, 
        R.layout.list_item, 
        new String[] { phonenumber,peoplenumber, remarks,status }, new int[] { 
          R.id.number,R.id.info, R.id.remark,R.id.statuss}); 

      list.setAdapter(adapter); 
      list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
             int position, long id) { 
        Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show(); 

       } 
      }); 

      } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 


} 
} 

回答

0
public class TabActivityQueue extends Fragment { 

    ListView list; 
    TextView number; 
    TextView info; 
    TextView remark; 
    TextView statuss; 
    Button Btngetdata; 
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>(); 

    // URL to get JSON Array 
    public static String url = "http://172.22.85.235:8080/Qproject/servlet/Qaction?action_flag=find"; 

    // JSON Node Names 
    public static final String Table2 = "table2"; 
    public static final String phonenumber = "phonenumber"; 
    public static final String peoplenumber = "peoplenumber"; 
    public static final String remarks = "remarks"; 
    public static final String status = "status"; 

    JSONArray table2 = null; 

    private Activity activity; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     // This layout contains your list view 
     View view = inflater.inflate(R.layout.activity_tab_activity_queue, container, false); 

     oslist = new ArrayList<HashMap<String, String>>(); 
     number = (TextView) view.findViewById(R.id.number); 
     info = (TextView) view.findViewById(R.id.info); 
     remark = (TextView) view.findViewById(R.id.remark); 
     statuss = (TextView) view.findViewById(R.id.statuss); 

     Btngetdata = (Button) view.findViewById(R.id.getdata); 
     Btngetdata.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       new JSONParse().execute(); 

      } 
     }); 

     activity = this.getActivity(); 
     return view; 
    } 

    private class JSONParse extends AsyncTask<String, String, JSONObject> { 
     private ProgressDialog pDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

      pDialog = new ProgressDialog(activity); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 

     } 

     @Override 
     protected JSONObject doInBackground(String... args) { 

      JSONParser jParser = new JSONParser(); 

      // Getting JSON from URL 
      JSONObject json = jParser.getJSONFromUrl(url); 
      return json; 
     } 

     @Override 
     protected void onPostExecute(JSONObject json) { 
      pDialog.dismiss(); 
      try { 
       // Getting JSON Array from URL 
       table2 = json.getJSONArray(Table2); 
       for (int i = 0; i < table2.length(); i++) { 
        JSONObject c = table2.getJSONObject(i); 

        // Storing JSON item in a Variable 
        String number = c.getString(phonenumber); 
        String info = c.getString(peoplenumber); 
        String remark = c.getString(remarks); 
        String statuss = c.getString(status); 

        // Adding value HashMap key => value 

        HashMap<String, String> map = new HashMap<String, String>(); 

        map.put(phonenumber, number); 
        map.put(peoplenumber, info); 
        map.put(remarks, remark); 
        map.put(status, statuss); 

        oslist.add(map); 
        list = (ListView) findViewById(R.id.list); 

        ListAdapter adapter = new SimpleAdapter(activity, oslist, R.layout.list_item, new String[] {phonenumber, peoplenumber, remarks, status}, new int[] {R.id.number, R.id.info, 
          R.id.remark, R.id.statuss}); 

        list.setAdapter(adapter); 
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

         @Override 
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
          Toast.makeText(activity, "You Clicked at " + oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show(); 

         } 
        }); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
    } 

} 
0

徘徊無論你使用上下文TabActivityQueue.this(或getApplicationCOntext( ))更改爲getActivity()。

此外,如果一些android預先定義的方法不起作用嘗試前綴getActivity()像: - getActivity()。method();

0

申報private Activity activity作爲public Activity activity;