我的标签是以这种方式动态创建的:
ActionBar bar = getActionBar();
JSONObject jobj1 = new JSONObject(jsonObjectReceived);
JSONArray shifts = jobj1.optJSONArray("resultObject");
for (int m = 0; m < shifts.length(); m++) {
bar.addTab(bar.newTab().setText("Shift " + (m + 1))
.setTabListener(tabListener));
}
在创建解析json_parser()
对象 jsonObjectReceived 的标签后,我调用方法json
,并在ListView
中显示内容。
private void json_parser(String jsonObjectReceived) {
try {
ArrayList<String> result = new ArrayList<String>();
JSONObject jsonRootObject = new JSONObject(jsonObjectReceived);
JSONArray arrayOfShifts = jsonRootObject
.getJSONArray("resultObject");
for (int i = 0; i < arrayOfShifts.length(); i++) {
JSONObject presentShift = arrayOfShifts.getJSONObject(i);
String timing = presentShift.getString("shiftTimeFrom");
TextView tv = (TextView) findViewById(R.id.ShiftTiming);
tv.setText(timing);
JSONArray arrayOfPatientsInPresentShift = presentShift
.getJSONArray("patientlist");
if (arrayOfPatientsInPresentShift == null
|| arrayOfPatientsInPresentShift.equals("")) {
Toast.makeText(getApplicationContext(), "NO Patient",
Toast.LENGTH_LONG).show();
}
for (int j = 0; j < arrayOfPatientsInPresentShift.length(); j++) {
JSONObject presentPatient = arrayOfPatientsInPresentShift
.getJSONObject(j);
String Name = presentPatient.getString("patientName");
String SL = presentPatient.getString("bookingSequence");
String Status = presentPatient.getString("statusFlag");
result.add(SL + " " + Name + "\n " + Status);
Log.i("MSG", result.toString());
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
result);
listView.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
问题是,ListView
显示了同一个标签中的所有项目。我知道它是因为外部的 for 循环。我想在单独的选项卡中显示来自每个外部“for”循环迭代的数据。我该怎么做?
我的JSON数据:The resultObject array contains multiple objects, and each object contains an array patientlist. patientlist contains multiple patients' details.
在尝试解决方案后Anshul Jain在答案中建议, onTabSelected()看起来像这样:
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
currentTabIndex = tabHost.getCurrentTab();
json_parser(jsonString1, currentTabIndex);
}
json_parser()
方法如下所示:
private void json_parser(String jsonObjectReceived, int i) {
try {
ArrayList<String> result = new ArrayList<String>();
JSONObject jsonRootObject = new JSONObject(jsonObjectReceived);
JSONArray arrayOfShifts = jsonRootObject
.getJSONArray("resultObject");
// for (int i = 0; i < arrayOfShifts.length(); i++) {
JSONObject presentShift =arrayOfShifts.getJSONObject(i);
String timing = presentShift.getString("shiftTimeFrom");
TextView tv = (TextView) findViewById(R.id.ShiftTiming);
tv.setText(timing);
JSONArray arrayOfPatientsInPresentShift = presentShift
.getJSONArray("patientlist");
for (int j = 0; j < arrayOfPatientsInPresentShift.length(); j++) {
JSONObject presentPatient = arrayOfPatientsInPresentShift
.getJSONObject(j);
String Name = presentPatient.getString("patientName");
String SL = presentPatient.getString("bookingSequence");
String Status = presentPatient.getString("statusFlag");
result.add(SL + " " + Name + "\n " + Status);
Log.i("MSG", result.toString());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
result);
listView.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
但是app崩溃了。
答案 0 :(得分:0)
您的活动应实施ActionBar.TabListener
,其中包含以下方法。从onTabSelected()
方法,使用正确的json调用jsonParser()
方法。
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//Gets called when a tab is selected
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
答案 1 :(得分:0)
很抱歉打扰你们,得到解决方案。
事实证明我一直在使用错误的方法来获取当前选项卡的索引。我使用getCurrentTab()
和TabHost
对象来调用它。
应该使用getPosition()
代替。
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
try {
json_parser(jsonString1, tab.getPosition());
} catch (Exception e) {
}
}