在可点击列表视图中搜索时如何找回旧位置?

时间:2016-01-11 13:03:34

标签: android listview android-listview

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

代码

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

2 个答案:

答案 0 :(得分:3)

在开始时,使postiion = 0并在onclickupdate中输入位置值

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

答案 1 :(得分:0)

另一种方法是使用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
    }
    }

在 活性2:

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