我正在使用自定义ArrayAdapter来显示我的listView
这是我的代码我想在转到活动时保存我的滚动位置并在恢复时恢复。
感谢
包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();
}
}
答案 0 :(得分:0)
你应该@Override 2更多方法。一个用于将当前滚动位置更新/保存到“onScroll”变量中。要触发该操作,请将“implements OnScrollListener”添加到ListActivity定义的签名中。 然后在onCreate中调用“getListView()。setOnScrollListener(this)”来注册你的滚动监听器。 或者你也可以像点击听众那样做,只是不要忘记注册。
最后,覆盖活动的“onSaveInstanceState(Bundle yourBundle)”方法,并将上次更新的滚动位置保存到包中。
然后,您可以在“onCreate”方法的捆绑包中检索已保存的滚动位置,该捆绑包在您返回此活动时已存在。使用这些位置以编程方式设置ListView的滚动位置。
Ps:抱歉,从SO的Android应用程序中编写任何代码都不是很方便,但谷歌是你丢失片段的朋友。