简体   繁体   English

如何保存列表视图状态并返回到该状态

[英]how to save list view state and get back to that state

i am getting data from MySQL database in JSON form and showing in custom list view.list view is very lengthy. 我正在以JSON形式从MySQL数据库获取数据并显示在自定义列表view.list视图中很长。 now i want that when i click on button in main activity it redirect me to the list view to the same position where i have left. 现在我希望当我在主要活动中单击按钮时,它将我重定向到列表视图,并移至我离开的位置。 thanks in advance. 提前致谢。

    public class ShowParahDetail extends AppCompatActivity {
ProgressDialog pd;
CustomAdapter c;
ArrayList<Ayah_textAR> ar_ayahAR;
ArrayList<Ayah_textTR> ar_ayah_TR;
JazzyListView lv;
String intent_parah_ID;
String intent_parahName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_parah_detail);
    intent_parah_ID=getIntent().getExtras().getString("parahID");
    intent_parahName=getIntent().getExtras().getString("parahName");
    setTitle(intent_parahName);
    pd = new ProgressDialog(ShowParahDetail.this, R.style.pdtheme);
    pd.setCancelable(false);
    pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
    pd.show();
    lv= (JazzyListView) findViewById(R.id.lvparahDetail);

    ar_ayahAR = new ArrayList<>();
    ar_ayah_TR = new ArrayList<>();
    ShowDataAR();
    ShowTranslationUR();
}

private void ShowTranslationUR() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            String RecievedString = "";
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("parahid", intent_parah_ID);
            Network network = new Network("showParahTranslationUR.php", params);

            try {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                RecievedString = network.ToRecieveDataFromWeb();
                JsonParsing jsonparsing = new JsonParsing(RecievedString);
                ArrayList<HashMap<String, String>> convertedarraydata = jsonparsing.ParsejsonArray(RecievedString);
                for (int i = 0; i < convertedarraydata.size(); i++) {
                    HashMap<String, String> positionHashmap;
                    positionHashmap = convertedarraydata.get(i);

                    String str_ayahText = positionHashmap.get("verseText");
                    String str_verseID = positionHashmap.get("verse_id");
                    String str_surahID = positionHashmap.get("surah_id");


                    Ayah_textTR ayahTR = new Ayah_textTR();
                    ayahTR.ayah_textTR = str_ayahText;
                    ayahTR.verse_id = str_verseID;
                    ayahTR.surah_id = str_surahID;
                    ar_ayah_TR.add(ayahTR);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

private void ShowDataAR() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            String RecievedString = "";
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("parahid", intent_parah_ID);
            Network network = new Network("showParahDetails.php", params);

            try {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                RecievedString = network.ToRecieveDataFromWeb();
                JsonParsing jsonparsing = new JsonParsing(RecievedString);
                ArrayList<HashMap<String, String>> convertedarraydata = jsonparsing.ParsejsonArray(RecievedString);
                for (int i = 0; i < convertedarraydata.size(); i++) {
                    HashMap<String, String> positionHashmap;
                    positionHashmap = convertedarraydata.get(i);
                    String str_ayahID = positionHashmap.get("verse_id");
                    String str_ayahText = positionHashmap.get("verseText");

                    Ayah_textAR ayahText = new Ayah_textAR();
                    ayahText.ayah_id = str_ayahID;
                    ayahText.ayah_textAR = str_ayahText;
                    ar_ayahAR.add(ayahText);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (ShowParahDetail.this == null)
                    return;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Parcelable state = lv.onSaveInstanceState();
                        lv.onRestoreInstanceState(state);
                        c = new CustomAdapter(ShowParahDetail.this, R.layout.cstm_parah, R.id.tv_parahNAME, ar_ayahAR);
                        lv.setAdapter(c);
                        if(pd!=null){
                            pd.dismiss();
                            pd = null;
                        }
                    }
                });
            }
        }
    }).start();
}


class CustomAdapter extends ArrayAdapter<Ayah_textAR> {
    public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<Ayah_textAR> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.cstm_ayah, parent, false);

        final TextView tv_ayah_text = (TextView) v.findViewById(R.id.tv_ayahText);
        final TextView tv_ayahTR = (TextView) v.findViewById(R.id.tv_ayahTR);

        try {
            Ayah_textAR ayah = ar_ayahAR.get(position);
            tv_ayah_text.setText(ayah.ayah_id + " : " + ayah.ayah_textAR);
            Ayah_textTR parahTR = ar_ayah_TR.get(position);
            tv_ayahTR.setText(parahTR.ayah_textTR);
        }catch (Exception e){
            notifyDataSetChanged();
        }

        });
        return v;
    }
}
public class Ayah_textAR {
    String ayah_id;
    String ayah_textAR;
}
public class Ayah_textTR {
    String verse_id;
    String ayah_textTR;
    String surah_id;
}



@Override
public void onDestroy() {
    super.onDestroy();
    if (pd != null) {
        pd.dismiss();
        pd = null;
    }
}

} }

You have three activities, A,B and C. Start your activity B with Intent. 您有三个活动,A,B和C。从Intent开始活动B。 And also start your activity C with intent. 并且还可以有目的地开始您的活动C。 Then, if you start your activity A and wish to save data which are exist in C, then start your activity A with intent. 然后,如果您开始活动A并希望保存C中存在的数据,则可以有意地开始活动A。 And don't finish your activity C. And if you want to resume activity C, just call onBackPressed() from your activity A. Don't forget finishing your activity A. For this call onFinish() . 而没有完成您的活动C.如果你想恢复活性C,只需调用onBackPressed()从你的活动A.不要忘记完成你的活动A.本次通话onFinish()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM