getApplicationContext在片段中

时间:2016-01-21 21:50:51

标签: java android android-fragments

我想在我的片段课程中获得我的MainActivity的Context但是我不知道到底是什么,这是我到目前为止所尝试的内容 -

我有一个customAdapter类 -

 public class CustomAdapter extends BaseAdapter {
ArrayList<String> result;
String [] resultArray;
Context context;
ArrayList<String>  imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, ArrayList<String>  prgmNameList, ArrayList<String>  prgmImages) {
    // TODO Auto-generated constructor stub
    result=prgmNameList;
    context=mainActivity;
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView noteTitle;
    TextView noteId;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.notes_list, null);
    holder.noteTitle=(TextView) rowView.findViewById(R.id.txtViewTitle);
    holder.noteId=(TextView) rowView.findViewById(R.id.txtViewOID);

    final int index = result.indexOf(position);
    int index2 = imageId.indexOf(position);

   // holder.noteTitle.setText(result[index]);
    holder.noteTitle.setText(result.get(index));
    holder.noteId.setText(imageId.get(index2));
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked " + result.get(index), Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}

}

并有一个Fragment类,它扩展了Fragment -

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


    List_View = (ListView) view.findViewById(R.id.notesList);


    context =  getActivity().getApplicationContext();



    List_View.setAdapter(new CustomAdapter( here am having Problem  ,notesArray , notesIDArray));

根据我的CustomAdapter类你可以看到我必须将Context作为参数但不知道如何获得我的MainAcivity的上下文

List_View.setAdapter(new CustomAdapter( here am having Problem ,notesArray , notesIDArray));

我已经尝试了getActivitygetActivity().getApplicationContext();,但我不认为这是他们想要获得mainActivity的任何指导方针 我的Fragment类中的上下文对我很有帮助

1 个答案:

答案 0 :(得分:2)

您似乎实际上不需要MainActivity所以更改:

public CustomAdapter(MainActivity mainActivity, ArrayList<String>  prgmNameList, ArrayList<String>  prgmImages)

要:

public CustomAdapter(Context mainActivity, ArrayList<String>  prgmNameList, ArrayList<String>  prgmImages)

并在getActivity()

onCreateView中传递Fragment