gpt4 book ai didi

安卓 : Custom ListView Disappear After Orientation Change

转载 作者:行者123 更新时间:2023-11-29 01:41:48 26 4
gpt4 key购买 nike

问题

我创建了自定义 ListView,其中包含自定义 View ,以及每个列表项的一些 TextView 。一切都按我的预期工作,直到设备方向改变,ListView 消失。我知道当方向改变 Activity 重新发生时会发生问题。但是,我已经在应用程序中将必要的文件保存为 JSON 字符串格式,当 Activity 重新创建时,我发现数据已加载并成功运行,直到代码结束没有强制关闭但是,listview 没有显示?

注意

我确信我的保存数据是可用的,因为这个文件用于防止从互联网上获取数据(如果可用的话),我已经测试过了,它可以工作!

我的自定义 ListView 有 ListView 类提供数组列表和适配器类,但我不能在这里提供,因为它太长了。

主类

   @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule);

aq = new AQuery(this);
lvSchedule = (ListView) findViewById(R.id.listView_schedule);
scheduleLvItems = new ArrayList<Schedule_ListView>();
dm = new DataManager(this);


//check available in app data
String scheduleJSON = dm.readFile("scheduleJSON");
if (scheduleJSON != "empty"){
System.out.println(scheduleJSON);
renderData(scheduleJSON);
}
else{
String url = "http://requestURL/getSchedule_v2.php?+" +
"loginId="+yourID+
"&password="+yourPassword+
"&year=2013&semester=2";

aq.ajax(url,String.class, this,"cb1");
}

}


public void cb1(String url, String html, AjaxStatus status){

System.out.println(html);
renderData(html);

}

