2015-05-13 49 views
0

我工作的Android項目有一個ListView幷包含一個TextView顯示聯繫人和聯繫人以json形式存儲在我的網站。如何提取textview整數值到另一個整數裏面listview onclicklistener

json link for contacts

我能夠解析我有沒有問題的聯繫人。但問題是解析的數據顯示爲TextView中的數字「776057619」,我希望這個TextView號碼被存儲在一個單獨的變量中。通過這樣做,我可以使用它來提示用戶「你想調用該特定號碼的天氣」??但我不就沒怎麼拉離TextView這個數字到一個單獨的變量,並用它來ListViewOnItemClickListener

內撥打下面是我的代碼

public class Contactmedia extends ListActivity { 

    private ProgressDialog pDialog; 
    JSONParser jsonParser = new JSONParser(); 

    private static final String READ_CONTACT_URL = "http://www.iamnotcrazy.hol.es/webservice/contact.php"; 

    private static final String TAG_NUMBER ="number"; 
    private static final String TAG_POSTS = "posts"; 

    private JSONArray mid = null; 
    //manages all of our comments in a list. 
    private ArrayList<HashMap<String, String>> mContactList; 

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

     setContentView(R.layout.contactmedialist); 
    } 

    @Override 
    protected void onResume() { 

     super.onResume(); 

     new LoadComments().execute(); 
    } 

    /** 
    * Retrieves json data of comments 
    */ 
    public void updateJSONdata() { 
     mContactList = new ArrayList<HashMap<String, String>>(); 

     JSONParser jParser = new JSONParser(); 

     JSONObject json = jParser.getJSONFromUrl(READ_CONTACT_URL); 

     try { 

      mid= json.getJSONArray(TAG_POSTS); 

      for (int i = 0; i < mid.length(); i++) { 
       JSONObject c = mid.getJSONObject(i);    
       String number = c.getString(TAG_NUMBER); 
       HashMap<String, String> map = new HashMap<String, String>();     
       map.put(TAG_NUMBER, number); 
       mContactList.add(map);  
      } 

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

    /** 
    * Inserts the parsed data into our listview 
    */ 
    private void updateList() { 

     ListAdapter adapter = new SimpleAdapter(this, mContactList, 
       R.layout.contactmediadesign, new String[] { TAG_NUMBER 
         }, new int[] { R.id.contactno 
         }); 

     setListAdapter(adapter); 

     ListView lv = getListView();  
     lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       /* this is where i have problem how to get that number without converting to string*/ 
       TextView v = (TextView)view.findViewById(R.id.contactno); 
       int myNum = Integer.parseInt(v.getText().toString()); 
       /* and here i want use that mynum after getiing phonenumber for calling purpouse ass shown below 
       * but its not working :(*/ 
       if (position == 0){ 
        Toast.makeText(getApplicationContext(), "yes you done it!!", Toast.LENGTH_SHORT).show(); 
        Intent callIntent = new Intent(Intent.ACTION_CALL); 
        callIntent.setData(Uri.parse("tel:"+"myNum")); 
        startActivity(callIntent); 
       } 

      } 
     }); 
    }  

    public class LoadComments extends AsyncTask<Void, Void, Boolean> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(Contactmedia.this); 
      pDialog.setMessage("Loading complaints..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 
     @Override 
     protected Boolean doInBackground(Void... arg0) { 

       updateJSONdata(); 

      return null; 

     } 

     @Override 
     protected void onPostExecute(Boolean result) { 
      super.onPostExecute(result); 
      pDialog.dismiss(); 

      updateList(); 
     } 
    } 
} 
+0

定義「它不工作」。什麼是/不是在做什麼?任何錯誤?此外,你真的應該花更多的時間格式化你的帖子,以便他們更容易閱讀。並且堆棧片段不適用於這種類型的代碼。使用代碼塊代替 – codeMagic

回答

0

既然你實際需要的字符串,你不必轉換爲在所有的整數:

TextView v = (TextView)view.findViewById(R.id.contactno); 
... 
callIntent.setData(Uri.parse("tel:"+ v.getText().toString())); 

或者如果你需要存儲的變量數由於某種原因,你應該使用String插件tead

TextView v = (TextView)view.findViewById(R.id.contactno); 
string myNum = v.getText().toString(); 
... 
callIntent.setData(Uri.parse("tel:" + myNum)); 
+0

它的工作形式我!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !坦克你謝謝:) –

+0

如果這是正確的答案,請接受它。 [如何接受答案的工作](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) –

0

我會建議獲取與該位置相關的數據,而不是試圖解析視圖來獲取它。

您可以使用adapter.getItem(int pos)adapter獲取數據。只需使adapter final或一個成員變量在OnItemClickListener中訪問它即可。

0

首先,你應該檢查你的清單文件,你應該有這樣的「應用程序」標籤之外,但「清單」標籤內:

<uses-permission android:name="android.permission.CALL_PHONE" /> 

嘗試做這樣的事情在你的代碼:

Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + v.getText().toString())); 
     startActivity(callIntent); 

,您應該使用字符串值,而不是INT

+0

謝謝你爲我工作 –

0

去除myNum的雙引號。你也可以使用基本的oop來保存你的整數值。 fyi,myNum不一定是一個整數。它可以是字符串

ListView lv = getListView();  
    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      /* this is where i have problem how to get that number without converting to string*/ 
      TextView v = (TextView)view.findViewById(R.id.contactno); 
      int myNum = Integer.parseInt(v.getText().toString()); 

      setNumber(myNum); //saving the myNum variable 
      System.out.println("number is: " + getNumber());//if you want to get the value of myNum, just call the getNumber() 

        if (position == 0){ 
         Toast.makeText(getApplicationContext(), "yes you done it!!", Toast.LENGTH_SHORT).show(); 
         Intent callIntent = new Intent(Intent.ACTION_CALL); 

         callIntent.setData(Uri.parse("tel:"+ myNum));//remove the quotation for myNum 

         startActivity(callIntent); 
        } 

     } 



private int number; 
void setNumber(int number){ 
    this.number=number; 
} 

int getNumber(){ 
    return number; 
} 
相關問題