Search code examples
androidandroid-layoutandroid-activityonitemclick

android - in actvity is correct get position from custom listener


In my app i have a listview with a custom listener.

I would like to use onSaveInstanceState and onRestoreInstanceState in my activity, but the only information that i need is in method OnItemClick inside my class CustomListener.

I need the value of parameter "position", one of 4 onItemClick's parameters, because is the only way to know in what position of list was clicked item. And in the two mthods onSaveInstanceState and onRestoreInstanceState i need to know to restore correctly the activity's state.

My questions are two: 1 - is correct(is best practise) create methods get and set in CustomListener and use them in Activity, where are the two methods onSaveInstanceState and onRestoreInstanceState (I think is not the right way to do this, but I don't have others ideas) 2 - once i have the parameter position, i have the problem to set this parameter inside CustomListener's method OnItemClick, because if i am in portrait mode and i pass to landscape mode, the system doesn't call OnItemClick and so the parameter inside the method will not modify, and also the landscape layout won't be modify.

this method is in MainActivity:

private void populate(ListView lv){

    ImageAdapter arrayAdapter;

    //set list view
    arrayAdapter = new ImageAdapter(this, R.layout.list_view_layout, arrayFromNumberToId, hmFromIdToName);
    lv.setAdapter(arrayAdapter);

    //set list listener
    listener=new CustomListener(hm, hmFromIdToName, arrayFromNumberToId);
    lv.setOnItemClickListener(listener);

}

This method is in class CustomListener:

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

    int imageID;
    String description;
    String name;



    imageID = arrayFromNumberToId[position];
    description = hm.get(imageID);
    name = hmIdToName.get(arrayFromNumberToId[position]);

    extras = new Bundle();
    extras.putInt("imageID", imageID);
    extras.putCharSequence("description", description);
    extras.putCharSequence("name", name);

    if(parent.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

        Intent intent = new Intent(parent.getContext(), DescriptionActivity.class);

        intent.putExtras(extras);

        parent.getContext().startActivity(intent);
    }
    else{
        ImageView imageViewLayout;
        TextView textView;


        textView = (TextView) parent.getRootView().findViewById(R.id.textDescription);
        textView.setText(description);

        imageViewLayout = (ImageView) parent.getRootView().findViewById(R.id.imageBig);
        imageViewLayout.setImageResource(imageID);

        textView = (TextView) parent.getRootView().findViewById(R.id.textName);
        textView.setText(name);
        textView.setTypeface(null, Typeface.BOLD);
    }
}

THANKS FOR YOUR ANSWERS!!!


Solution

  • To answer your first question, my recommendation is to store the position as a member variable of the activity and add it to the saved instance state, like this:

    class MyActivity extends Activity {
        private int mSelectedItemPosition;
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putInt(POSITION, mSelectedItemposition);
        }
    
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            savedInstanceState.getInt(POSITION);
        }
        ...
    }
    

    Then, as long as your CustomListener class is nested in your activity class, simply set the mSelectedItemPosition variable inside the onItemClick method:

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
        mSelectedItemPosition = position;
        ...
    }
    

    I might be misunderstanding your second question, but there is no need to set the position value inside the onItemClick method, because the position of the item clicked is passed to you as an argument for that method.