请帮助新手...
我正在使用此代码作为示例/学习练习LINK
在此示例程序中,有三个使用片段的可滑动选项卡。我创建了一个新的ListFragment,希望在其中一个选项卡中显示,但是当Eclipse编译时,我的PagerAdapter中出现以下错误:
Type mismatch: cannot convert from ContactListFragment to Fragment
这是我的代码:
PagerAdapter:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new TopRatedFragment();
case 1:
// Games fragment activity
return new ContactListFragment();
case 2:
// Movies fragment activity
return new MoviesFragment();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
这是我的ContactListFragment.java
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ContactListFragment extends ListFragment {
private ContactAdapter mAdapter;
private List<ContactItem> contactItemList = new LinkedList<ContactItem>();
private LayoutInflater mInflater;
public boolean taskRun = false;
long currentID = 0;
long currentContactID = 0;
public ContactListFragment() {}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mInflater = (LayoutInflater) getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(!taskRun){
FragmentTransaction ft = getFragmentManager().beginTransaction();
ListContactTask task= new ListContactTask(getActivity(),ft);
task.execute();
}
taskRun = true;
mAdapter = new ContactAdapter(getActivity(), R.layout.lvmain,R.id.key, contactItemList);
mAdapter .setInflater(mInflater);
mAdapter.setLayout(R.layout.lvmain);
setListAdapter(mAdapter );
ListView listView = getListView();
getListView().invalidate();
}
public void setDataList( List<ContactItem> list) {
Activity act = getActivity();
this.contactItemList = list;
if(act != null) {
mAdapter = new ContactAdapter(act, R.layout.lvmain,R.id.key, list);
mAdapter .setInflater(mInflater);
mAdapter.setLayout(R.layout.lvmain);
setListAdapter(mAdapter );
getListView().invalidate();
}
}
}
class ContactAdapter extends ArrayAdapter<ContactItem> {
private static String TAG = ContactAdapter.class.getName();
private LayoutInflater inflator = null;
List<ContactItem> pairList = null;
private int layout;
public ContactAdapter(Context context, int resource,
int textViewResourceId, List<ContactItem> objects) {
super(context, resource, textViewResourceId, objects);
this.pairList = objects;
}
public void setInflater(LayoutInflater mInflater) {
this.inflator = mInflater;
}
public void setLayout(int layout){
this.layout = layout;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
try {
if (convertView == null) {
convertView = this.inflator.inflate(
layout, null);
holder = new ViewHolder();
holder.key = (TextView) convertView
.findViewById(R.id.key);
holder.value = (TextView) convertView.findViewById(R.id.value);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
ContactItem pair = (ContactItem) getItem(position);
String key = pair.mDisplayName;
String value = pair.mPhone;
holder.key.setText(key);
holder.value.setText(value);
} catch (Exception e) {
Log.e(TAG, e.toString(), e);
}
return convertView;
}
static class ViewHolder {
TextView key;
TextView value;
}
public Filter getFilter() {
return null;
}
public long getItemId(int position) {
return 1;
}
public int getCount() {
return pairList.size();
}
public ContactItem getItem(int position) {
return (ContactItem) super.getItem(position);
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
@Override
public boolean isEmpty() {
return super.isEmpty();
}
}
我的lvmain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#00000000"/>
</RelativeLayout>
就像FYI一样,在尝试将ListFragment添加到寻呼机适配器中之前,所有内容都可以正常编译。任何帮助将不胜感激。提前致谢
最佳答案
在您的ContactListFragment
类import android.support.v4.app.ListFragment
中。
我认为它将起作用。
谢谢
关于android - 具有pagerAdapter的ListFragment,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23718217/