Hello i’ve build already smartphone apps but now i’m starting working on a project to make my app compatible with tablets. Now i’m using fragments this is my first using fragments so thats why i need your advice, please help or give me examples with my code. Many thanks already
my code my “news” class:
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class Nieuws extends FragmentActivity{
// Connection detector
//ConnectionDetector cd;
// Alert dialog manager
//AlertDialogManager alert = new AlertDialogManager();
// Progress Dialog
//private ProgressDialog pDialog;
private static String URL = "http://localhost/fetch.php?page=2&android";
// JSON Node namen
static final String TAG_DATA = "data";
static final String TAG_ID = "id";
static final String TAG_CATEGORY = "category";
static final String TAG_TITLE = "title";
static final String TAG_AUTHOR = "author";
static final String TAG_DATE = "date";
static final String TAG_INTRODUCTION = "introduction";
static final String TAG_CONTENT = "content";
static final String TAG_THUMBNAIL = "thumbnail";
static final String TAG_MEDIA = "media";
static final String TAG_SOURCE = "source";
static final String TAG_LINKEDMEDIA = "linkedMedia"; //array waarin de plaatjes zitten*/
// Nieuws JSONArray
JSONArray newsArray;
ListView list;
LazyAdapter adapter;
private PullToRefreshListView mPullRefreshListView;
ArrayList<HashMap<String, String>> newsList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nieuws);
//list=(ListView)findViewById(R.id.list);
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
/* FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
StartFragment myFragment = new StartFragment();
ft.add(R.id.myFragment, myFragment);
ft.commit();*/
/* cd = new ConnectionDetector(getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(Nieuws.this, "Internet Connectie Error", "Zorg voor een werkende internet connectie", false);
// stop executing code by return
return;
}*/
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// Do work to refresh the list here.
new GetJSONData().execute();
}
});
//Async
new GetJSONData().execute();
}
class GetJSONData extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>{
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute(){
super.onPreExecute();
//pDialog = new ProgressDialog(Nieuws.this);
//pDialog.setMessage("Nieuws laden ...");
//pDialog.setIndeterminate(false);
//pDialog.setCancelable(false);
//pDialog.show();
}
/**
* Get de json
*/
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {
// Hashmap voor listView
final ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
// Maak een JSON Parser instance
JSONParser jParser = new JSONParser();
// Pakt JSON string uit URL
JSONObject json = jParser.getJSONFromUrl(URL);
try{
// Pakt de Array van Nieuwsartikelen
newsArray = json.getJSONArray(TAG_DATA);
// Loop door alle Nieuwsartikels
for(int i=0; i < newsArray.length(); i++){
JSONObject c = newsArray.getJSONObject(i);
// Het plaatsen van elk json item in variabele
String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_CONTENT);
String date = c.getString(TAG_DATE);
//String introduction = c.getString(TAG_INTRODUCTION);
String thumbnail = c.getString(TAG_THUMBNAIL);
String linkedMedia = c.getString(TAG_LINKEDMEDIA);
//String thumbnailName = c.getString(TAG_THUMBNAIL);
//String thumbnailFormat = "http://iappministrator.com/mooiwark/media/%s";
//String thumbnail = String.format(thumbnailFormat, thumbnailName);
// maak een nieuwe HashMap
HashMap<String, String> map = new HashMap<String, String>();
// voeg elk item child node in de Hashmap -> value
map.put(TAG_TITLE, title);
map.put(TAG_CONTENT, content);
map.put(TAG_DATE, date);
//map.put(TAG_INTRODUCTION, introduction);
map.put(TAG_THUMBNAIL, thumbnail);
map.put(TAG_LINKEDMEDIA, linkedMedia);
// voeg de HashList toe aan ArrayList
newsList.add(map);
// Click event for single list row
mPullRefreshListView.setOnItemClickListener(new OnItemClickListener(){
//list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = newsList.get(position - 1);
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
//Intent in = new Intent(SingleMenuItemActivity.this, org.scout.android.library.LibraryDetail.class);
in.putExtra(TAG_TITLE, map.get(TAG_TITLE));
in.putExtra(TAG_CONTENT, map.get(TAG_CONTENT));
in.putExtra(TAG_DATE, map.get(TAG_DATE));
//in.putExtra(TAG_INTRODUCTION, map.get(TAG_INTRODUCTION));
in.putExtra(TAG_THUMBNAIL, map.get(TAG_THUMBNAIL));
in.putExtra(TAG_LINKEDMEDIA, map.get(TAG_LINKEDMEDIA));
startActivity(in);
}
});
}
} catch(JSONException e){
e.printStackTrace();
}
return newsList;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
//De items worden ingeladen
adapter=new LazyAdapter(Nieuws.this, result, R.layout.list_row,
new String[]{TAG_TITLE, TAG_CONTENT, TAG_DATE, TAG_THUMBNAIL}, new int[] {
R.id.title, R.id.subtitle, R.id.date, R.id.list_image}); //TAG_INTRODUCTION mist nog
//list.setAdapter(adapter);
mPullRefreshListView.setAdapter(adapter);
// dismiss the dialog after getting all deelnemers
//pDialog.dismiss();
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
}
in this activity it would normally load the content when on click. this class is the one which i’ve tried to turn into a fragments class
“SingleMenuItemActivity” class :
import android.app.Activity;
import android.content.Intent;
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.ImageView;
import android.widget.TextView;
public class SingleMenuItemActivity extends Fragment {
// JSON node keys
private static final String TAG_TITLE = "title";
private static final String TAG_CONTENT = "content";
private static final String TAG_DATE = "date";
private static final String TAG_LINKEDMEDIA = "linkedMedia";
//private static final String TAG_THUMBNAIL = "thumbnail";
// Connection detector
ConnectionDetector cd;
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
/*@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);*/
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.single_list_item, container, false);
return view;
cd = new ConnectionDetector(getActivity().getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(getActivity(), "Internet Connectie Error", "Zorg voor een werkende internet connectie", false);
// stop executing code by return
return;
}
// getting intent data
Intent in = getIntent().getExtras();
//final String image_url = in.getStringExtra(TAG_THUMBNAIL);
final String image_url = in.getStringExtra(TAG_LINKEDMEDIA);
ImageView imgv = (ImageView) getView().findViewById(R.id.images_label);
ImageLoader imageLoader = new ImageLoader(getActivity().getApplicationContext());
imageLoader.DisplayImage(image_url, imgv);
// Get JSON values from previous intent
String title = in.getStringExtra(TAG_TITLE);
String date = in.getStringExtra(TAG_DATE);
String message = in.getStringExtra(TAG_CONTENT);
//String images = in.getStringExtra(TAG_IMAGES);
//Bitmap bitmap = in.getParcelableExtra(TAG_IMAGES);
//ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
// Displaying all values on the screen
TextView lblTitle = (TextView)getView().findViewById(R.id.title_label);
TextView lblDate = (TextView) getView().findViewById(R.id.date_label);
TextView lblMessage = (TextView) getView().findViewById(R.id.message_label);
//ImageView lblImages = (ImageView) findViewById(R.id.images_label);
//TextView lblImages = (TextView) findViewbyId(R.id.images_label);
// loader image
//int loader = R.drawable.loader;
System.out.println("Ja en nu werkt het niet meer");
// image url
//String image_url = "http://d24w6bsrhbeh9d.cloudfront.net/photo/5614379_460s.jpg";
//ImageLoader imgLoader = new ImageLoader(getApplicationContext());
//-imgLoader.DisplayImage(song.get(CustomizedListView.TAG_IMAGES), lblImages);
System.out.println("Error? haha bam jammer dan:");
//imgLoader.DisplayImage(images, lblImages);
//System.out.println("Plaatjes?:"+ images);
lblTitle.setText(title);
lblDate.setText(date);
lblMessage.setText(message);
//lblImages.setImageURI(Uri.parse(images));
//ImageLoader imageLoader = new ImageLoader(getApplicationContext());
//imageLoader.DisplayImage(images,lblImages);
//lblImages.setImageResource(images);
//imageLoader.displayImage(images);
//lblImages.setImageBitmap(bitmap);
//lblImages.setImageResource(R.drawable.bitmap);
//lblImages.setImageResource(images);
/*if (d instanceof BitmapDrawable) {
Bitmap bm = ((BitmapDrawable)d).getBitmap();
//Maybe more code here?
lblImages.setImageBitmap(bm);
}*/
//lblImages.setImageResource(images);
//lblImages.DisplayImage(images);
//lblImages.DisplayImage(images);
}
}
Now i’m getting to the part which i don’t understand my Intent in = getIntent() doesn’t work i get errors. Can someone guide me on turning from code form a activity to a fragment many thanks already
Screenshot:

Left the “news class” right “SingleMenuItemActivity”
It’s going wrong when i click the onclick in the class on the left.
First of all i advise you to use a ListFragment for your list and define a listerner for onclick event (this listener could be your Nieuws activity).
Then you will need to have 2 layouts. The classic one with one fragment:
and the other one with two fragment:
in your Nieuws Activity you will check if the layout loaded has two fragments if not you will load the ListFragment using the fragment manager
Then in your on item selection method you need again to load the display fragment if you are in one pane mode:
There is more to explain but this is the basics. You may need to check these two usefull links:
http://developer.android.com/training/multiscreen/adaptui.html
http://developer.android.com/training/basics/fragments/fragment-ui.html
You will need also to handle screen rotation.