2017-04-11 8 views
0

私のアプリでは、カスタムリスナーのリストビューがあります。android - actvityは正しいですか?カスタムリスナーからの位置取得

私のアクティビティでonSaveInstanceStateとonRestoreInstanceStateを使用したいと思いますが、必要な情報は、クラスCustomListener内のメソッドOnItemClickです。

クリックされた項目のリストの位置を知る唯一の方法であるため、4つのonItemClickのパラメータの1つであるパラメータ "position"の値が必要です。そして、2つのmthods onSaveInstanceStateとonRestoreInstanceStateでは、アクティビティの状態を正しく復元する必要があります。 (私が行うには正しい方法ではないと思う方法を取得し、CustomListenerに設定し、2つの方法がonSaveInstanceStateとonRestoreInstanceStateある活動、それらを使用し作成する(ベストプラクティスがある)正しい - 1:

私の質問は2つですこれは私が他のアイデアを持っていない) 2 - 一度私はパラメータの位置を持っている、私はポートレートモードで私は横モード、システムに渡すため、CustomListenerのメソッドOnItemClick内にこのパラメータを設定する問題があるOnItemClickを呼び出さないため、メソッド内のパラメータは変更されず、ランドスケープレイアウトも変更されません。

この方法は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); 

} 

このメソッドは、クラスCustomListenerである:あなたの答えFOR

@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 !!!

答えて

0

私の推薦は、活動のメンバ変数として位置を保存し、このように、保存されたインスタンスの状態にそれを追加することで、あなたの最初の質問に答えるために:

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); 
    } 
    ... 
} 

次に、あなたのCustomListener限り

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

    mSelectedItemPosition = position; 
    ... 
} 

私はあなたの2番目の質問を誤解されるかもしれないが、onItemClickメソッド内の位置の値を設定する必要がない、B:クラスは、あなたの活動のクラスにネストされている、単にonItemClickメソッド内mSelectedItemPosition変数を設定クリックされたアイテムの位置が、そのメソッドの引数として渡されます。

+0

お返事ありがとうございました。 – soissy

0

お返事ありがとうございました。

あなたの助言の前に、私は内部クラスについて考えていませんでしたが、良い解決策です。

2度の質問では、私は解決しましたが、他に同じ問題がある場合、私はより良い説明をしようとします。

コードのレイアウトがonItemClick内でのみ変更された場合、どのようにmSelectedItemPositionを復元できますか?私は呼び出さないがシステムによってのみ呼び出されるメソッドですか?

内部クラスのようなCustomListenerを使用した後、私はMyActivity checkOrientation内にメソッドを作成しました。これは、コードの2つのポイント、onItemClick内およびonRestoreInstanceState内で使用されています。

この二つの方法

はMyActivityにあります。

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putInt("POSITION", myPosition); 
} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    myPosition = savedInstanceState.getInt("POSITION"); 
    checkOrientation(myPosition, null); 
} 

親がアクティビティ内onRestoreInstanceStateによって呼び出されたnullの場合は、この方法は、MyActivityでもあり、そしてあるケースでは、私は親を持っているので、私は私のコードを変更しましたonItemClickから、他のケースでは、私は活動の内側だと私は私が必要とするすべての方法があります。

private void checkOrientation(Integer position, AdapterView<?> parent){ 

    int imageID; 
    String description; 
    String name; 
    Bundle extras; 

    imageID = arrayFromNumberToId[position]; 
    description = hm.get(imageID); 
    namePokemon = hmFromIdToName.get(arrayFromNumberToId[position]); 

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

    if(parent != null) { 

     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); 
     } 

    } 
    else{ 

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

      Intent intent = new Intent(this, DescriptionActivity.class); 

      intent.putExtras(extras); 

      this.startActivity(intent); 

     } else { 

      ImageView imageViewLayout; 
      TextView textView; 


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

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

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

をこの方法では、CustomListener内部にある:

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

     myPosition = position; 

     checkOrientation(position, parent); 
    }