簡體   English   中英

如何保存列表視圖狀態並返回到該狀態

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

我正在以JSON形式從MySQL數據庫獲取數據並顯示在自定義列表view.list視圖中很長。 現在我希望當我在主要活動中單擊按鈕時,它將我重定向到列表視圖,並移至我離開的位置。 提前致謝。

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

}

您有三個活動,A,B和C。從Intent開始活動B。 並且還可以有目的地開始您的活動C。 然后,如果您開始活動A並希望保存C中存在的數據,則可以有意地開始活動A。 而沒有完成您的活動C.如果你想恢復活性C,只需調用onBackPressed()從你的活動A.不要忘記完成你的活動A.本次通話onFinish()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM