如何在CustomAdapter中使用Intent方法到Project_Fragment

时间:2016-06-25 07:36:43

标签: android android-fragments android-listfragment

我有CustomAdapter,如何使用intent方法转到Project_Fragment 我有两个片段一个PodCasts_Fragment创建listview和customAdapter另一个Project_Fragment从服务器获取数据 如何使用CustomAdapter中的Intent方法? 这是适配器

public class CustomAdapter extends BaseAdapter {
Context context;
String [] results;
int [] imageId,imageId_2;

private static LayoutInflater inflater = null;
public CustomAdapter(Context c){
    this.context=c;
}

public CustomAdapter(FragmentActivity podCasts_fragment, String[] s, int[] images, int[] images2){
    results = s;
    imageId = images;
    context = podCasts_fragment;
    imageId_2 = images2;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    return results.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}
public class Holder
{
    TextView textView;
    ImageView imageView1,imageView2;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Holder holder = new Holder();
    convertView = inflater.inflate(R.layout.program_list, null);
    holder.textView = (TextView)convertView.findViewById(R.id.textView1);
    holder.imageView1 = (ImageView)convertView.findViewById(R.id.imageView1);
    holder.imageView2 = (ImageView)convertView.findViewById(R.id.imageView2);
    holder.textView.setText(results[position]);
    holder.imageView1.setImageResource(imageId[position]);
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                Intent intent = new Intent(context.getApplicationContext(), Projects_Fragment.class);
                context.getApplicationContext().startActivity(intent);
                Toast.makeText(context, "You Clicked " + results[position], Toast.LENGTH_LONG).show();
            }
    });
    return convertView;
}

}

这是Project_Fragment

public class Projects_Fragment extends Fragment {
RecyclerView recyclerView;
ProjectAdapter projectAdapter;
List<Project> ProjectList = new ArrayList<>();


public Projects_Fragment() {
    // Required empty public constructor
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_projects_, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.Project_recycler_view);

    recyclerView.setHasFixedSize(true);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    recyclerView.setAdapter(projectAdapter);
    projectAdapter = new ProjectAdapter(ProjectList);
    try {
        // Inflate the layout for this fragment
        JSONObject jsonObject = new JSONObject();



        new ProjectTask() {
            @Override
            protected void onPostExecute(ProjectResponse projectResponse) {
                if (projectResponse != null && projectResponse.data != null && projectResponse.data.size()>0) {
                    ProjectList = projectResponse.data;
                    //apiResponse.data.toArray();
                    //newAdapter.notifyDataSetChanged();

                    projectAdapter = new ProjectAdapter(ProjectList);
                    recyclerView.setAdapter(projectAdapter);
                }
                Log.i("Pro_resp", "onPostExecute");
            }
        }.execute(jsonObject);
    }catch (NullPointerException e){
        Log.e("search Project_Fragment",e.toString());
    }
    return rootView;
}

}

这是Podcast_Fragment

public class PodCasts_Fragment extends Fragment {
PodCasts_Fragment context;
ListView listView;
public static int[] images = {
        R.drawable.projects,
        R.drawable.event,
        R.drawable.social,
        R.drawable.gallery,
        R.drawable.shop,
        R.drawable.share};
public static int[] images2 = {
        R.drawable.nextbutton
};

public PodCasts_Fragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View rootView =  inflater.inflate(R.layout.fragment_pod_casts_, container, false);

    listView = (ListView)rootView.findViewById(R.id.listView);

    String s[] = {"Projects","Events","Social Links","Gallery","Shop","Share our App"};

    listView.setAdapter(new CustomAdapter(getActivity(),s,images,images2));

 return rootView;

}

}

0 个答案:

没有答案