单击ListView项目打开一个新视图

时间:2014-08-14 09:28:37

标签: android json listview android-listview

我有一个列表视图,显示来自JSON的数据。我想单击一个特定的列表视图项,当单击该项时,它应显示已单击的列表视图项的整个视图。

Ex:列表视图中的新闻列表--->点击任何新闻---->该新闻的详细版本

我创建的那个无法正常工作。当我点击列表视图中的新闻项时,它会显示所有新闻的详细版本。我该如何解决这个问题。

所以这是我的NewsFragment类。它有listview

package com.fortuna.cinemalk;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.content.Intent;
import android.widget.AdapterView;



import com.fortuna.cinemalk.adapter.LazyAdapter;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.service.CommonVariable;
import com.fortuna.cinemalk.service.JSONServices;
import com.fortuna.cinemalk.util.Element;

public class NewsFramgment extends Fragment {

    private GridView gridView;
    private ListView listView;


    private ArrayList<BaseElement> News;
    private LazyAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;

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

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

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        //gridView = (GridView) view.findViewById(R.id.gridView2);
        listView = (ListView) view.findViewById(R.id.list);


        listView.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView<?> parent, View v,
                 int position, long id)

               {


              android.support.v4.app.Fragment detail = new NewsDetailFragment();
              android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction().add(R.id.content_frame, detail).addToBackStack("back").commit();

                /*   FragmentTransaction fragmentTransaction = getFragmentManager()
                            .beginTransaction();
            Fragment profileFragment = new NewsDetailFragment();//the fragment you want to show
            Bundle bundle = null;
            profileFragment.setArguments(bundle);
            fragmentTransaction
                .add(R.id.content_frame, profileFragment);//R.id.content_frame is the layout you want to replace
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();*/

               }
              });



            new BackGround().execute();

        return view;
    }


public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            News = JSONServices.getNewsDescription();
            return null;
        } 



        @Override
        /* check again */
        protected void onPostExecute(Void result) {

            commonVariable.setNewsDescription(News);

            adapter = new LazyAdapter(News, activity,Element.NEWS_LIST.getType());

            listView.setAdapter(adapter);

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

    }


}

这是我的NewsDetailFragment,这是整个新闻视图的片段。

package com.fortuna.cinemalk;

import java.util.ArrayList;
import android.app.Activity;
import android.os.AsyncTask;
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.ListAdapter;
import android.widget.AdapterView;



import com.fortuna.cinemalk.adapter.LazyAdapter;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.service.CommonVariable;
import com.fortuna.cinemalk.service.JSONServices;
import com.fortuna.cinemalk.util.Element;

public class NewsDetailFragment extends Fragment {


    private View view1;

    private ArrayList<BaseElement> newsdetail;
    private LazyAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;

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

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

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        view1 = (View) view.findViewById(R.id.list);



        new BackGround().execute();

        return view;
    }


public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            newsdetail = JSONServices.getNewsDescription();
            return null;
        } 



        @Override
        /* check again */
        protected void onPostExecute(Void result) {

            commonVariable.setTheater(newsdetail);

            adapter = new LazyAdapter(newsdetail, activity,Element.NEWS_DETAIL.getType());

            ((AdapterView<ListAdapter>) view1).setAdapter(adapter);

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

    }


}

1 个答案:

答案 0 :(得分:1)

你应该像我在这里展示的那样传递新闻:

listView.setOnItemClickListener(new OnItemClickListener() {
           public void onItemClick(AdapterView<?> parent, View v,
             int position, long id)

           {


          android.support.v4.app.Fragment detail = NewsDetailFragment.newInstance(id);
          android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
          fragmentManager.beginTransaction().add(R.id.content_frame,       
          detail).addToBackStack("back").commit();
         ........


 public class NewsDetailFragment extends Fragment {

  public static NewsDetailFragment newInstance (int id){

   NewsDetailFragment  frag=new NewsDetailFragment ();
   Bundle bundle=new Bundle();
   bundle.putInt("id",id);
   frag.setArguments(bundle);
   return frag;
 }


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

   ........

   new BackGround().execute(getArguments.getInt("id"));

 }

  .......

  public class BackGround extends AsyncTask<Void, Void, Integer> {

    @Override
    protected String doInBackground(Integer... params) {
       int id=params[0];
        newsdetail = JSONServices.getNewsDescription(id);
        return newsdetail;
    } 

   protected void onPostExecute(String newsdetail) {

        commonVariable.setTheater(newsdetail);
        adapter = new LazyAdapter(newsdetail, activity,Element.NEWS_DETAIL.getType());
        ((AdapterView<ListAdapter>) view1).setAdapter(adapter);
        super.onPostExecute(result);
    }