-1

In ListView here i have all my contacts with check box. When i select 2 contacts from list and hit a button then selected list's value should be display in next activity. How can i do this?

Its my Activity class :

public class ContactListActivity extends Activity implements OnItemClickListener {

private ListView listView;
private List<ContactBean> list = new ArrayList<ContactBean>();

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listView = (ListView) findViewById(R.id.list);
    listView.setOnItemClickListener(this);

    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    while (phones.moveToNext()) {

        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        ContactBean objContact = new ContactBean();
        objContact.setName(name);
        objContact.setPhoneNo(phoneNumber);
        list.add(objContact);
    }
    phones.close();



    ContanctAdapter objAdapter = new ContanctAdapter(ContactListActivity.this, R.layout.alluser_row, list);
    listView.setAdapter(objAdapter);

    if (null != list && list.size() != 0) {
        Collections.sort(list, new Comparator<ContactBean>() {

            @Override
            public int compare(ContactBean lhs, ContactBean rhs) {
                return lhs.getName().compareTo(rhs.getName());
            }
        });
        AlertDialog alert = new AlertDialog.Builder(ContactListActivity.this).create();
        alert.setTitle("");

        alert.setMessage(list.size() + " Contact Found!!!");

        alert.setButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alert.show();

    } else {
        showToast("No Contact Found!!!");
    }
}

private void showToast(String msg) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onItemClick(AdapterView<?> listview, View v, int position, long id) {
    ContactBean bean = (ContactBean) listview.getItemAtPosition(position);
    showCallDialog(bean.getName(), bean.getPhoneNo());
}

private void showCallDialog(String name, final String phoneNo) {
    AlertDialog alert = new AlertDialog.Builder(ContactListActivity.this).create();
    alert.setTitle("Call?");

    alert.setMessage("Are you sure want to call " + name + " ?");

    alert.setButton("No", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    alert.setButton2("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            String phoneNumber = "tel:" + phoneNo;
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(phoneNumber));
            startActivity(intent);
        }
    });
    alert.show();
}

And My Adapter Class to Hold Data is

public class ContanctAdapter extends ArrayAdapter<ContactBean> {

private Activity activity;
private List<ContactBean> items;
private int row;

private LayoutInflater inflater = null;

public ContanctAdapter(Activity act, int row, List<ContactBean> items) {
    super(act, row, items);
    this.activity = act;
    this.row = row;
    this.items = items;
    this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(row, null);
        holder.tvname = (TextView) convertView.findViewById(R.id.tvname);
        holder.tvPhoneNo = (TextView) convertView.findViewById(R.id.tvphone);
        holder.checkbox = (ImageView) convertView.findViewById(R.id.img_checkbox);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if ((items == null) || ((position + 1) > items.size()))
        return convertView;

    ContactBean objBean = items.get(position);

    holder.checkbox.setSelected((objBean.getIsSelected() == 1) ? true : false);

    if (holder.tvname != null && null != objBean.getName() && objBean.getName().trim().length() > 0) {
        holder.tvname.setText(Html.fromHtml(objBean.getName()));
    }
    if (holder.tvPhoneNo != null && null != objBean.getPhoneNo() && objBean.getPhoneNo().trim().length() > 0) {
        holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
    }

    holder.checkbox.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            items.get(position).isSelected = (v.isSelected()) ? 0 : 1;
            notifyDataSetChanged();

        }
    });

    return convertView;
}

public class ViewHolder {
    public TextView tvname, tvPhoneNo;
    private ImageView checkbox;
}
}
Krrish
  • 2,256
  • 18
  • 21

3 Answers3

2

There is multiple ways to achieve that :

Method 1:

Use static class setter and getter method:

create static class and set values from first activity and get value from second activity

Method 2:

Post your values through the intent

Method 3:

Use database to store data from one activity and get data from other activity

Method 4:

Use Shared preference

Example:

Post values using Intent like this

Post values in Shared preference

Another tutorial for Shared preference

Community
  • 1
  • 1
Amsheer
  • 7,046
  • 8
  • 47
  • 81
0

Try this, It may Help you

Add this code in your onitemClicklistener Listview Page

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
            long id) {


String TVNameitem = ((TextView) view.findViewById(R.id.tvname)).getText().toString();

String TVPhoneitem = ((TextView) view.findViewById(R.id.tvphone)).getText().toString();

Intent intent1 = new Intent(this,NextActivity.class);
intent1.putExtra("STRING_I_NEED_From_TVNAME", TVNameitem );
intent1.putExtra("STRING_I_NEED_From_TVPHONE",TVPhoneitem );
    startActivity(intent1);

 }

Add this code in your Nextactivty Oncreate for Getting Values, Then Show in Textview

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

     Bundle extras = getIntent().getExtras();
        String VALUE_1= extras.getString("STRING_I_NEED_From_TVNAME");
        String Value_2 =extras.getString("STRING_I_NEED_From_TVPHONE");

         TextView  Textview1=(TextView)findViewById(R.id.CompanyText);

    Textview1.setText(VALUE_1+":"+Value_2);

}
Android Dev
  • 421
  • 8
  • 26
  • Hi Mano thnxx for quick response but in my list i am using checkbox as selector and only selected List's value I have to display on another activity. – android_softy Jun 04 '15 at 06:18
  • in your checkbox is Checked Get your String Values ,then Transfer to Next Activity – Android Dev Jun 04 '15 at 06:22
  • Thnxx mano so much... its working for selection of list-view but my requirement is to select number of items and then when i click next Button then all records should be displayed on TextView which i had selected... – android_softy Jun 04 '15 at 10:38
  • Try any example used item Selected, I Have used only listview Item Select. – Android Dev Jun 04 '15 at 13:05
0

Create getter and setter to share contact details.

public class GetContacts {
  private String contactNumber;
  private String  contactName;

  GetContacts(){}// constructor without parameter.

  public String getContactNumber() {
      return contactNumber;
  }

  public void setContactNumber(String contactNumber) {
      this.contactNumber = contactNumber;
  }

  public String getContactName() {
      return contactName;
  }

  public void setContactName(String contactName) {
      this.contactName = contactName;
  }
}

Now set contact values to the setters in GetContact class

create an instance of GetContact class in your first Activity.

 GetContact getContact= new GetContact();

And Set Parameters.

 getContact.setContactNumber(phoneNumber);
 getContact.setContactName(name);

Now its time to get those values in second activity. create an instance of GetContact class in your second Activity like you did before.

And Get Parameters, and display into TextView.

 textView1.setText(getContact.getContactNumber(phoneNumber));
 textView2.setText(getContact.getContactName(name));
D_Alpha
  • 4,039
  • 22
  • 36