菜单项没有响应和方法没有响应

时间:2018-04-08 21:28:28

标签: android menu android-viewpager

第一个问题是点击时菜单项action_editaction_delete没有响应。

其次,当您看到方法insertPostion(String name)被称为PageViewAdapter1构造函数时,该方法不执行或不执行应该执行的操作。请问问题是两个但有点属于同一个活动。当没有添加mViewPager时,菜单会表现并响应,但是当添加视图寻呼机时,它们都不会响应。

    public class AddEdit extends AppCompatActivity{

             private ViewPager mViewPager;
             private  PageViewAdapter1  slider;
             private List informatn = new ArrayList<>();
             private int current;

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

       Toolbar tool=(Toolbar) findViewById(R.id.toolbar1);

        setSupportActionBar(tool);
        Log.d("m","addedit on create instantiated");
       mViewPager=(ViewPager) findViewById(R.id.pager);

        slider=new PageViewAdapter1(this);

        if( !slider.equals(null))
            Log.d("m","slider NOT null"+current);
        mViewPager.setAdapter(slider);
        Log.d("m","current inside oncreate"+current);
        mViewPager.setCurrentItem(current);

        if(mViewPager.isShown())
            Log.d("m","viewpager is shown");

        // load the contact

       // getSupportLoaderManager().initLoader(CONTACT_LOADER, null, this);

    }



            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                MenuInflater findMenuItems = getMenuInflater();
                findMenuItems.inflate(R.menu.fragment_details_menu, menu);
                return super.onCreateOptionsMenu(menu);
            }


            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_delete:
                        Toast.makeText(AddEdit.this,"delete ",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.action_edit:
                        finish();
                        break;
                }
                return super.onOptionsItemSelected(item);
            }



    public class PageViewAdapter1 extends PagerAdapter {
        private Context mcontext;
        public String mname=getIntent().getStringExtra("contact");

        List informatn = new ArrayList<>();
        private SQLiteDatabase mDatabase;


        public PageViewAdapter1(Context context) {
            this.mcontext = context;
           Log.d("m","adapter called");
            Log.d("m","name"+" "+mname);
            informatn=getInfos();
            Collections.sort(informatn,new MyCompName());
            insertPostion(mname);
        }

        @Override

        public int getCount() {


            return informatn.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view==(View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            LayoutInflater mInflater=(LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View item_view=mInflater.inflate(R.layout.detail,container,false);
            try
            {
            Log.d("m","instantiateItem");

            TextView nameTextView; // displays contact's name
            TextView phoneTextView; // displays contact's phone
            TextView emailTextView; // displays contact's email
            TextView streetTextView; // displays contact's street
            TextView cityTextView; // displays contact's city
            TextView stateTextView; // displays contact's state

            TextView zipTextView; // displays contact's zip

            // get the EditTexts

            nameTextView = (TextView) item_view.findViewById(R.id.nameTextView1);
            phoneTextView = (TextView) item_view.findViewById(R.id.phoneTextView1);
            emailTextView = (TextView) item_view.findViewById(R.id.emailTextView1);
            streetTextView = (TextView) item_view.findViewById(R.id.streetTextView1);
            cityTextView = (TextView) item_view.findViewById(R.id.cityTextView1);
            stateTextView = (TextView) item_view.findViewById(R.id.stateTextView1);
            zipTextView = (TextView) item_view.findViewById(R.id.zipTextView1);


            nameTextView.setText(informatn.get(position).name);
            phoneTextView.setText(informatn.get(position).phone);
            streetTextView.setText(informatn.get(position).street);
            emailTextView.setText(informatn.get(position).eamil);
            cityTextView.setText(informatn.get(position).City);
            stateTextView.setText(informatn.get(position).state);
            zipTextView.setText(informatn.get(position).zip);


            container.addView(item_view);
        }
            catch(Exception e)
        {
            e.printStackTrace();
        }

            return item_view;


        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((GridLayout)object);
        }

        private ACursorWrapper queryInfo(String whereClause, String[] whereArgs){

            mDatabase = new AddressBookDatabaseHelper(mcontext)
                    .getWritableDatabase();
            Cursor cursor = mDatabase.query(
                    DatabaseDescription.Contact.TABLE_NAME,
                    null, // columns - null selects all columns
                    whereClause,
                    whereArgs,
                    null, // groupBy
                    null, // having
                    null // orderBy
            );
            return new ACursorWrapper(cursor);
        }




        public List getInfos() {

            List information = new ArrayList<>();
            ACursorWrapper cursor = queryInfo(null, null);
            try {
                cursor.moveToFirst();
                while (!cursor.isAfterLast()) {
                    information.add(cursor.getInfo());
                    cursor.moveToNext();
                }
            } finally {
                cursor.close();
            }
            return information;
        }


        public Info getInfo(int id) {

            ACursorWrapper cursor = queryInfo(
                    DatabaseDescription.Contact._ID + " = ?",
                    new String[] { id+"" }
            );
            try {
                if (cursor.getCount() == 0) {
                    return null;
                }
                cursor.moveToFirst();
                return cursor.getInfo();
            } finally {
                cursor.close();
            }
        }



    }


    public void insertPostion(String name){


        for(int i=0;i<informatn.size();i++){

            informatn.get(i).current=i;

            Log.d("m","indexes"+i);

        }
        for(int i=0;i<informatn.size();i++){
            Log.d("m","current"+i);
            if(informatn.get(i).name.equals(name)) {
                current = i;

                break;
            }



        }




    }


    class MyCompName implements Comparator { // eclipse ask for casting object
        public int compare(Object emp1, Object emp2) {

            String emp1Name = ((Info) emp1).name;

            String emp2Name = ((Info) emp2).name;

            return emp1Name.compareTo(emp2Name);
            //return (int)((emp2Name*100) - (emp1Name*100.0));
        }
    }

    }

0 个答案:

没有答案