2017-04-15 86 views
0

我想在一個嵌套的片段中,將作爲元素圖像與其下面的標題保持一個列表視圖,雖然我不斷收到我的自定義適配器我創建填充列表視圖。這裏是代碼:如何實現自定義適配器嵌套的片段

的自定義適配器:

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

import java.util.ArrayList; 
import java.util.List; 

public class ImageAdapter extends ArrayAdapter<Image> { 

    ArrayList<Image> imageList; 
    private Context mContext; 
    int resource; 

    public ImageAdapter(Context context, int resource, ArrayList<Image> imageObjects) { 
     super(context, resource, imageObjects); 
     this.imageList = imageObjects; 
     this.mContext = context; 
     this.resource = resource; 
    } 

    public ArrayList<Image> getImageList() { 
     return imageList; 
    } 

    public void setImageList(ArrayList<Image> imageList) { 
     this.imageList = imageList; 
    } 

    public Context getmContext() { 
     return mContext; 
    } 

    public void setmContext(Context mContext) { 
     this.mContext = mContext; 
    } 

    public int getResource() { 
     return resource; 
    } 

    public void setResource(int resource) { 
     this.resource = resource; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 

     if(convertView==null){ 
      LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
      convertView = layoutInflater.inflate(R.layout.fragment_membership, null, true); 

     } 

     Image image = getItem(position); 

     ImageView layoutImage = (ImageView)convertView.findViewById(R.id.membershipimage); 
     layoutImage.setImageResource(image.getImageResourceID()); 
     TextView layoutText = (TextView)convertView.findViewById(R.id.membershipCaption); 
     layoutText.setText(image.getImageCaption()); 

     return super.getView(position, convertView, parent); 
    } 
} 

我用的片段,另一個片段中:

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.GridView; 
import android.widget.ListView; 

import java.util.ArrayList; 

public class MembershipFragment extends Fragment { 

    ArrayList<Image> imageList; 
    ListView membershipListView; 

    public static MembershipFragment newInstance() { 
     MembershipFragment fragment = new MembershipFragment(); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_membership, container, false); 

     return view; 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     imageList = new ArrayList<>(); 
     for(int i=0; i<3; i++){ 
      Image image = new Image(R.drawable.cinnamon_apple_breakfast, "Test Caption"); 
      imageList.add(image); 
     } 
     membershipListView = (ListView)view.findViewById(R.id.membershipListView); 
     try{ 
      ImageAdapter adapter = new ImageAdapter(getContext(), R.layout.list_view_item, imageList); 
      membershipListView.setAdapter(adapter); 
     } 
     catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 

該應用程序保持在啓動時發生崩潰,在自定義適配器拋出空指針異常在getView方法中,當我嘗試將圖像資源ID設置爲imageview。哪裏有問題?

+0

爲什麼會出現在setAdapter一個嘗試捕捉? –

回答

0

我覺得你很混亂你的佈局。您可以在這裏使用getActivity

ImageAdapter adapter = new ImageAdapter(getContext(), R.layout.list_view_item 

您在那裏傳遞了該佈局資源。但在適配器內部,您正在使用不同的佈局。

@Override 
public View getView(int position, View convertView, ViewGroup parent){ 

    if(convertView==null){ 
     LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.fragment_membership, null, true); 

    } 

你犯了一個getResource方法,是有原因的,我承擔?你永遠不會說它。

我不認爲你希望你的片段佈局持有片段佈局的列表(因爲遞歸)

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_membership, container, false); 

    return view; 
} 
+0

getresource方法是我需要進一步實現和工作,一旦我得到這個工作。我需要使用嵌套片段,因爲我有一個底部導航欄,其中一個片段還包含一個包含更多片段的Tablayout。我已將適配器中的佈局更改爲正確的佈局,但仍然出現錯誤; ArrayAdapter需要資源ID是TextView – Igor

+0

我不確定你在哪裏得到這個錯誤,但是我知道如果你搜索它,這個消息的答案就存在。而且我沒有對Tab佈局嵌套的Fragments進行評論。我所指出的是'R.layout.list_view_item'從未被使用,因此你的適配器找不到你的ImageView和TextView –

相關問題