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

时间:2017-05-10 06:21:00

标签: java android mysql

我从JSON格式的MySQL数据库中获取数据,并在自定义列表视图中显示.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;
    }
}

}

1 个答案:

答案 0 :(得分:0)

您有三个活动,A,B和C.使用Intent开始您的活动B.并且还有意图开始您的活动C.然后,如果您启动活动A并希望保存C中存在的数据,则启动您的活动A.并且没有完成您的活动C.如果您想要恢复活动C,只需从您的活动A中拨打onBackPressed()。不要忘记完成您的活动A.对于此致电{{1} }。