Saturday, 17 August 2013

how to keep parsing list in the tabs after moving from one to other

how to keep parsing list in the tabs after moving from one to other

hi all in my application i want to use the tabs with fragment , in the
main activity i have created the tabs and give the view to the fragments
,, now in the first fragment i want to read the feed from a page from the
face book and i should view it in a custom list view this code was
implemented and worked correctly ,, the application run good but it must
work as the following instead of it's run ,, when we click on tab 2 an the
return to tab 1 the information must be exists not to load them again from
the internet , but my application doesn't do this it reload the
information again how can i do this please ,, is it applicable to use the
onAttach() fun ?? , if yeah how can i use it in my code This is my code :
package com.androidbegin.fragmenttabs;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import classes.act2;
import work.connectivity;
import parsing.parsingFeedData;
import classes.feedCustomListView;
import classes.feedAttributes;
import android.R.integer;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
@SuppressLint({ "NewApi", "ValidFragment" })
public class FragmentsTab1 extends Fragment implements
ActionBar.TabListener, View.OnClickListener {
public static ListView listView;
public static ImageButton previous;
public static ImageButton next;
public static Button update;
String pageID;
public static ArrayList<feedAttributes> feedArrayList=new
ArrayList<feedAttributes>();
public static ArrayList<feedAttributes>
feedArrayListToCheckUpdates=new ArrayList<feedAttributes>();
public static ArrayList<feedAttributes>
feedArrayListAfterMove=new ArrayList<feedAttributes>();
public static ProgressDialog p ;
public static ArrayAdapter<feedAttributes>arrayadapter ;
public static String nextFeed=null;
public static String previousFeed=null;
// public static AlertDialog.Builder alertDialog ;
public static ProgressDialog progressDialog ;
String s1;
String s2;
String accessToken;
String URL;
private Fragment mFragment;
ArrayList< String > ar;
ArrayAdapter< String > ad ;
ListView lv ;
static int pos;
Context _c;
@SuppressLint("ValidFragment")
public FragmentsTab1(Object context) {
// TODO Auto-generated constructor stub
_c=(Context) context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
// @TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1,
container, false);
////////////
Log.d("start", "ok");
// pageID="aljazeera";
pageID="Andalusn";
/**
problem: android.os.NetworkOnMainThreadException.
There are Two way to fix this Issue:
1.Instead of writing network call in Main UI Thread
use Async Task.
2.Add below code into your activity class inside
onCreate() method just after the
setContentView(R.layout.activity_main).
if( Build.VERSION.SDK_INT >= 9){
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
*/
// if( Build.VERSION.SDK_INT >= 9){
// StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
// StrictMode.setThreadPolicy(policy);
// }
Log.d("before_def", "ok");
listView=(ListView)rootView.findViewById(R.id.listView1);
previous=(ImageButton)rootView.findViewById(R.id.imageButton2);
next=(ImageButton)rootView.findViewById(R.id.imageButton1);
update=(Button)rootView.findViewById(R.id.button1);
previous.setVisibility(View.GONE);
next.setVisibility(View.GONE);
update.setVisibility(View.GONE);
Log.d("after_def", "ok");
previous.setOnClickListener( this);
next.setOnClickListener( this);
update.setOnClickListener( this);
//for feed query
s1="https://graph.facebook.com/";
s2="/posts?&access_token=";
accessToken="CAACEdEose0cBAB0sWi01yZBypSjTn4ySQ6F6R8pDTwcwdTngZCVdu0PdxcheEAl4HKzZBerUCMjXdD7S0BzxGpRmseyV5jk6ajBZBB45dK8HZBK5LrsF0ygJVaGOilEJ4HfRKwojfhV5v8vyq3mNu6ZAsQHMYTZBJ4ZD";
/*****************************************/
// alertDialog = new Builder(this);
// alertDialog.setTitle("Warnning" ) ;
// alertDialog.setMessage("You cant to connect with net " );
//
//
// alertDialog.setNegativeButton("Not connect", null);
// alertDialog.setPositiveButton("Ok", new
DialogInterface.OnClickListener() {
//
// public void onClick(DialogInterface arg0, int arg1) {
// new
conn().execute("https://gdata.youtube.com/feeds/api/videos?q=google&v=2&alt=json&max-results=3");
//
//
// }
// });
/*****************************************/
p = new ProgressDialog(getActivity());
p.setMessage("Wait Loading .... ");
p.setCancelable(false);
Log.d("before_con", "ok");
//'''''''''"""""
boolean connection;
connectivity internetConnection;
internetConnection=new connectivity();
//need to add uses permission/access_network state (^_*)
connection=internetConnection.hasConnection(getActivity());
//
connection=internetConnection.isNetworkConnected(getApplicationContext());
//''''''''"""""
Log.d("after_con", "ok");
if(connection==true)
{
Log.d("if_con_t", "ok");
int flag;
flag=-1;
Log.d("inside if","in if ,flag="+flag+"!!!!!!!!!!!");
URL=s1+pageID+s2+accessToken;
new asyncTask().execute(URL);
for(int i=0;i<feedArrayList.size();i++)
{
Log.d("in
main:feedID","feedID:"+feedArrayList.get(i).getFeedID()+"################");
Log.d("in main:feedID","profile
picture:"+feedArrayList.get(i).getProfilePicture()+"################");
}
//''''''''"""""
//''''''''"""""
listView.setAdapter(new
feedCustomListView(getActivity(), feedArrayList));
}//end if Internet connection=true
else
{ Log.d("no internet connection","no internet
connection!!!!!!!!!!!");
//''''''''"""""
//''''''''"""""
listView.setAdapter(new
feedCustomListView(getActivity(), feedArrayList));
Log.d("feedArrayList.size()",String.valueOf(feedArrayList.size()));
previous.setVisibility(View.VISIBLE);
next.setVisibility(View.VISIBLE);
update.setVisibility(View.VISIBLE);
}
/*****************************************/
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View
vie, int position,
long id) {
// TODO Auto-generated method stub
feedAttributes feed=(feedAttributes)
adapter.getItemAtPosition(position);
// Intent i2=new Intent(getActivity(), act2.class);
// i2.putExtra("feed", feed);
// startActivity(i2);
//
Fragment newFragment = new new_frag();
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
// Replace whatever is in the
fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(android.R.id.content,
newFragment);
// transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
return rootView;
}//end onCreate method
@Override
public void onClick(View argument)
{//start onClick method
// TODO Auto-generated method stub
if(argument==previous)
{
URL=previousFeed;
new asyncTask().execute(URL);
// if(feedArrayListAfterMove.size()==0)
// {
// Toast.makeText(getActivity(), "no previous
feeds ", Toast.LENGTH_SHORT).show();
// }
// else
// {
//
// Toast.makeText(getActivity(), "previous feeds
", Toast.LENGTH_SHORT).show();
// listView.setAdapter(new
feedCustomListView(getActivity(), feedArrayListAfterMove));
// }
//
}//end previous argument
/*****************************************/
if(argument==next)
{
URL=nextFeed;
new asyncTask().execute(URL);
// if(feedArrayListAfterMove.size()==0)
// {
// Toast.makeText(getActivity(), "no next feeds
", Toast.LENGTH_SHORT).show();
// }
// else
// {
//
//
// for(int i=0;i<feedArrayList.size();i++)
// {
// Log.d("in
main:feedID","feedID:"+feedArrayList.get(i).getFeedID()+"################");
// Log.d("in main:feedID","profile
picture:"+feedArrayList.get(i).getProfilePicture()+"################");
// }
//
//
// /*****/
// Toast.makeText(getActivity(), "next feeds ",
Toast.LENGTH_SHORT).show();
//
//
//// listView.setAdapter(new
feedCustomListView(getActivity(), feedArrayListAfterMove));
//
// }
}
/*****************************************/
if(argument==update)
{
URL=s1+pageID+s2+accessToken+"&limit=1";
new asyncTask().execute(URL);
//
if(feedArrayListToCheckUpdates.get(0).getFeedID().equals(feedArrayList.get(0).getFeedID()))
// {
// Log.i("updats", "no updates!!!!!!!!!!!!!!!!");
// Toast.makeText(getActivity(), "no updates",
Toast.LENGTH_SHORT).show();
// }
// else
// {
// Log.i("updats", "new updates!!!!!!!!!!!!!!!!");
// Toast.makeText(getActivity(), "new updates",
Toast.LENGTH_SHORT).show();
// }
}
/*****************************************/
}//end onClick method
//end MainActivity class
/////////////////////////////////////////////////////////////
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
mFragment = new FragmentsTab1(_c);
// Attach fragment1.xml layout
ft.add(android.R.id.content, mFragment);
ft.attach(mFragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
// Remove fragment1.xml layout
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public class asyncTask extends AsyncTask<String, integer,
ArrayList<feedAttributes>>
{//start of asyncTaskForPage class
@Override
protected void onPreExecute() {
// p.show();
super.onPreExecute();
}
@Override
protected ArrayList<feedAttributes> doInBackground(String...
params) {
// TODO Auto-generated method stub
ArrayList<feedAttributes> result=new
ArrayList<feedAttributes>();
parsingFeedData parsingFeed=new parsingFeedData();
result=parsingFeed.parsingFeedDataMethod(params[0]);
return result;
}//end of doInBackground
@Override
protected void onPostExecute(ArrayList<feedAttributes>
result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// p.dismiss();
if(result.size()!=0)
{
listView.setAdapter(new
feedCustomListView(getActivity(), result));
previous.setVisibility(View.VISIBLE);
next.setVisibility(View.VISIBLE);
update.setVisibility(View.VISIBLE);
}
}
}//end asyncTaskForFeed class
}//end of frag.

No comments:

Post a Comment