Displaying a snippet when clicking an item in the list

I have a list in a fragment class that receives data from JSON. So far I have managed to show the data in the list. (data here refers to NEWS items). I want to display a detailed news page when a specific news is viewed in the list.

Ex: Listview contains only news title and image. Clicking on this list should display a detailed version of this news segment. How can i do this?

This is the slice class that the Listview has.

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();
               }
              });



            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();
        }

    }


}

      

In the code, you will notice that when the element is clicked, it moves to the NewsDetailFragment. And this is the class I want to write now.

PS: my JSON already contains all the details including title, images, description.

UPDATE :: This is my NewsDetailFragment class. It shows all the news at once, not the one I clicked.

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.ListAdapter;
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 NewsDetailFragment extends Fragment {

    private GridView gridView;
    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);


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


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



            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();
        }

    }


}

      

+3


source to share


2 answers


You can use the following

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

      

EDIT



FragmentTransaction is used to perform actions such as adding or removing a fragment. If you replace / modify an existing layout, we perform such a transaction to replace fragments. Bundles are the most convenient tool you need to save changes to data when you change configuration or transfer data from one activity to another, or to transfer data between fragments. Fragments read this.

For link1 and link2 and docs . The links are very easy to understand.

Hope the editing helps.

+4


source


I did it...



@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.frag_for_accsummary,container, false);
       final FragmentTransaction ft=    getFragmentManager().beginTransaction();


      //geting resources
    Resources res=getActivity().getResources();
    title=res.getStringArray(R.array.title);
        // TODO Auto-generated method stub

    ListView l=(ListView)v.findViewById(R.id.list);
    MyAadapter m=new MyAadapter(getActivity(),R.layout.singlerow, title, icon);//custom adapter
    //title--> array of text to be shown on list    icon-->  array of images to be shown on list     R.layout.singlerow--> tells how single row should appear in list
    l.setAdapter(m);

    l.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub
            Log.i("in seton","setonclick");

            switch(position){

            case 0: 
                AccSummary frag0=new AccSummary();

            bun.putString("uname",uname);
            bun.putString("sessid",sessid);
            bun.putString("custid",custid1);
            frag0.setArguments(bun);
            ft.replace(R.id.lin2,frag0);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.addToBackStack(null);
            ft.commit();
            Log.i("FragFor", uname);

                break;

    //-------------------------------------------------------------------       

            case 1:
                      FromAcc acc=new FromAcc();
                      bun.putString("Name",uname); 
                      bun.putString("sessid",sessid);// key - value pair
                      bun.putString("custid",custid1);
                      acc.setArguments(bun);
                      ft.replace(R.id.lin2, acc);
                      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                        ft.addToBackStack(null);
                        ft.commit();


            break;
            case 2:break;
            case 3:break;
            case 4:break;
            case 5:ChangepassFrag cf=new ChangepassFrag();
                  bun.putString("uname",uname);
                  bun.putString("sessid",sessid);
                  bun.putString("custid",custid);
                  bun.putString("password",pass);
                 cf.setArguments(bun);
                    ft.replace(R.id.lin2,cf);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.addToBackStack(null);
                    ft.commit();
                    break;

            case 6:   break;
            case 7:break;
            case 8:break;
            case 9:
                   Fragment1 f=new Fragment1();
                   bun.putString("uname",uname);
                    bun.putString("sessid",sessid);
                //  bun.putString("custid",custid1);
                    f.setArguments(bun);
                        ft.replace(R.id.lin2,f);
                        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                        ft.addToBackStack(null);
                        ft.commit();
                break;
            case 10:break;
            }




            }





    });





    return v;
}

      

0


source







All Articles