Question

I am a new android Developer and i want to send String Array from Custom ADapter to main activity class but my code is not working and I am Using CustomAdapter class please can any help me

My ContactList class is following.....

public class ContactList extends Activity {
    ListView lvContacts;
    List<String> listname;
    List<String> listnumber;
    SimpleCursorAdapter adapter;
    ArrayList<PhoneList> arr;
    ArrayList<PhoneList> arrnumber;
    Button smsbtn;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        smsbtn = (Button) findViewById(R.id.sms);
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER, };
        Cursor c1 = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
                null, null, null);
            listname = new ArrayList<String>();
        listnumber = new ArrayList<String>();
        while (c1.moveToNext()) {
            String name = c1
                    .getString(c1
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = c1
                    .getString(c1
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            listname.add(name);
            listnumber.add(number);
        }
        c1.close();
        Object[] obj = listname.toArray();
        Object[] objnumber = listnumber.toArray();
        arr = new ArrayList<PhoneList>();
        String[] da = new String[obj.length];
        String[] danumber = new String[objnumber.length];
        for (int i = 0; i < obj.length; i++) {
            danumber[i] = (String) objnumber[i];
            da[i] = (String) obj[i];
            arr.add(new PhoneList(da[i], danumber[i], i));
        }
        ListView listView = (ListView) findViewById(R.id.lvContacts);
        CustomAdapter adpttt = new CustomAdapter(ContactList.this,
                R.layout.contacts_list_item, arr);
        listView.setAdapter(adpttt);
        smsbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = getIntent();
                String[] myStrings = intent.getStringArrayExtra("numberarray");
                for (int i = 0; i < myStrings.length; i++) {
                    Toast.makeText(getApplicationContext(), myStrings[i],
                            Toast.LENGTH_LONG).show();
                }

            }
        });
    }

}

My Custom Adapter Class is Following....

public class CustomAdapter extends ArrayAdapter<PhoneList> {
    int inflatr;
    Context ctxt;
    boolean bulkflag = false;
    static int k=0;
    ArrayList<PhoneList> data=new ArrayList<PhoneList>();
    private static final String[] arr1=new String[256];
    private static String[] arr=new String[256];
        public CustomAdapter(Context context, int resource, ArrayList<PhoneList> arr) {

        super(context, resource, arr);
        this.inflatr = resource;
        this.ctxt = context;
        this.data= arr;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         UserHolder holder = null;
         View row = convertView;
        if(convertView==null)
        {
             LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
             row = inflater.inflate(inflatr, parent, false);
             holder = new UserHolder();
             holder.textName=(TextView)row.findViewById(R.id.lblName);
             holder.stnumber=(TextView)row.findViewById(R.id.mobilenum);
             holder.checked=(CheckBox)row.findViewById(R.id.check);

//           holder.btnEdit = (ImageButton) row.findViewById(R.id.atomPay_removePay);
             row.setTag(holder);
        }
        else
        {
             holder = (UserHolder) row.getTag();            
        }
         final PhoneList dta=data.get(position);
         holder.checked.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                 int position = (Integer) v.getTag();
                 data.get(position).setselect(!data.get(position).getSelect());
                 notifyDataSetChanged();
                 arr=dta.getnumberr();
                 arr1[k]=arr[position];
            }
        });

         holder.checked.setChecked(dta.getSelect());

         holder.checked.setTag(position);
        for(int i=0; i<=data.size(); i++)
        {
            holder.textName.setText(dta.getName());
        }
        for(int j=0; j<=data.size(); j++)
        {
            holder.stnumber.setText(dta.getNumber());
        }

//      holder.btnEdit.setOnClickListener(new OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//              Toast.makeText(ctxt, "Humayoon    Siddiqueeeeeeeeeeeeeeeeeee"+dta.getName(), Toast.LENGTH_SHORT).show();
//              Intent moreIntent=new Intent(getContext(),ContactList.class);
//              String tName=dta.getName();
//              moreIntent.putExtra("Template",tName);
//              v.getContext().startActivity(moreIntent);
//               // ctxt.startActivity(ctxt,ContactList.class);
//          }
//      });
        Intent ii=new Intent(getContext(),ContactList.class);
        ii.putExtra("numberarray", arr1);
        return row;
    }
    @Override
    public int getCount() {
//      // TODO Auto-generated method stub
        return data.size();
    }

    static class UserHolder {
        TextView textName;
        TextView textAddress;
        TextView textLocation;
        ImageButton btnEdit;
        Button btnDelete;
        TextView stnumber;
        CheckBox checked;
        }


}

My xml Layout is following....

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@layout/bg_player_header"
    >
    <ListView
        android:id="@+id/lvContacts"
        android:layout_width="match_parent"
        android:layout_height="422dp"
        android:divider="#FFEEEE" >
</ListView>

    <Button
        android:id="@+id/sms"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dip"
        android:layout_marginLeft="109dip"
        android:text="Send Sms"
        android:onClick="btnclicked"
         />

</LinearLayout>
Was it helpful?

Solution

Create function in your custom adapter for getting data you need. You need to have reference of the adapter in your main activity class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top