2015-02-05 75 views
0

嗨,我是新來的android我嘗試設置一個web服務的響應字符串的按鈕的文本,按鈕是在一個listview ..我已經嘗試了下面的代碼,但它不會工作..如何更改按鈕的文本。我如何設置json響應字符串android按鈕文本

這裏是我的代碼

public class MainActivity extends ListActivity implements RegisterContactToServer.AsyncResponseListener { 
    ListView lv; 
    Cursor cursor; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.list_contact); 

     cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
     startManagingCursor(cursor); 
     String[] from={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone._ID}; 
     int[] to={R.id.name_entry,R.id.number_entry}; 

     ContactList listadapter=new ContactList(this,R.layout.single_row1,cursor,from,to); 

     setListAdapter(listadapter); 
     lv=getListView(); 
     lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

    } 

    class ContactList extends SimpleCursorAdapter implements Filterable { 
     private Context context; 
     String strfromMobileno=""; 
     private int layout; 
     public ContactList(Context context, int layout, Cursor c, 
       String[] from, int[] to) { 
      super(context, layout, c, from, to); 
      this.context=context; 
      this.layout=layout; 

      // TODO Auto-generated constructor stub 

     } 

      @Override 
     public void bindView(View v, final Context context, Cursor c) { 

      int nameCol = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
      String name = c.getString(nameCol); 

      int noCol = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
      String no = c.getString(noCol); 
     final CheckBox p1 = (CheckBox) v.findViewById(R.id.checkBox1); 
      final CheckBox p2 = (CheckBox) v.findViewById(R.id.checkBox2); 
      final CheckBox p3 = (CheckBox) v.findViewById(R.id.checkBox3); 



      final TextView name_text = (TextView) v.findViewById(R.id.name_entry); 
      if (name_text != null) { 
       name_text.setText(name); 
      } 

      final TextView no_text = (TextView) v.findViewById(R.id.number_entry); 
      if (no_text != null) { 
       no_text.setText(no); 
      } 
      final Button btn=(Button)v.findViewById(R.id.button1); 
      btn.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 




        String contactName = name_text.getText().toString(); 
        String contactNo =no_text.getText().toString(); 

        /*Intent intent = getIntent(); 
        String fromMobileno = intent.getExtras().getString("MN");*/ 



        Contact contact=new Contact(); 
        contact.setFromMobileno(contactName); 
        contact.setToMobileno(contactNo); 


        if(p1.isChecked()){ 
         int priority1=Integer.parseInt(p1.getText().toString()); 
         contact.setPriority1(1); 
         }else{contact.setPriority1(0);} 

         if(p2.isChecked()) 
         { 
         int priority2=Integer.parseInt(p2.getText().toString()); 
         contact.setPriority2(1); 
         }else{contact.setPriority2(0);} 

         if(p3.isChecked()){ 
         int priority3=Integer.parseInt(p3.getText().toString()); 
         contact.setPriority3(1); 
         }else{contact.setPriority3(0);} 
         contact.setStatus(statusR); 

        if (!contactName.equals("")){ 

        RegisterContactToServer reg=new RegisterContactToServer(context); 
         reg.execute(contact); 

         } 




       } 
      }); 
     } 


    } 
    @Override 
    public void onresponse(String status, String jsonData) { 
     // TODO Auto-generated method stub 
     LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflator.inflate(R.layout.single_row1, null, false); 

     final Button btn=(Button)v.findViewById(R.id.button1); 
     btn.setText(jsonData); 

     Toast.makeText(getBaseContext(), jsonData, Toast.LENGTH_LONG).show(); 

    }} 
+0

TL; DR,你應該嘗試陳述你的問題儘可能清楚和儘可能短。儘量避免長時間的「複製粘貼」代碼。祝你好運。 – 2015-02-05 13:49:39

回答

0

你會需要修改你正在使用填充您的ListView適配器的內容,你想在列表元素中的數據。然後,您將指示系統刷新ListView(notifyDataSetChanged())並用新刷新的數據重新繪製ListView。

相關問題