Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need to call <code>Activity#startActivityForResult(Intent, int)</code> from your <code>BaseAdapter</code>, you should pass your Activity to BaseAdapter's constructor. You might already be doing this by calling:</p> <pre><code>ListNumberAdapter lna = new ListNumberAdapter(this, someListOfStrings); </code></pre> <p>But, in ListNumberAdapter's constructor, you downcast the Activity to Context.</p> <p>So, the first change you'll need to make is:</p> <pre><code>// In place of Context, use Activity // We can do this because Context class is // an ancestor of Activity class private YourActivity mActivity; // We'll use this variable to keep track of // the position of the button that's been clicked public int positionClicked; .... .... public ListNumberAdapter(YourActivity act, List&lt;String&gt; listNumber){ mActivity = act; .... .... } </code></pre> <p>Now, declare the ArrayList <code>listOfNumbers</code> that you pass to your adapter with class-scope, so that it is accessible from methods in your activity.</p> <p>Your activity's basic setup will now be:</p> <pre><code>public class YourActivity extends Activity { ListNumberAdapter listNumberAdapter; List&lt;String&gt; listOfNumbers; ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listView = ....; listOfNumbers = new ArrayList&lt;String&gt;(); // Popuplate 'listOfNumbers' listNumberAdapter = new ListNumberAdapter(this, listOfNumbers); listView.setAdapter(listNumberAdapter); .... } } </code></pre> <p>I will assume that you are able to receive the phone number in an overriden <code>onActivityResult(int, int, Intent)</code> method (in your activity), but aren't able to display it in the ListView:</p> <pre><code>@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); .... .... // Contact's phone number String contactNumber = ...; // Set the String (contactNumber) at the specified position // Position is obtained by using the variable we defined in // the adapter listOfNumbers.set(listNumberAdapter.positionClicked, contactNumber); // Finally, call notifyDataSetChanged() on your adapter listNumberAdapter.notifyDataSetChanged(); } } </code></pre> <p>Change the <code>OnClickListener</code> for the button defined in <code>ListNumberAdapter</code>:</p> <pre><code>holder.btnAddress.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); // Update the 'positionClicked' variable using 'position' // You will need to mark 'position' as 'final' in // 'getView's' method signature positionClicked = position; // Use mActivity in place of mContext because // 'startActivityForResult(Intent, int)' is defined in Activity, // but not in Context mActivity.startActivityForResult(i,0); // Works now } }); </code></pre> <p>If you do not want to keep <code>positionClicked</code> as a public variable, declare it as private and provide an accessor method for it. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload