3

i'm using a custom ArrayAdapter to show my listView

This is my code I want to save my scroll position when go to activity and restore that when came back. thanks
package ir.ebiroux.love; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List;

import ir.ebiroux.database.DBAdapter;
import ir.ebiroux.database.Dastan;
import ir.ebiroux.love.R;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;

public class MainActivity extends ListActivity {
    DBAdapter db;
    List<Dastan> dastanha;
    ListView lst;

    boolean isAll;

    @SuppressLint("SdCardPath")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button btn_all = (Button) findViewById(R.id.main_all);
        Button btn_fav = (Button) findViewById(R.id.main_fav);



        isAll =true;// pish farz "All" hast
        lst = getListView();
        db = new DBAdapter(getBaseContext());
        db.open();
        Log.i(DBAdapter.TAG, "3");
        dastanha = db.getAllContacts();
        Log.i(DBAdapter.TAG, "4");

        if (dastanha.size() == 0) {

            String destPath = "/data/data/" + getPackageName() + "/databases";

            try {
                CopyDB(getBaseContext().getAssets().open("mydb"),
                        new FileOutputStream(destPath + "/dastanha"));
                Log.i(DBAdapter.TAG, "db copy shod");


                dastanha = db.getAllContacts();

                refreshDisplay()

                Log.i(DBAdapter.TAG, dastanha.size() + "= tedad dastanha");


            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else {
            refreshDisplay();
        }



        btn_all.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dastanha = db.getAllContacts();
                isAll =true;
                refreshDisplay();
            }
        });
        btn_fav.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dastanha = db.findFAVContacts();
                isAll=false;
                refreshDisplay();
            }
        });





    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

        Dastan dastan = dastanha.get(position);// migim dastani ke rush kelik


        Intent next = new Intent(this, ShowDastan.class);
        next.putExtra("thisdastan", dastan);
        startActivity(next);

    }


    public void CopyDB(InputStream inputStream, OutputStream outputStream)
            throws IOException {
        // ---copy 1K bytes at a time---
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        inputStream.close();
        outputStream.close();
    }

    public void refreshDisplay() {
        Log.i(DBAdapter.TAG, dastanha.size() + "= tedad dastanha");
        // ye log zadim befahmim ki be kiye

        ArrayAdapter<Dastan> adapter = new DastanAdapter(this, dastanha);

        setListAdapter(adapter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        if (isAll) {
            dastanha = db.getAllContacts();
        }else {
            dastanha=db.findFAVContacts();
        }

        refreshDisplay();
    }
}
  • Check this answer: http://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview – joao2fast4u Jul 17 '14 at 01:58

1 Answers1

0

You should @Override 2 more methods here. One for updating/saving the current scroll position into a variable which is "onScroll". In order for that to be triggered, add "implements OnScrollListener" to the signature of your ListActivity definition. Then in onCreate call "getListView().setOnScrollListener(this)" to register your scroll listener. Alternatively you can do this the same way that you did with the click listeners just dont forget to register.

Finally, override "onSaveInstanceState(Bundle yourBundle)" method of the activity and save your last updated scroll positions into the bundle.

You can then retrieve the saved scroll position in your "onCreate" method's bundle which is already there when you return to this activity. Use those positions to programmatically set the ListView's scroll position.

Ps: Sorry it's not very convenient to write any code from SO's android app but google is your friend for the missing snippets.

Kerem
  • 2,465
  • 19
  • 33