将itemchecked重定向到新活动

时间:2014-07-22 19:23:31

标签: android android-intent

我已阅读并尝试关注stackoverflow中的其他一些线程,我不了解如何使其工作。

简要说明。我有一个活动将联系人列表加载到自定义列表视图中。我可以使用复选框选择联系人。我还有一个完成按钮和一个取消按钮。选择完成按钮后,它将获取所有复选框项目,并在具有自己的自定义列表视图的新活动中显示联系人。

我的问题是如何设置单击按钮以仅显示所选联系人。

这是我的完成按钮代码非常粗糙:

     private Button mDoneButton, mCancelButton;

ListView guestListView;
ProgressDialog guestProgressDialog;
ArrayList<String> guestAA = new ArrayList<String>();
ArrayList<String> guestNum = new ArrayList<String>();
//ArrayList<String> guestEmail = new ArrayList<String>();

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

    setContentView(R.layout.contacts);

    guestListView = (ListView) findViewById(R.id.ListView);

    new AsyncTask<Void, Void, Void>() {
        @Override
        protected void onPreExecute() {
            guestProgressDialog = ProgressDialog.show(contactDisplayActivity.this, "Loading...", "Please Wait", true, false);
        } //end of on PreExecute method

        @Override
        protected Void doInBackground(Void... params) {
            getGuestContacts();

            return null;
        } //end of doInBackground method

        @Override
        protected void onPostExecute(Void result) {
            //getGuestContacts();
            guestProgressDialog.dismiss();
            CustomAdapter guestCustomAdapter = new CustomAdapter(contactDisplayActivity.this);
            guestListView.setAdapter(guestCustomAdapter);
        } //end of onPostExecute Method
    } .execute((Void[]) null);

    //Done Button
    mDoneButton = (Button)findViewById(R.id.doneButton);
    mDoneButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            long [] guestIds = guestListView.getCheckedItemIds();
            for (long guestId : guestIds ) {
                getGuestContacts();

            }
            Intent myIntent = new Intent(contactDisplayActivity.this, GuestList.class);
            startActivity(myIntent);
        }
    });

    //Cancel Button
    mCancelButton = (Button)findViewById(R.id.cancelButton);
    mCancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //mListener.onFragmentInteraction();
            //((PlanMeMainActivity) getActivity()).newActivityToLaunch(1);
            Intent myIntent = new Intent(contactDisplayActivity.this, EventDetails.class);
            startActivity(myIntent);
        }
    });
}

private void getGuestContacts() {
    ContentResolver guestContactResolver = getContentResolver();
    Cursor guestCursor = guestContactResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (guestCursor.getCount() > 0) {
        while (guestCursor.moveToNext()) {
            String guestId = guestCursor.getString(guestCursor.getColumnIndex(ContactsContract.Contacts._ID));
            String guestName = guestCursor.getString(guestCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            //String guestEmail = guestCursor.getString(guestCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));

            guestAA.add(guestName);


            if (Integer.parseInt(guestCursor.getString(guestCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor phoneCursor = guestContactResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{guestId}, null);

                while (phoneCursor.moveToNext()) {
                    String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    guestNum.add(phoneNumber);
                }

                //while (phoneCursor.moveToNext()) {
                  //  String email = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
                    //guestEmail.add(email);
                //}
                phoneCursor.close();
            }
        }
    }

}

public class CustomAdapter extends BaseAdapter {

    private Context mContext;

    public CustomAdapter(Context context) {
        mContext = context;

    }

    public int getCount() {
        return guestNum.size();
    }

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

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        final int pos = position;

        if (convertView == null) {
            holder = new ViewHolder();

            convertView = LayoutInflater.from(mContext).inflate(R.layout.contacts_display, null);

            holder.textviewName = (TextView) convertView.findViewById(R.id.contactsTextView1);
            holder.textviewNumber = (TextView) convertView.findViewById(R.id.contactsTextView2);
            //holder.textViewEmail = (TextView) convertView.findViewById(R.id.contactsTextView3);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.contactsCheckBox);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.checkbox.setId(position);
        holder.textviewName.setId(position);
        holder.textviewNumber.setId(position);
        //holder.textViewEmail.setId(position);

        holder.textviewName.setText(guestAA.get(position));
        holder.textviewNumber.setText("No. " + guestNum.get(position));
        //holder.textViewEmail.setText(guestEmail.get(position));

        holder.id = position;
        return convertView;
    }
}

static class ViewHolder {
    TextView textviewName;
    TextView textviewNumber;
    //TextView textViewEmail;
    CheckBox checkbox;
    int id;

}

}

getGuestContacts方法显示联系人列表。

任何帮助或方向都会很棒。感谢。

0 个答案:

没有答案