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

问题描述 投票:0回答:2

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

代码:

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
2个回答
3
投票
@Override public void onResume() { super.onResume(); // Always call the superclass method first listView.setSelection(position); }

0
投票
假设您使用的是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();
}
© www.soinside.com 2019 - 2024. All rights reserved.