2016-03-02 122 views
4

我正在研究基於碎片的學校項目。我製作了XML文件,但在nearfragment.java中出現錯誤。你能告訴我錯誤在哪裏,因爲我熟悉Activity而不是Fragment。請幫我糾正錯誤。帶有片段的Android ListView

near_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
    tools:context="{relativePackage}.${activityClass}" > 

    <ListView 
     android:id="@+id/list_li" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" > 
    </ListView> 

</RelativeLayout> 

near_layout_mylist.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:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/icon_list" 
     android:layout_width="60dp" 
     android:layout_height="60dp" 
     android:padding="5dp" /> 

    <LinearLayout android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/item_list" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Medium Text" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="5dp" 
      android:padding="2dp" 
      android:textColor="#33CC33" /> 
     <TextView 
      android:id="@+id/textView1_list" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TextView" 
      android:layout_marginLeft="10dp"/> 
    </LinearLayout> 
</LinearLayout> 

CustomListNearAdapter.java

import android.app.Activity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class CustomListNearAdapter extends ArrayAdapter<String> { 

    private final Activity context; 
    private final String[] itemname; 
    private final Integer[] imgid; 

    public CustomListNearAdapter(Activity context, String[] itemname, Integer[] imgid) { 
     super(context, R.layout.near_layout_mylist, itemname); 
     // TODO Auto-generated constructor stub 

     this.context=context; 
     this.itemname=itemname; 
     this.imgid=imgid; 
    } 

    public View getView(int position,View view,ViewGroup parent) { 
     LayoutInflater inflater=context.getLayoutInflater(); 
     View rowView=inflater.inflate(R.layout.near_layout_mylist, null,true); 

     TextView txtTitle = (TextView) rowView.findViewById(R.id.item_list); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_list); 
     TextView extratxt = (TextView) rowView.findViewById(R.id.textView1_list); 

     txtTitle.setText(itemname[position]); 
     imageView.setImageResource(imgid[position]); 
     extratxt.setText("Description "+itemname[position]); 
     return rowView; 

    }; 
} 

NearFragment

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.GridView; 
import android.widget.ListView; 

/** 
* Created by Ratan on 7/29/2015. 
*/ 
public class NearFragment extends Fragment { 



    ListView list; 
    String[] itemname ={ 
      "Safari", 
      "Camera", 
      "Global", 
      "FireFox", 
      "UC Browser", 
      "Android Folder", 
      "VLC Player", 
      "Cold War" 
    }; 

    Integer[] imgid={ 
      R.drawable.akash, 
      R.drawable.akash, 
      R.drawable.akash, 
      R.drawable.akash, 
      R.drawable.akash, 
      R.drawable.akash, 
      R.drawable.akash, 
      R.drawable.akash, 
    }; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     View view = inflater.inflate(R.layout.near_layout, container,false); 
     CustomListNearAdapter adapter=new CustomListNearAdapter(this, itemname, imgid); 

     ListView listView = (ListView) view.findViewById(R.id.list_item); 
     listView.setAdapter(new CustomListNearAdapter(getActivity())); 
     return view; 


    } 


} 

錯誤在於nearfragment,我無法編寫片段的代碼。所以請給我一些指導。

+0

:Akashdeep Singh:什麼是錯誤?併發布logcat。 – kevz

+0

而不是這個,爲什麼你不使用'CustomActivity()' –

+0

'CustomListNearAdapter'你不應該使用構造函數的所有項目?活動,項目名稱和圖像? – KVISH

回答

1

在您的代碼中,您沒有在設置adapter時將正確的值傳遞給構造函數,這意味着在您的代碼行中。

listView.setAdapter(new CustomListNearAdapter(getActivity())); 

在上面的行中有問題,所以用下面的代碼行替換代碼。

 CustomListNearAdapter adapter=new CustomListNearAdapter(getActivity(), itemname, imgid); 

     ListView listView = (ListView) view.findViewById(R.id.list_li); 
     listView.setAdapter(adapter); 
+0

錯誤刪除,但應用程序崩潰 –

+0

@AkashdeepSingh ..什麼崩潰你得到..pleasse讓我konw –

+0

請參考我的答案。你必須爲'ListView'使用'onActivityCreated'。這個答案是不完整的。 – KVISH

1

使用getActivity()的代替在波紋線

CustomListNearAdapter adapter=new CustomListNearAdapter(this, itemname, imgid); 

,並設置適配器的ListView像波紋管

listView.setAdapter(adapter); 
1

我建議你寫的代碼活動加載後的ListView。所以,讓你的代碼象下面這樣:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.near_layout, container, false); 
    return view; 
} 

public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    CustomListNearAdapter adapter=new CustomListNearAdapter(getActivity(), itemname, imgid); 
    ListView listView = (ListView) view.findViewById(R.id.list_item); 
    listView.setAdapter(new CustomListNearAdapter(getActivity())); 
} 

getActivity()活動以來尚未創建,在這裏可以引起問題。此外,您沒有正確初始化適配器。你沒有提供所有的值。

1

像這樣重新調整您的代碼。

View view = inflater.inflate(R.layout.near_layout, container,false); 

    ListView listView = (ListView) view.findViewById(R.id.list_item); 
    listView.setAdapter(new CustomListNearAdapter(getActivity(), itemname, imgid)); 
    return view;