2017-04-16 56 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