1

我希望点击 holder.btnAddress 按钮打开联系人地址,并在我从联系人地址中选择一个用户后在 holder.editNumber 控件中填写电话号码。我能怎么做?谢谢!

顺便说一句,我不知道我是否可以protected void onActivityResult (int requestCode, int resultCode, Intent data)在 ListNumberAdapter 类中添加该函数。

package ui;
import info.dodata.smsforward.R;
import java.util.List;
import bll.PublicPar;
import android.content.Context;
import android.content.Intent;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ListNumberAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<String> mListNumber;
    private Context mContext;

    public ListNumberAdapter(Context context, List<String> listNumber){
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       
        mListNumber=listNumber;
        mContext=context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mListNumber.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.sms_list_phone_number, null);
            holder.tvContactName = (TextView) convertView.findViewById(R.id.tvname);
            holder.editNumber = (EditText) convertView.findViewById(R.id.editnumber);
            holder.btnAddress=(Button)convertView.findViewById(R.id.btnAddress);
            holder.btnDelete=(Button)convertView.findViewById(R.id.btnDelete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.id=position;
        holder.tvContactName.setId(position);
        holder.editNumber.setId(position);
        holder.btnAddress.setId(position);
        holder.btnDelete.setId(position);

        holder.editNumber.setText(mListNumber.get(position));

        holder.btnAddress.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
                mContext.startActivity(i);              
                //mContext.startActivityForResult(i,0); //Don't work
            }
        });           

        return convertView;
    }


}


class ViewHolder {
    int id;
    TextView tvContactName;
    EditText editNumber;
    Button btnAddress;
    Button btnDelete;
}
4

2 回答 2

1

如果需要Activity#startActivityForResult(Intent, int)从Activity 调用BaseAdapter,则应将 Activity 传递给 BaseAdapter 的构造函数。您可能已经通过调用:

ListNumberAdapter lna = new ListNumberAdapter(this, someListOfStrings);

但是,在 ListNumberAdapter 的构造函数中,您将 Activity 向下转换为 Context。

因此,您需要进行的第一个更改是:

// 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<String> listNumber){
    mActivity = act;
    ....
    ....
}

现在,使用类范围声明传递给适配器的 ArrayList listOfNumbers,以便可以从 Activity 中的方法访问它。

您的活动的基本设置现在将是:

public class YourActivity extends Activity {

    ListNumberAdapter listNumberAdapter;
    List<String> listOfNumbers;
    ListView listView;

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

        listView = ....;

        listOfNumbers = new ArrayList<String>();

        // Popuplate 'listOfNumbers'

        listNumberAdapter = new ListNumberAdapter(this, listOfNumbers);

        listView.setAdapter(listNumberAdapter);
        ....
    }
}

我将假设您能够以覆盖的onActivityResult(int, int, Intent)方法(在您的活动中)接收电话号码,但无法在 ListView 中显示它:

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

    }
}

更改中OnClickListener定义的按钮ListNumberAdapter

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

如果您不想保留positionClicked为公共变量,请将其声明为私有并为其提供访问器方法。

于 2013-11-15T22:25:05.357 回答
0

致 user2558882:我可以使用 ((Activity) mContext).startActivityForResult(i, 50); 做同样的事 ?

于 2013-11-16T07:26:19.640 回答