public void renderData(String jsonString){

try {

List<List<String>> monData = new ArrayList<List<String>>();
List<List<String>> tueData = new ArrayList<List<String>>();
List<List<String>> wedData = new ArrayList<List<String>>();
List<List<String>> thuData = new ArrayList<List<String>>();
List<List<String>> friData = new ArrayList<List<String>>();
List<List<String>> satData = new ArrayList<List<String>>();
List<List<String>> sunData = new ArrayList<List<String>>();

/*Note [Knowledge]
*JSON Symbol {,} = JSONObject
*JSON symbol [,] = JSONArray
*/

//Change JSONString to JSONArray
JSONArray jsonArray_data = new JSONArray(jsonString);

//Set jsonArray_data in global variable for further use
// ((Global_Variable)getApplicationContext()).setJsonSubject(jsonArray_data);

//Save Persistence Json Array in app data for fetch with out internet
dm.writeFile("scheduleJSON",jsonArray_data.toString());

for(int i = 0; i < jsonArray_data.length(); i++){
//Change object to String and stored each JSON item in Variable
JSONObject dataRecord = jsonArray_data.getJSONObject(i);

String yearSemester = dataRecord.getString("YEAR / SEM");
String courseID = dataRecord.getString("COURSE ID");
String courseName = dataRecord.getString("COURSE NAME");
String courseSection = dataRecord.getString("SECTION");
JSONArray courseInfo = dataRecord.getJSONArray("COURSE INFO");
String finalDate = dataRecord.getString("FINAL DATE");
String finalRoom = dataRecord.getString("FINAL ROOM");
String finalTime = dataRecord.getString("FINAL TIME");
String midtermDate = dataRecord.getString("MIDTERM DATE");
String midtermRoom = dataRecord.getString("MIDTERM ROOM");
String midtermTime = dataRecord.getString("MIDTERM TIME");
int subjectColor = getResources().getColor(SUBJECT_COLOR[i]);

for(int ii = 0; ii < courseInfo.length(); ii++){
//Change object to String and stored each JSON item in Variable
JSONObject infoRecord = courseInfo.getJSONObject(ii);

String day = infoRecord.getString("DAY").replace(".","").trim();
String tStart = infoRecord.getString("TIME").replace(" - ","").trim().substring(0,5);
String tStop = infoRecord.getString("TIME").replace(" - ","").trim().substring(5);
String roomStudy = infoRecord.getString("ROOM STUDY");
String instructor = infoRecord.getString("INSTRUCTOR");

List<String> dataRow = new ArrayList<String>();

if (day.contains("Mon")) {
dataRow.add(courseName); //course name
dataRow.add(tStart); //period start
dataRow.add(tStop); //period end
dataRow.add(String.valueOf(subjectColor)); //subject color
dataRow.add(String.valueOf(i)); //subject position in json
monData.add(dataRow);
} else if (day.contains("Tue")) {
dataRow.add(courseName); //course name
dataRow.add(tStart); //period start
dataRow.add(tStop); //period end
dataRow.add(String.valueOf(subjectColor)); //subject color
dataRow.add(String.valueOf(i)); //subject position in json
tueData.add(dataRow);
} else if (day.contains("Wed")) {
dataRow.add(courseName); //course name
dataRow.add(tStart); //period start
dataRow.add(tStop); //period end
dataRow.add(String.valueOf(subjectColor)); //subject color
dataRow.add(String.valueOf(i)); //subject position in json
wedData.add(dataRow);
} else if (day.contains("Thu")) {
dataRow.add(courseName); //course name
dataRow.add(tStart); //period start
dataRow.add(tStop); //period end
dataRow.add(String.valueOf(subjectColor)); //subject color
dataRow.add(String.valueOf(i)); //subject position in json
thuData.add(dataRow);
} else if (day.contains("Fri")) {
dataRow.add(courseName); //course name
dataRow.add(tStart); //period start
dataRow.add(tStop); //period end
dataRow.add(String.valueOf(subjectColor)); //subject color
dataRow.add(String.valueOf(i)); //subject position in json
friData.add(dataRow);
} else if (day.contains("Sat")) {
dataRow.add(courseName); //course name
dataRow.add(tStart); //period start
dataRow.add(tStop); //period end
dataRow.add(String.valueOf(subjectColor)); //subject color
dataRow.add(String.valueOf(i)); //subject position in json
satData.add(dataRow);
} else if (day.contains("Sun")){
dataRow.add(courseName); //course name
dataRow.add(tStart); //period start
dataRow.add(tStop); //period end
dataRow.add(String.valueOf(subjectColor)); //subject color
dataRow.add(String.valueOf(i)); //subject position in json
sunData.add(dataRow);
}
}
}

if(monData.size() != 0){
scheduleLvItems.add(new Schedule_ListView(
R.drawable.monday_mdpi,
monData,
getResources().getInteger(R.integer.COLLAPSED_HEIGHT_1)
));
}
if(tueData.size() != 0){
scheduleLvItems.add(new Schedule_ListView(
R.drawable.tuesday_mdpi,
tueData,
getResources().getInteger(R.integer.COLLAPSED_HEIGHT_1)
));
}
if(wedData.size() != 0){
scheduleLvItems.add(new Schedule_ListView(
R.drawable.wendesday_mdpi,
wedData,
getResources().getInteger(R.integer.COLLAPSED_HEIGHT_1)
));
}
if(thuData.size() != 0){
scheduleLvItems.add(new Schedule_ListView(
R.drawable.thursday_mdpi,
thuData,
getResources().getInteger(R.integer.COLLAPSED_HEIGHT_1)
));
}
if(friData.size() != 0){
scheduleLvItems.add(new Schedule_ListView(
R.drawable.friday_mdpi,
friData,
getResources().getInteger(R.integer.COLLAPSED_HEIGHT_1)
));
}
if(satData.size() != 0){
scheduleLvItems.add(new Schedule_ListView(
R.drawable.saturday_mdpi,
satData,
getResources().getInteger(R.integer.COLLAPSED_HEIGHT_1)
));
}
if(sunData.size() != 0) {
scheduleLvItems.add(new Schedule_ListView(
R.drawable.sunday_mdpi,
sunData,
getResources().getInteger(R.integer.COLLAPSED_HEIGHT_1)
));
}

//add padding top - bottom for listview
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout header = (LinearLayout) inflater.inflate(R.layout.listview_header, null);
lvSchedule.addFooterView(header, null, false);
lvSchedule.addHeaderView(header, null, false);
//setup listView Adapter
scheduleAdapter = new Schedule_Adapter(Schedule.this, R.layout.schedule_listview, scheduleLvItems);
//setup adapter to listView
lvSchedule.setAdapter(scheduleAdapter);
//setup OnClickListener
lvSchedule.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

int postion = i - lvSchedule.getHeaderViewsCount();
toggle(view, postion);
}
});

}catch (Exception e){
System.out.println(e);
}
}

最佳答案

发生这种情况是因为您在 onCreate 中将数据分发到 ListView 并且每当您更改方向时, Activity 将重新启动并且不会调用 onCreate。因此,它不会被分发。要解决此问题,只需将此行和相关行移动到 onResume

renderData(scheduleJSON); //take more important lines with it to onResume

应该是这样的:

protected void onResume()
{
super.onResume();
renderData(scheduleJSON); //take more important lines with it to onResume
}

第二种解决方案:

您可以通过将其添加到 Activity 标签中的 list 中来防止方向发生变化时 Activity 重新启动

<activity android:name="YourActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"> //this line

关于安卓 : Custom ListView Disappear After Orientation Change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24102815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com