android - 带有 ImageView 和 TextView 的 Android fragment 的自定义适配器

标签 android android-fragments baseadapter

我正在尝试实现这样的东西 - have a look .

这是我的 BaseAdapter 类

public class ImageAdapter extends BaseAdapter {
private Context mContext;
LayoutInflater in = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder view;
    if (convertView == null) {

        view = new ViewHolder();
        convertView = in.inflate(R.layout.country_row, null);

        view.txtView = (TextView) convertView
                .findViewById(R.id.countryName);

        view.txtView.setText(countryName[position]);

        convertView.setTag(view);

    } else {
        view = (ViewHolder) convertView.getTag();
    }
    view.imgView = (ImageView) convertView.findViewById(R.id.countryImage);
    view.imgView.setImageResource(mThumbIds[position]);

    return convertView;
}

private Integer[] mThumbIds = { R.drawable.india_flag,
        R.drawable.china_flag, R.drawable.argentina_flag,
        R.drawable.england_flag, R.drawable.france_flag,
        R.drawable.united_states_flag, R.drawable.uruguay_flag,
        R.drawable.pakistan_flag, R.drawable.united_kingdom_flag,
        R.drawable.italy_flag, R.drawable.germany_flag,
        R.drawable.brazil_flag, R.drawable.belgium_flag,
        R.drawable.denmark_flag, R.drawable.czech_republic_flag,
        R.drawable.jamaica_flag, R.drawable.indonesia_flag,
        R.drawable.kenya_flag, R.drawable.korea_flag,
        R.drawable.ireland_flag,

};
private String[] countryName = { "India", "China", "Argentina", "England",
        "France", "United States", "Uruguay", "Pakistan", "United Kingdom",
        "Italy", "Germany", "Brazil", "Belgium", "Denmark",
        "Czech Republic", "Jamaica", "Indonesia", "Kenya", "Korea",
        "Ireland"

};

public static class ViewHolder {
    public ImageView imgView;
    public TextView txtView;
}

country_row.xml 包含 ImageView 和 TextView
Countrys 类扩展 Fragment,counties.xml 包含 GridView (我猜我在这个类中搞砸了一些东西,但我无法纠正它)

public class Countries extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.countries,container,false);
    GridView gridView = (GridView) view.findViewById(R.id.myGrid);
    gridView.setAdapter(new ImageAdapter(view.getContext())); 
    return view;


}

}
country_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp">

<ImageView
    android:layout_height="128dp"
    android:id="@+id/countryImage"
    android:layout_width="128dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">
</ImageView>

<TextView
    android:layout_height="wrap_content"
    android:id="@+id/countryName"
    android:layout_width="wrap_content"
    android:layout_below="@+id/countryImage"
    android:layout_marginTop="2dp"
    android:layout_centerHorizontal="true"
    android:textSize="18sp"
    android:ellipsize="marquee"></TextView>


</RelativeLayout>


countries.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >

<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="130dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:padding="10dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

</LinearLayout>

activity_main.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="horizontal">

  <fragment 
  android:name="com.example.fragmentsproject.Countries"
  android:id="@+id/frag1"
  android:layout_weight="1"
  android:layout_width="0px"
  android:layout_height="match_parent"/>

<fragment 
  android:name="com.example.fragmentsproject.Capitals"
  android:id="@+id/frag2"
  android:layout_weight="1"
  android:layout_width="0px"
  android:layout_height="match_parent"/>

</LinearLayout>

主要 Activity

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

}

这是我的 logcat

02-20 17:44:50.576: E/AndroidRuntime(1362): FATAL EXCEPTION: main
02-20 17:44:50.576: E/AndroidRuntime(1362): java.lang.RuntimeException: Unable to start     activity     ComponentInfo{com.example.fragmentsproject/com.example.fragmentsproject.MainActivity}:     android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-20 17:44:50.576: E/AndroidRuntime(1362):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at android.os.Looper.loop(Looper.java:132)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at android.app.ActivityThread.main(ActivityThread.java:4123)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at java.lang.reflect.Method.invokeNative(Native Method)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at java.lang.reflect.Method.invoke(Method.java:491)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at dalvik.system.NativeStart.main(Native Method)
02-20 17:44:50.576: E/AndroidRuntime(1362): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-20 17:44:50.576: E/AndroidRuntime(1362):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
02-20 17:44:50.576: E/AndroidRuntime(1362):     at android.view.LayoutInflater.inflate(LayoutInflater.java:479)



请有人帮助我,


提前致谢。

最佳答案

您提供的跟踪中的根本原因是“02-20 17:44:50.576: E/AndroidRuntime(1362): Caused by: android.view.InflateException: Binary XML file line #7: Error inflateing classfragment “, 因此请确保您正在使用:

import android.support.v4.app.Fragment;

作为一个类来扩展你的国家类。此外,使用 fragment 的 Activity 应该扩展 FragmentActivity 而不是常规 Activity。导入:

import android.support.v4.app.FragmentActivity;

此外,我不建议您以这种方式初始化类字段:

LayoutInflater in = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

因为 mContext 字段在构造函数调用后可以保存空引用。最好在构造函数中进行一些条件检查。

关于android - 带有 ImageView 和 TextView 的 Android fragment 的自定义适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15004897/

相关文章:

android - 使用哪个适配器 - BaseAdapter 还是 ArrayAdapter?

android - 通过 onClickListener 按自定义适配器中的位置获取数据

android - 从互联网下载图像,并放入 ImageView

android - 在 WebView 中完成 AdBlock

Java-Android : Unable to find explicit activity class

android - 在 Android 项目中使用 emma 为代码着色

android - fragment 中的回收 View 的 "RecyclerView: No Adapter attached; skipping layout"

java - 单选按钮 : Listener in XML or JAVA?

android - fragment 不显示任何内容

java - 我的 ListView 太慢了。并在向上滚动时崩溃