我的TabLayout
和SQLite
有一些问题,
我与IOT,MQTT合作,
从设备发布数据时,数据将更新为mysqlite,然后显示在列表视图中。
当我移至TabLayout
索引0和索引1时可以,问题是当我单击TabLayout
索引3并从SQLite
读取数据时显示错误。
SQLiteDatabase database = this.getWritableDatabase();
MainActivity
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("AirConditioner"));
tabLayout.addTab(tabLayout.newTab().setText("Chiller"));
tabLayout.addTab(tabLayout.newTab().setText("VoltMeter"));
tabLayout.addTab(tabLayout.newTab().setText("AmpereMeter"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
//Toast.makeText(FirstActivity.this, getSupportFragmentManager().toString().trim(), Toast.LENGTH_SHORT).show();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
PagerAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import sems.idi.controllergospel.fragments.TabAC;
import sems.idi.controllergospel.fragments.TabAmper;
import sems.idi.controllergospel.fragments.TabChiller;
import sems.idi.controllergospel.fragments.TabVolt;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabAC tab1 = new TabAC();
return tab1;
case 1:
TabChiller tab2 = new TabChiller();
return tab2;
case 2:
TabVolt tab3 = new TabVolt();
return tab3;
case 3:
TabAmper tab4 = new TabAmper();
return tab4;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
从MQTT发布
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("ListAC"));
TabAC片段
public class TabAC extends Fragment {
ListView listView;
ArrayAdapter<DataAc> adapter;
List<DataAc> itemListAC = new ArrayList<DataAc>();
AdapterAc adapterAc;
Context context;
public static TabAC newInstance() {
return new TabAC();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_ac, container, false);
initializeViews(v);
loadData();
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(ListAC,
new IntentFilter("ListAC"));
return v;
}
private BroadcastReceiver ListAC = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
loadData();
}
};
/*
INITIALIZE VIEWS
*/
private void initializeViews(View rootView)
{
listView = (ListView) rootView.findViewById(R.id.list_ac);
adapterAc = new AdapterAc(getActivity(), itemListAC);
listView.setAdapter(adapterAc);
}
/*
LOAD DATA
*/
private void loadData()
{
itemListAC.clear();
DbHelper SQLite = new DbHelper(getActivity());
ArrayList<HashMap<String, String>> row = SQLite.getDataAC();
for (int i = 0; i < row.size(); i++) {
String poster = row.get(i).get(DbHelper.F_KD);
String title = row.get(i).get(DbHelper.F_TI);
String title0 = row.get(i).get(DbHelper.F_TO);
String title1 = row.get(i).get(DbHelper.F_AR);
String title2 = row.get(i).get(DbHelper.F_SG);
String title3 = row.get(i).get(DbHelper.F_DT);
String title4 = row.get(i).get(DbHelper.F_PS);
String title5 = row.get(i).get(DbHelper.F_TPA);
DataAc data = new DataAc();
data.setKD(poster);
data.setTI(title);
data.setTO(title0);
data.setAR(title1);
data.setSG(Integer.parseInt(title2));
data.setDT(title3);
data.setPS(title4);
data.setTPA(title5);
itemListAC.add(data);
//Toast.makeText(MainActivity.this,"Kode Device" + poster + "|" + title, Toast.LENGTH_SHORT).show();
}
adapterAc.notifyDataSetChanged();
}
@Override
public String toString() {
return "TabAc";
}
}
DbHelper类中的错误
public ArrayList<HashMap<String, String>> getDataAC() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT KD, TI, TOs, AR, SG, DT, PS, TPA FROM " + TABLE_Isems + " WHERE TP = '1'";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put(F_KD, cursor.getString(0));
map.put(F_TI, cursor.getString(1));
map.put(F_TO, cursor.getString(2));
map.put(F_AR, cursor.getString(3));
map.put(F_SG, cursor.getString(4));
map.put(F_DT, cursor.getString(5));
map.put(F_PS, cursor.getString(6));
map.put(F_TPA, cursor.getString(7));
wordList.add(map);
} while (cursor.moveToNext());
}
Log.e("select sqlite ", "" + wordList);
database.close();
return wordList;
}