首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何保存列表视图状态并返回到该状态

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

Stack Overflow用户
提问于 2017-05-10 14:21:00
回答 1查看 56关注 0票数 0

我从JSON格式的MySQL数据库中获取数据,并在自定义列表view.list视图中显示,非常冗长。现在,我希望当我单击main activity中的按钮时,它会将我重定向到列表视图,转到我离开时的相同位置。提前谢谢。

代码语言:javascript
复制
    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;
    }
}

}

EN

回答 1

Stack Overflow用户

发布于 2017-05-10 15:05:53

你有三个活动,A,B和C。带着意图开始你的活动B。同时也要从意图开始你的活动C。然后,如果您开始您的活动A,并希望保存存在于C中的数据,则带着意图开始您的活动A。不要完成你的活动C,如果你想继续你的活动C,只需要从你的活动A调用onBackPressed()就可以了。不要忘记完成你的活动A。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43884901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档