2017-04-16 65 views
0

我是android.I的初学者,我试图在fragment.But中做图像listview,但没有完成。 这里我的代码是在实现列表视图与片段中的图像时出现错误

ListoneFragment.java:(这里NAME 1和NAME是我的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> 

当我运行it.It显示错误

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 in line
MyAdapter adapter = new MyAdapter(this,titles,images,shortforms);参数 将方法'MyAdapter'的第一个参数从上下文更改为'ListoneFragment'

当我这样做时,它会在MyAdapter类的构造函数中产生错误。 我该怎么做,有没有更正或有其他方法来实现this.Please帮助?

回答

0

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

ListOneFragment使用:

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

感谢man.it worked.But你能告诉我为什么以前编写的“本”作为一个参数不工作? – harsh

+0

片段是活动的一部分...所以对于适配器中的上下文,您需要传递活动上下文:在您的活动中使用'getActivity()'和'this'关键字来传递上下文。所以它不会在你的片段类中工作 – rafsanahmad007

相关问题