-2

In my project i got a string from one activity to other ListActivity.now i want to get this string to my adapter class.how can i do this.. below is my code, in this i want to get the fromMobileno string to my adapter class

    public  class MainActivity extends ListActivity {
        ListView lv;
        Cursor cursor;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list_contact);
            cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            startManagingCursor(cursor);
            String[] from={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone._ID};
            int[] to={R.id.name_entry,R.id.number_entry};

            adapterlistadapter=new adapter(this,R.layout.single_row1,cursor,from,to);
            setListAdapter(listadapter);

            lv=getListView();
            lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

            Bundle extras = getIntent().getExtras();
            String fromMobileno = extras.getString("KEY");



        }

        @Override()
        public long getSelectedItemId() {
            // TODO Auto-generated method stub
            return super.getSelectedItemId();
        }

        @Override
        public int getSelectedItemPosition() {
            // TODO Auto-generated method stub
            return super.getSelectedItemPosition();
        }
        class adapter extends SimpleCursorAdapter implements Filterable {
            private Context context;

            private int layout;
            public adapter(Context context, int layout, Cursor c,
                    String[] from, int[] to) {
                super(context, layout, c, from, to);
                this.context=context;
                this.layout=layout;

                // TODO Auto-generated constructor stub

            }


            @Override
            public void bindView(View v, Context context, Cursor c) {

                ....


        }
}
1
  • have you ever heard about constructors?
    – DroidDev
    Feb 3, 2015 at 6:12

1 Answer 1

0

i want to get this string to my adapter class.how can i do this

Add one more String parameter to adapter which to get required value from class:

 String strfromMobileno="";
 public adapter(Context context, int layout, Cursor c,
                    String[] from, int[] to,String fromMobileno) {
     super(context, layout, c, from, to);
     ///... your code here 
     strfromMobileno=fromMobileno;
  }

and on onCreate method get value from intent before creating object of adapter class.

And another way is because adapter is inner class of MainActivity class so you can also access outer class variables in inner class.declare fromMobileno as class level variable instead of inside onCreate method then use MainActivity.this.fromMobileno to access fromMobileno in adapter class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.