使用Fragment中的图像实现列表视图时出错

时间:2017-04-16 20:48:35

标签: android listview android-fragments constructor

我是android的初学者。我正在尝试使用fragment中的图像制作listview。但是它没有完成。 我的代码是

ListoneFragment.java:(此处name1和name2是 strings.xml 文件中的字符串数组名称)

package fragments.h.safmical;

import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import safmical.h.R;

/**
 * Created by hadvani on 4/14/2017.
 */

public class ListOneFragment extends Fragment {
    String[] titles;
    String[] shortforms;
    int[] images={R.drawable.hcll,R.drawable.hno,R.drawable.hcll,R.drawable.hno,R.drawable.hcll,R.drawable.hno};
    ListView listView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.fragment_listone, container, false);
        Resources res =getResources();
        titles=res.getStringArray(R.array.name1);
        shortforms=res.getStringArray(R.array.name2);
        listView = (ListView) rootview.findViewById(R.id.list1);

       MyAdapter adapter=new MyAdapter(this,titles,images,shortforms);
        listView.setAdapter(adapter);
        return rootview;
    }
}
class MyAdapter extends ArrayAdapter<String>{
Context context;
    int[] images;
    String[] titleArray;
    String[] shortformArray;
    MyAdapter(Context c,String[] titles,int imgs[],String[] shortform){
     super(c,R.layout.fragment_listpattern,R.id.textView1,titles);
        this.context=c;
        this.images=imgs;
        this.titleArray=titles;
        this.shortformArray=shortform;

    }


    @Override
    public View getView(int position,  View convertView,  ViewGroup parent) {
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=inflater.inflate(R.layout.fragment_listpattern,parent,false);

        ImageView myImage=(ImageView)row.findViewById(R.id.imageView2);
        TextView myTitle=(TextView)row.findViewById(R.id.textView1);
        TextView myShortform=(TextView) row.findViewById(R.id.textView2);

        myImage.setImageResource(images[position]);
        myTitle.setText(titleArray[position]);
        myShortform.setText(shortformArray[position]);

        return row;
    }
}

这是我的xml文件

fragment_listone.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list1">

</ListView>
</FrameLayout>

fragment_listpattern.xml:(对于列表视图的单行模式)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="72dp">
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/imageView2"
        android:layout_toRightOf="@+id/imageView2"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_toEndOf="@+id/imageView2"
        android:layout_toRightOf="@+id/imageView2"
        android:text="TextView" />


</RelativeLayout>
</FrameLayout>

当我运行它时。它显示错误

Error:(37, 40) error: incompatible types: ListOneFragment cannot be converted to Context
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED

它在行中的 ListoneFragment.java 中显示建议 MyAdapter适配器=新的MyAdapter(此,标题,图片,缩写); 参数 那                  将方法'MyAdapter'的第一个参数从Context更改为'ListoneFragment'

当我这样做时,它会在MyAdapter类的构造函数中出错。 我该怎么办,是否有任何更正或有其他方法可以实现这一点。请帮忙吗?

1 个答案:

答案 0 :(得分:0)

使用活动context初始化适配器类:

ListOneFragment使用中:

MyAdapter adapter=new MyAdapter(getActivity(), titles, images,shortforms);