【问题标题】:How to get back old position when searching in clickable listview?在可点击的列表视图中搜索时如何恢复旧位置?
【发布时间】:2016-01-11 13:03:33
【问题描述】:

我正在开发一个具有可点击列表视图的应用程序。我在列表视图上方创建了一个搜索过滤器,它运行良好。当我点击不同的项目时,我会在另一个很好的活动中获得它们的相应详细信息。但是,当我使用搜索过滤器搜索同一项目时,我会在其他位置获取详细信息。怎么找回原来的位置?这是我的代码:

代码:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub

    //we use the items of the listview as title of the next activity
    String course = listAdapter.getItem(position);

    //we retrieve the description of the juices from an array defined in arrays.xml
    String[] description = getResources().getStringArray(R.array.description);
    final String courselabel = description[position];

    //retrieve content for the dialog
    String[] dialogmessage = getResources().getStringArray(R.array.dialogmessage);
    final String dialogmsg = dialogmessage[position];

    Intent intent = new Intent(getApplicationContext(), MainActivityas.class);
    intent.putExtra("message", message);
    startActivity(intent);
}

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

    在开始时使 postiion=0 并在 onclick 中更新位置值

    @Override
     public void onResume() {
     super.onResume();  // Always call the superclass method first
     listView.setSelection(position);
    }
    

    【讨论】:

      【解决方案2】:

      另一种方法是使用startActivityForResult()onActivityResult() 我觉得这个比较有用。

      假设您使用的是 Activity1 和 Activity2。

      在 Activity1 中启动 Activity2:

      public static final String ACT2 = "ACT2";
      public static final String POS = "POS";
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          // TODO Auto-generated method stub
      
          someAction(position);
      }
      
      public void someAction(int position){
      
          //we use the items of the listview as title of the next activity
          String course = listAdapter.getItem(position);
      
          //we retrieve the description of the juices from an array defined in arrays.xml
          String[] description = getResources().getStringArray(R.array.description);
          final String courselabel = description[position];
      
          //retrieve content for the dialog
          String[] dialogmessage = getResources().getStringArray(R.array.dialogmessage);
          final String dialogmsg = dialogmessage[position];
      
      
          Intent intent = new Intent(this, Activity2.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          intent.putExtra(POS,lv.getPosition());
          intent.putExtra("message", message);
          startActivityForResult(intent, _.ACT2);
      }
      
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (resultCode == RESULT_OK) {
              lv.setPosition(mPosition);//HERE POSITION SETUP
          }
      }
      

      在 Activity2 中:

      @Override
      public int mPosition = 0;
          protected void onCreate(Bundle savedInstanceState) {
      
          super.onCreate(savedInstanceState);
          Intent data = getIntent();
          String msg =data.getStringExtra("message", "");
          mPosition = data.getIntExtra(Activity1.POS, mPosition);
      }
      
      @Override
      public void finish() {     
      
          Intent data = new Intent();
          data.putExtra(Activity1.POS,mPosition);
          setResult(RESULT_OK, data); 
      
          super.finish();
      }
      

      【讨论】:

      • 请问我如何以及在何处将您的代码与您的解决方案结合起来?
      • @MaryJane , Activity1 - 这是您放置public void onItemClick 的第一个活动。 Activity2 - 是您的MainActivityas 课程。明白了吗?
      • 您能否根据您的解决方案修改我上面的代码,因为它在我的项目中变得混乱。请。谢谢。
      猜你喜欢
      • 2017-01-09
      • 1970-01-01
      • 2018-12-04
      • 2016-02-19
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-03
      相关资源
      最近更新 更多