Mary Jane
Mary Jane

Reputation: 41

How to get back old position when searching in clickable listview?

I am developing an app which has a clickable listview. I have created a search filter above the listview and it is working well. When I click on different items I get their corresponding details in an other activity which is good. However, when I am searching the same item using search filter, I am getting details at an other position. How do I get back the old position? Here is my code:

Code:

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

Upvotes: 0

Views: 142

Answers (2)

Vyacheslav
Vyacheslav

Reputation: 27211

ANother approach is to use startActivityForResult() and onActivityResult() I think this is more useful.

Let's assume Activity1 and Activity2 you use.

in Activity1 you launch 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
    }
}

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

Upvotes: 0

Muhammad Younas
Muhammad Younas

Reputation: 1603

At start make postiion=0 and in onclickupdate the position value

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

Upvotes: 3

Related Questions