我正在开发一个项目,该项目要求SlidingTabLayout选项卡中的ListViews填充数据,但是,当我启动程序时,列表不会出现。我已经调试过以确保数据实际发送到那里,这完全发生了。
活动本身的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<com.example.jafeth.wearnote.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/ColorPrimary"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
标签的xml:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swiperefresh1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.jafeth.wearnote.ZabbixActivity">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/errorTextView" />
活动代码:
package com.example.jafeth.wearnote;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import java.util.ArrayList;
/**
* An activity that shows all the possibilities for communication with Zabbix.
* Created by Jafeth on
*/
public class ZabbixActivity extends ListActivity {
private ViewPager pager;
private ViewPagerAdapter madapter;
private SlidingTabLayout tabs;
private CharSequence Titles[]={"Triggers","Events"};
private int Numboftabs =2;
private SwipeRefreshLayout swipeRefreshLayout1, swipeRefreshLayout2;
private ListView listView1, listView2;
private StableArrayAdapter stableArrayAdapter1, stableArrayAdapter2;
private ArrayList<ZabbixModel> models1, models2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zabbix);
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
madapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(madapter);
View view = getLayoutInflater().inflate(R.layout.tab_1, pager);
View view2 = getLayoutInflater().inflate(R.layout.tab_2, pager);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
listView1 = (ListView) view.findViewById(R.id.listView1);
listView2 = (ListView) view2.findViewById(R.id.listView2);
models1 = new ArrayList<ZabbixModel>();
models2 = new ArrayList<ZabbixModel>();
stableArrayAdapter1 = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, models1);
stableArrayAdapter2 = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, models2);
//Defines the global variables of the object.
token = LoginActivity.getToken();
swipeRefreshLayout1 = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh1);
swipeRefreshLayout2 = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh2);
// Assiging the Sliding Tab Layout View
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
swipeRefreshLayout1.setOnRefreshListener(new SwipeRefreshListener(this));
swipeRefreshLayout2.setOnRefreshListener(new SwipeRefreshListener(this));
listView1.setAdapter(stableArrayAdapter1);
listView2.setAdapter(stableArrayAdapter2);
fillList();
}
@Override
protected void clearList() {
models1.clear();
models2.clear();
stableArrayAdapter1.fillMap(models1);
stableArrayAdapter2.fillMap(models2);
stableArrayAdapter1.notifyDataSetChanged();
stableArrayAdapter2.notifyDataSetChanged();
}
private void addModelsToList1(ArrayList<ZabbixTriggerModel> z) {
for (int i = 0; i < z.size(); i++) {
models1.add(z.get(i));
}
stableArrayAdapter1.fillMap(models1);
stableArrayAdapter1.notifyDataSetChanged();
}
private void addModelsToList2(ArrayList<ZabbixEventModel> z) {
for (int i = 0; i < z.size(); i++) {
models2.add(z.get(i));
}
stableArrayAdapter2.fillMap(models2);
stableArrayAdapter2.notifyDataSetChanged();
}
/**
* Fills the list of subactivities.
*/
@Override
protected void fillList()
{
//Makes it so that the SwipeRefreshLayout can show that the list is refreshing, even if the refresh is started without a swipe.
if(!swipeRefreshLayout1.isRefreshing())
{
swipeRefreshLayout1.setRefreshing(true);
}
if(!swipeRefreshLayout2.isRefreshing())
{
swipeRefreshLayout2.setRefreshing(true);
}
//Empties the ListView so that it doesn't show the same thing(s) twice.
clearList();
//Creates the JSOn Obejct that is sent to the server.
JsonObject json = new JsonObject();
json.addProperty("usertoken", token);
//Sends the request.
Ion.with(this)
.load("https://zwm-tvdgeer-sping.c9.io/zabbix/triggers.php")
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (result != null) {
boolean b = true;
int i = 0;
ArrayList<ZabbixTriggerModel> models = new ArrayList<ZabbixTriggerModel>();
while (b) {
//Fills the ListView and notifies the user.
if (result.get(String.valueOf(i)) != null) {
ZabbixTriggerModel model = new ZabbixTriggerModel();
model.setTriggerid(result.get(String.valueOf(i)).getAsJsonObject().get("eventid").getAsString());
model.setDescription(result.get(String.valueOf(i)).getAsJsonObject().get("value").getAsString());
models.add(model);
} else {
b = false;
}
i++;
}
addModelsToList1(models);
Toast.makeText(getApplicationContext(), R.string.refreshComplete, Toast.LENGTH_SHORT).show();
} else if (e != null) {
//An error happened while collecting data, but the connection itself is functional.
Toast.makeText(getApplicationContext(), R.string.mainError + ": " + e.toString(), Toast.LENGTH_LONG).show();
} else {
//Something is wrong with the server connection.
Toast.makeText(getApplicationContext(), R.string.connectionError, Toast.LENGTH_LONG).show();
}
}
});
//Sends the request.
Ion.with(this)
.load("https://zwm-tvdgeer-sping.c9.io/zabbix/events.php")
.setJsonObjectBody(json)
.as(new TypeToken<JsonObject>() {})
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (result != null) {
boolean b = true;
int i = 0;
ArrayList<ZabbixEventModel> models = new ArrayList<ZabbixEventModel>();
while (b) {
//Fills the ListView and notifies the user.
if (result.get(String.valueOf(i)) != null) {
ZabbixEventModel model = new ZabbixEventModel();
model.setEventId(result.get(String.valueOf(i)).getAsJsonObject().get("eventid").getAsString());
model.setValue(result.get(String.valueOf(i)).getAsJsonObject().get("value").getAsString());
model.setClock(result.get(String.valueOf(i)).getAsJsonObject().get("clock").getAsString());
models.add(model);
} else {
b = false;
}
i++;
}
addModelsToList2(models);
Toast.makeText(getApplicationContext(), R.string.refreshComplete, Toast.LENGTH_SHORT).show();
}
else if (e != null) {
//An error happened while collecting data, but the connection itself is functional.
Toast.makeText(getApplicationContext(), R.string.mainError + ": " + e.toString(), Toast.LENGTH_LONG).show();
} else {
//Something is wrong with the server connection.
Toast.makeText(getApplicationContext(), R.string.connectionError, Toast.LENGTH_LONG).show();
}
}
});
swipeRefreshLayout1.setRefreshing(false);
swipeRefreshLayout2.setRefreshing(false);
}
/**
* Adds one or more Strings to the list of subactivities
* @param s An array of the Strings to be added.
*/
private void addStringsToList(ArrayList<String> s)
{
for (int i = 0;i < s.size();i++) {
strings.add(s.get(i));
}
stringAdapter.fillMap(strings);
stringAdapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_zabbix, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
任何人都可以帮我找到让ListViews再次出现的方法吗?