2013-06-23 72 views
0

任何人都可以告訴我如何將自定義viewList中的選定行的值傳遞給android中的另一個活動。我的意思是我有一個自定義的ListView,它有2個textView和一個複選框。當用戶選中複選框並點擊提交按鈕時,我想存儲2個textView字段的值並將其傳遞給下一個活動。傳遞給活動不是問題。問題是我不知道如何存儲檢查行的值。 代碼粘貼在下面。將選定行的內容傳遞給另一個活動

import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.util.Log; 
import android.util.SparseBooleanArray; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class contacts extends Activity implements OnItemClickListener { 
    static final String TAG = "contacts"; 
    ArrayList<String> contactName = new ArrayList<String>(); 
    ArrayList<String> contactNumber = new ArrayList<String>(); 
    ArrayList<String> checkboxArray = new ArrayList<String>(); 
    MyAdapter myAdapter; 
    Button AddNumber; 
    String numberIntent; 
    View vi; 
    ListView listView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listview); 

     getAllContacts(this.getContentResolver()); 
     listView = (ListView) findViewById(R.id.lists); 
     myAdapter = new MyAdapter(); 
     listView.setAdapter(myAdapter); 
     listView.setOnItemClickListener(this); 
     listView.setItemsCanFocus(false); 
     listView.setTextFilterEnabled(true); 
     // adding 
     AddNumber = (Button) findViewById(R.id.AddNumbers); 
     AddNumber.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       StringBuilder checkedContacts = new StringBuilder(); 
       Log.d(TAG, "onClick" + myAdapter.mCheckStates.size()); 
       for (int i = 0; i < contactName.size(); i++) 

       { 
        if (myAdapter.mCheckStates.get(i) == true) { 
         checkedContacts.append(contactName.get(i).toString()); 
         checkedContacts.append("\n"); 

        } else { 
         Log.d(TAG, "No OnClick" + contactName.get(i).toString()); 
        } 

       } 

       Toast.makeText(contacts.this, checkedContacts, 
         Toast.LENGTH_LONG).show(); 

       SparseBooleanArray checked = listView.getCheckedItemPositions(); 

      } 
     }); 

    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     myAdapter.toggle(arg2); 
    } 

    public void getAllContacts(ContentResolver cr) { 

     Cursor cursor = cr.query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, 
       null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME 
         + " COLLATE LOCALIZED ASC"); 
     while (cursor.moveToNext()) { 
      String name = cursor 
        .getString(cursor 
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
      String phoneNumber = cursor 
        .getString(cursor 
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      Log.d(TAG, "getAllContacts" + phoneNumber); 
      contactName.add(name); 
      contactNumber.add(phoneNumber); 

     } 

     cursor.close(); 
     Intent in = new Intent(this, TrackLogic.class); 
     in.putExtra("contact", contactName.toArray()); 

     in.putExtra("contact1", contactNumber.toArray()); 
    } 

    class MyAdapter extends BaseAdapter implements 
      CompoundButton.OnCheckedChangeListener { 
     private SparseBooleanArray mCheckStates; 
     LayoutInflater mInflater; 
     TextView phoneView, contactView; 
     CheckBox checkBox; 

     MyAdapter() { 
      mCheckStates = new SparseBooleanArray(contactName.size()); 
      mInflater = (LayoutInflater) contacts.this 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 

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

     @Override 
     public Object getItem(int position) { 
      return position; 
     } 

     @Override 
     public long getItemId(int position) { 

      return 0; 
     } 

     @Override 
     public View getView(final int position, View convertView, 
       ViewGroup parent) { 
      vi = convertView; 
      if (convertView == null) 
       vi = mInflater.inflate(R.layout.row, null); 
      contactView = (TextView) vi.findViewById(R.id.contact_name); 
      phoneView = (TextView) vi.findViewById(R.id.phone_number); 
      checkBox = (CheckBox) vi.findViewById(R.id.checkBox_id); 
      contactView.setText(contactName.get(position)); 
      phoneView.setText(contactNumber.get(position)); 
      checkBox.setTag(position); 
      checkBox.setChecked(mCheckStates.get(position, false)); 
      checkBox.setOnCheckedChangeListener(this); 


      return vi; 
     } 

     public boolean isChecked(int position) { 

      return mCheckStates.get(position, false); 
     } 

     public void setChecked(int position, boolean isChecked) { 
      mCheckStates.put(position, isChecked); 
      Log.d(TAG, "setChecked"); 
      notifyDataSetChanged(); 
     } 

     public void toggle(int position) { 
      setChecked(position, !isChecked(position)); 
     } 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
       boolean isChecked) { 

      mCheckStates.put((Integer) buttonView.getTag(), isChecked); 
     } 
    } 
} 

任何幫助將不勝感激..謝謝!

回答

0

如果你要使用共享的首選項(可能是個好主意) - 這些方針的東西可能是你在找什麼:

//in caller activity (save shared prefs value) ... 
String phone = phoneView.getText().toString(); 

private SharedPreferences prefs; 
prefs = getSharedPreferences("yourPrefName", Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = prefs.edit(); 
editor.putString("phone", phone); 
editor.commit(); 


//in called activity ... 
private SharedPreferences prefs; 
prefs = getSharedPreferences("yourPrefName", Context.MODE_PRIVATE); 
String phone=prefs.getString("phone", null); //will be null if "phone" not found 

希望這有助於...

+0

你能告訴我如何在我的代碼上實現這個?一個片段將是偉大的 – user2511882

+0

我應該如何通過兩個活動之間的首選項?我可以使用意圖嗎? – user2511882

+0

我的代碼相當完整 - 您不會傳遞前綴,前傳會傳遞數據。只是實施「原樣」並從那裏採取。 – IanB

0

Android爲存儲持久性數據提供了幾種不同的選項。它們記錄在以下鏈接中:http://developer.android.com/guide/topics/data/data-storage.html

您可能希望從基本SharedPreferences實施開始,然後從此處開始。

+0

我希望可能有一些方法可以使用checkBox可用的一些方法檢索checked行的值。我不確定如何去做 – user2511882

+0

你想存儲數據的時間有多長? – SBerg413

+0

那麼傳遞的數據將被下一個活動使用的默認值類型用於進一步的操作。 – user2511882

相關問題