-1

我從服務器檢索了一些數據並將這些數據填充到列表視圖中。我想使用我收到的部分數據並將其發送回服務器。我該怎麼做?當我點擊其中一個列表項時,它將啓動另一個帶按鈕的活動,當我點擊其中一個按鈕時,應用程序將使用該列表項中的部分數據(之前從服務器中檢索到)並將其發送回服務器。我是新來的android編程,我卡住了,不知道如何做到這一點。有人能告訴我嗎?我知道如何通過手動鍵入要發送的數據將數據發送到服務器。android在列表視圖中向服務器發送數據

這是我如何從服務器獲取數據並填充到一個列表視圖,我想發送特定PHONENUMBER收到回服務器當我點擊列表視圖項:

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 
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; 



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); 


      return view; 
     } 


@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
// TODO Auto-generated method stub 
super.onActivityCreated(savedInstanceState); 

oslist = new ArrayList<HashMap<String, String>>(); 
Btngetdata = (Button)getView().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> { 
    public ProgressDialog pDialog; 
    @Override 
    protected void onPreExecute() { 
    super.onPreExecute(); 
    number = (TextView)getView().findViewById(R.id.number); 
    info = (TextView)getView().findViewById(R.id.info); 
    remark = (TextView)getView().findViewById(R.id.remark); 
    statuss = (TextView)getView().findViewById(R.id.statuss); 
    pDialog = new ProgressDialog(getActivity()); 
    pDialog.setMessage("Getting Data ..."); 
    pDialog.setIndeterminate(false); 
    pDialog.setCancelable(true); 
    pDialog.show(); 



} 

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

    JSONParser jParser = new JSONParser(); 

    // Getting JSON from URL 
    JSONObject json = jParser.getJSONFromUrl(url); 
    return json; 
} 
@Override 
public 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)getView().findViewById(R.id.list); 





      ListAdapter adapter = new SimpleAdapter(getActivity(), oslist, 
        R.layout.list_view, 
        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(getActivity(), "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show(); 
        Intent ThreeButton = new Intent(getActivity(), ThreeButton.class); 
        startActivity(ThreeButton); 
       } 
       } 
      ); 

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


} 
} 
} 

回答

0
  list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 

       Intent ThreeButton = new Intent(getActivity(), ThreeButton.class); 
       ThreeButton.putExtra("phone", oslist.get(position).get("phonenumber")); 

       //u can put all variables similarly... 
       startActivity(ThreeButton); 
      } 
      } 
     ); 

In ThreeButton Activity 

onCreate(){ 

     Intent data=getIntent(); 
     data.getString("phone"); 
     Log.v("phone",phone); 


    } 
+0

謝謝你......我明白了 – 2014-10-19 08:53:45

0

您沒有訪問數據在您下一次活動的列表視圖中。而不是試圖從那裏獲得,你可以將所需的數據傳遞給新的活動。在調用startActivity之前,使用putExtra方法將數據輸入到intent數據中。順便說一句,如果你正在使用JSON,你應該檢查Gson庫來解析json

相關問題