【问题标题】:Getting error in implementing list view with images in Fragment在片段中使用图像实现列表视图时出错
【发布时间】:2017-04-16 20:48:34
【问题描述】:

我是 android 的初学者。我正在尝试用片段中的图像制作列表视图。但它没有完成。 这是我的代码

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 adapter=new MyAdapter(this,titles,images,shortforms); 中显示建议 那 将方法 'MyAdapter' 的第一个参数从 Context 更改为 'ListoneFragment'

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

【问题讨论】:

    标签: android listview android-fragments constructor


    【解决方案1】:

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

    在您的ListOneFragment 中使用:

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

    【讨论】:

    • 谢谢 man.it 工作。但你能告诉我为什么以前写“this”作为参数不起作用?
    • 片段是活动的一部分...所以对于适配器内部的上下文,您需要传递活动上下文:getActivity()this 关键字在活动中用于传递上下文。所以它不会在你的片段类中工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 2014-12-02
    • 2015-06-05
    • 2015-11-01
    • 2019-01-17
    • 2015-09-03
    相关资源
    最近更新 更多