我对如何在通过FragmentPagerAdapter控制的片段中实现ListViews感到非常困惑。
目前我正在尝试返回一些JSON数据,并根据JSON中的值将数据分成3个单独的列表; '贷款','预订'和'预订'。这就像TeamTreehouse练习“构建博客阅读器Android应用程序”,我试图扩展它以进一步提高我对Android的教育。
当我尝试实现Fragments时,我开始遇到问题。根据我的理解,Fragments是具有自己的活动生命周期的可重用元素,因此实现包含所有逻辑的片段似乎是完美的,然后使用FragmentPagerAdapter将我需要的信息拉到单独的列表中。唯一的问题是ListView没有显示,尽管信息是在LogCat中提取的。我花了几天时间阅读和研究这个问题,我认为这可能与我在Fragment中实现ListView的方式有关,但我真的可以帮忙。
我已经为我的Fragment类添加了代码,我称之为'LoanFragment',以及FragmentPagerAdapter'LibraryPagerAdapter'和我的'Library'类,从中调用FragmentPagerAdapter:
public class LoanFragment extends Fragment {
ListView lv;
private final String KEY_TITLE = "Title";
private final String KEY_AUTHOR = "Author";
public static final String TAG = Library.class.getSimpleName();
protected JSONArray mTasksData;
SessionManager session;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.booked_fragment, container, false);
lv = (ListView) rootView.findViewById(R.id.listMe);
bindListView();
return rootView;
}
public void bindListView() {
new GetLibraryInformation(getActivity(),lv).execute("");
}
class GetLibraryInformation extends AsyncTask<Object, Void, JSONArray> {
ListView mListView;
Activity mContext;
public GetLibraryInformation(Activity context,ListView gview) {
this.mListView=gview;
this.mContext=context;
}
@Override
protected JSONArray doInBackground(Object... params) {
JSONArray jsonResponse = null;
// Get JSON data, all coming through fine
}
@Override
protected void onPostExecute(JSONArray result) {
mTasksData = result;
String[] keys = { KEY_TITLE, KEY_AUTHOR };
int[] ids = { android.R.id.text1, android.R.id.text2 };
String author = "";
String title = "";
List<HashMap<String, String>> fetch2 = new ArrayList<HashMap<String, String>>();
if(mTasksData == null) {
}
else {
try {
for (int i = 0; i < mTasksData.length(); i++) {
JSONObject task = mTasksData.getJSONObject(i);
if(task.has(KEY_TITLE)) {
title = task.getString(KEY_TITLE);
}
else {
title = "Title not set.";
}
if(task.has(KEY_AUTHOR)) {
author = task.getString(KEY_AUTHOR);
}
else {
author = "Author not set.";
}
HashMap<String, String> libraryInfo = new HashMap<String, String>();
libraryInfo = new HashMap<String, String>();
libraryInfo.put(KEY_TITLE, title);
libraryInfo.put(KEY_AUTHOR, author);
fetch2.add(libraryInfo);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), fetch2, android.R.layout.simple_list_item_2, keys, ids);
lv.setAdapter(adapter);
}
catch(JSONException e) {
Log.e(TAG, "Exception caught! ", e);
}
}
}
}
}
这是我的FragmentPagerAdapater,据我所知应该返回每个单独的Fragment(此时它们看起来都像LoanFragment):
public class LibraryPagerAdapter extends FragmentPagerAdapter {
public LibraryPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new LoanFragment();
case 1:
return new ReservationFragment();
case 2:
return new BookedFragment();
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
}
我的图书馆,扩展了FragmentActivity:
public class Library extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private LibraryPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "Loan", "Reservation", "Booked" };
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new LibraryPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tasks, menu);
return true;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
最后是'booked_fragment.xml',我试图用它来显示ListView:
FragmentPagerAdapter都使用相同的Fragment导入(android.support.v4.app.Fragment),但除了存在的标签外,应用程序只是返回完全空白!
任何帮助都将受到大力赞赏,因为我开始变成萝卜。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#ffffff"
android:layout_height="fill_parent" >
<ListView android:id="@+id/listMe"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="#f9f9f9"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
答案 0 :(得分:0)
这是对要传递给SimpleAdapter的布局的误解。 在您对构造函数的调用(Context context,List&gt; data,int resource,String [] from,int [] to)中,您需要传递列表项的布局,而不是包含ListView本身的布局。 当项目显示在ListView中并动态添加时,SimpleAdapter需要知道(并传递)此布局。如果将它与表进行比较,例如:您使用的布局定义了表的布局,列表项的布局定义了该表中一行的外观。