gpt4 book ai didi

android - 带有 pagerAdapter 的 ListFragment

转载 作者:行者123 更新时间:2023-11-29 00:20:09 32 4
gpt4 key购买 nike

请帮助新手...我将此代码用作示例/学习练习 LINK

在这个示例程序中,有三个使用 fragment 的可滑动选项卡。我创建了一个新的 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>

仅供引用,在我尝试将 ListFragment 添加到寻呼机适配器之前,一切都编译得很好。任何帮助将不胜感激。提前致谢

最佳答案

在您的 ContactListFragment 类中 import android.support.v4.app.ListFragment 它。我认为它会起作用。谢谢

关于android - 带有 pagerAdapter 的 ListFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718217/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com