Android AsyncTask: Skipped X frames - too much work inside main thread - android

I am making some RSS reader for some website, so I wanted to implement actionbar and viewpager on lower versions of Androd that 4.0, so I am using ActionBarSherlock and ViewPagerIndicator from Jake Wharton, so I am working with fragments.
And I want to read some RSS feed from URL, and I have AsyncTask class for that called LoadXMLData, and here is code of that class.
LoadXMLData class:
public class LoadXMLData extends AsyncTask<String, RSSFeed, RSSFeed>{
private ProgressDialog mProgressDialog;
private Context context;
RSSFeed feed;
private String RSSFEEDURL = "http://balkanandroid.com/feed/";
public LoadXMLData(Context context) {
this.context = context;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Molimo Vas, sačekajte. Podaci se učitavaju.");
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
Log.d("OVDE SAM:", "onPreExecute()");
}
#Override
protected RSSFeed doInBackground(String... urls) {
// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(urls[0]);
Log.d("OVDE SAM:", "PARSIRAM XML");
return feed;
}
#Override
protected void onPostExecute(RSSFeed result) {
mProgressDialog.dismiss();
super.onPostExecute(result);
}
}
Also I have class MainActivity, which extends SherlockFragmentActivity.
public class MainActivity extends SherlockFragmentActivity {
BAFragmentAdapter mAdapter;
RSSFeed feed;
Application myApp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("OVDE SAM:", "MAIN ACTIVITY");
myApp = getApplication();
mAdapter = new BAFragmentAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(mAdapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
public RSSFeed getFeed() {
return feed;
}
public void setFeed(RSSFeed feed) {
this.feed = feed;
}
And the most imporatnt I have few fragment classes (LatestFragments, PhonesFragments, TabletFragments, ApplicationFragments and so on), and on each fragments I have almost same code as shown below.
Here is the full code of LatestFragment:
public class LatestFragment extends Fragment {
GridView lv;
RSSFeed feed;
CustomListAdapter adapter;
private String RSSFEEDURL = "http://balkanandroid.com/feed/";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_najnovije, container,
false);
AsyncTask<String, RSSFeed, RSSFeed> xml = new LoadXMLData(getActivity())
.execute(RSSFEEDURL);
// AsyncTask<String, RSSFeed, RSSFeed> load = new
// LoadXMLData(getActivity()).execute(RSSFEEDURL);
try {
feed = xml.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lv = (GridView) view.findViewById(R.id.GridView1);
// Set an Adapter to the ListView
adapter = new CustomListAdapter();
lv.setAdapter(adapter);
// Set on item click listener to the ListView
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// actions to be performed when a list item clicked
int pos = arg2;
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtras(bundle);
intent.putExtra("pos", pos);
startActivity(intent);
}
});
return view;
}
#Override
public void onDestroy() {
super.onDestroy();
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
class CustomListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
public CustomListAdapter() {
layoutInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(getActivity().getApplicationContext());
}
public int getCount() {
// TODO Auto-generated method stub
// Set the total list item count
return feed.getItemCount();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);
}
// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
TextView tvDate = (TextView) listItem.findViewById(R.id.tvDate);
// Set the views in the layout
imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
tvTitle.setText(feed.getItem(pos).getTitle());
tvDate.setText(feed.getItem(pos).getDate());
return listItem;
}
}
}
As you can see I use AsyncTask class LoadXMLData to read data in doInBackground() method, and then I call that AsyncTask class under all fragment classes with this code, because I need result of RSSFeed, because I need that data to show it to user.
AsyncTask<String, RSSFeed, RSSFeed> xml = new LoadXMLData(getActivity())
.execute(RSSFEEDURL);
try {
feed = xml.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And it works, but when I click to open other view from view pager its slow, almost freezes my app, and I get this message with Logcat.
07-10 00:22:40.598: I/Choreographer(623): Skipped 316 frames! The application may be doing too much work on its main thread.

That is because you are calling get() on your AsyncTask, which blocks. Do not do this.
Instead, use your feed in onPostExecute() of the AsyncTask.

Related

How to Implement on scroll load ListView in Android?

I have implemented listview in fragment. In that listview i have used custom adapter to display images with their description. Data is coming from web server. There are many list items with image so I'd like to implement on scroll load ListView for the same.
Which approach should i follow to do the same? I have tried to google it and found some sample but it does not fit into my requirement.
Here is the code of my Fragment containing listview. Thanks in advance.
FragmentTab1.class
public class FragmentTab1 extends Fragment implements
AdapterView.OnItemClickListener {
DisplayImageOptions options;
protected ImageLoader imageLoader = ImageLoader.getInstance();
JSONArray AdsArray = null;
String TAG_IMAGE = "image";
String TAG_DISCRIPTION = "description";
ListView lv;
final Context context = getActivity();
static ArrayList<HashMap<String, String>> adList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab1, container,false);
options = new DisplayImageOptions.Builder().showStubImage(R.drawable.no_image).cacheInMemory().cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565).build();
lv = (ListView) rootView.findViewById(R.id.listView1);
lv.setOnItemClickListener(this);
adList = new ArrayList<HashMap<String, String>>();
new LoadMyAds().execute();
return rootView;
}
class LoadMyAds extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
UserFunctions userFunction = new UserFunctions();
String type = "all";
JSONObject json = userFunction.myAds(userid, type);
try {
if (json.getString("status") != null) {
String res = json.getString("status");
if (Integer.parseInt(res) == 1) {
adList.clear();
AdsArray = json.getJSONArray("myadslistsArray");
for (int i = 0; i < AdsArray.length(); i++) {
JSONObject c = AdsArray.getJSONObject(i);
String description = c.getString(TAG_DISCRIPTION);
String image = c.getString(TAG_IMAGE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_IMAGE, image);
map.put(TAG_DISCRIPTION, description); adList.add(0, map);
}
} else {
Toast.makeText(getActivity().getApplicationContext(),
"parsing error", Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
// image parameter need to be added
lv.setAdapter(new CustomAdapter(adList));
}
}
class CustomAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> locallist;
public CustomAdapter(ArrayList<HashMap<String, String>> list) {
// TODO Auto-generated constructor stub
locallist = list;
imageLoader.init(ImageLoaderConfiguration
.createDefault(getActivity()));
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return locallist.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
// return null;
return getItem(getCount() - position - 1);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
if (row == null) {
row = getActivity().getLayoutInflater().inflate(
R.layout.ad_list_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.desc= (TextView) row.findViewById(R.id.title);
viewHolder.img = (ImageView) row.findViewById(R.id.thumbImage);
row.setTag(viewHolder);
}
final ViewHolder holder = (ViewHolder) row.getTag();
holder.desc.setText(""+locallist.get(position).get(TAG_DISCRIPTION));
imageLoader.displayImage(locallist.get(position).get(TAG_IMAGE),
holder.img, options);
return row;
}
}
class ViewHolder {
protected TextView desc;
protected ImageView img;
}
}
Attach scroll listener to the listView
lv.setOnScrollListener(new EndlessScrollListener());
Here is the class implementation:
class EndlessScrollListener implements OnScrollListener {
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
new LoadMyAds().execute();
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}

Getting AsyncTask result data from Fragment

I have AsyncTask class called LoadXMLData, and as you can see I parse XML data in doInBackground() method.
public class LoadXMLData extends AsyncTask<String, RSSFeed, RSSFeed>{
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
private Context context;
RSSFeed feed;
public LoadXMLData(Context context) {
this.context = context;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Molimo Vas, sačekajte. Podaci se učitavaju.");
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
Log.d("OVDE SAM:", "onPreExecute()");
}
#Override
protected RSSFeed doInBackground(String... urls) {
// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(urls[0]);
Log.d("OVDE SAM:", "PARSIRAM XML");
return feed;
}
#Override
protected void onPostExecute(RSSFeed result) {
mProgressDialog.dismiss();
super.onPostExecute(result);
}
}
And I have few fragments, where I need to get data from that AsyncTask. How I could do that?
Here is the code of an fragment called NajnovijeFragment.
public class NajnovijeFragment extends Fragment{
GridView lv;
RSSFeed feed;
CustomListAdapter adapter;
private String RSSFEEDURL = "http://balkanandroid.com/feed/";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_najnovije, container,
false);
lv = (GridView) view.findViewById(R.id.GridView1);
// Set an Adapter to the ListView
adapter = new CustomListAdapter();
lv.setAdapter(adapter);
// Set on item click listener to the ListView
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// actions to be performed when a list item clicked
int pos = arg2;
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtras(bundle);
intent.putExtra("pos", pos);
startActivity(intent);
}
});
return view;
}
#Override
public void onDestroy() {
super.onDestroy();
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
class CustomListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
public CustomListAdapter() {
layoutInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(getActivity().getApplicationContext());
}
public int getCount() {
// TODO Auto-generated method stub
// Set the total list item count
return feed.getItemCount();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);
}
// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
TextView tvDate = (TextView) listItem.findViewById(R.id.tvDate);
// Set the views in the layout
imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
tvTitle.setText(feed.getItem(pos).getTitle());
tvDate.setText(feed.getItem(pos).getDate());
return listItem;
}
}
}
The easiest way to get data from an ASyncTask is by implementing a callback.
Create an Interface:
public interface OnXMLLoadFinishedListener {
public void onXMLDataReady(RSSFeed results);
}
In you LoadXMLData:
private OnXMLLoadFinishedListener listener;
public void setOnXMLLoadFinishedListener(OnXMLLoadFinishedListener listener){
this.listener = listener;
}
#Override
protected void onPostExecute(RSSFeed result) {
super.onPostExecute(result);
listener.onXMLDataReady(RSSFeed results);
}
In your Fragment:
public class NajnovijeFragment extends Fragment implements OnXMLLoadFinishedListener{
and override onXMLDataReady:
#override
public void onXMLDataReady(RSSFeed results){
//display your data.
}
Make sure that when you create your AsyncTask instance you set the listener otherwise this will not work:
LoadXMLData xmlLoader = new LoadXMLData();
xmlLoader.setOnXMLLoadFinishedListener(this);
Your AsyncTask already knows context, so you could call back into your activity (called ActivityMain for illustrative purposes) in onPostExecute. e.g.
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
((ActivityMain) context).loadCompleteHandler(param1,param2,...)
}
It's then up to you how you want to implement loadCompleteHandler in your activity. Now your activity might not exist, so you must be careful to cancel the AsyncTask when the activity is removed. Fragments belonging to an activity can also access the activity.
AsyncTask is a Class that is very related to the UI, if you need to update the UI with this XML parsing you should take this consideration:
Make the asynctask an inner class in your fragment or
Pass the fragment to your asynctask
Update the fragment's view in onPostExecute()
In any case you should check if your activity is null, if so... avoid updating views, something like that:
onPostExecute(Object xml) {
if(getActivity != null) {
// update Views like...
textViewLabel.setText(parsedXml.getTitle);
}
}
I would suggest you to use SafeAsyncTask, which is a java class from the Roboguice Project, only one file, and it is related to java.util.concurrent.Callable, just copy and paste the source:
SafeAsyncTask.java
How to use it!

problems updating a listview from xml

i have a listview builded from xml file by an lazyadapter (baseadpter)
for example: urlxml --> parser ---> baseadapter ---> listview
when i modified xml file on server i would like to refresh listview and see rows changed in it. By the way im using lib "pull to refresh".
When i pull to refresh i got new rows togheter old rows.
Do i have to delete listview content first and repeat this:
urlxml --> parser ---> baseadapter ---> listview
what is wrong?
EDIT woth some code
my adapter
public class LazyAdapter extends BaseAdapter {
private Activity activity;
ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>> ();
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<ArrayList<String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.get(0).size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.elenco_articoli_items, null);
String Tt = data.get(0).get(position);
String URl = data.get(1).get(position);
TextView text=(TextView)vi.findViewById(R.id.title);;
ImageView image=(ImageView)vi.findViewById(R.id.image);
text.setText(Html.fromHtml(Tt));
imageLoader.DisplayImage(URl, image,1);
return vi;
}
}
my main activity
public class Lista_articoli_categoria extends Activity {
ArrayList<String> images = new ArrayList<String> ();
ArrayList<String> title = new ArrayList<String> ();
ArrayList<String> author = new ArrayList<String> ();
ArrayList<String> description = new ArrayList<String> ();
private LazyAdapter adapter;
private RefreshableListView mListView;
Parser task = new Parser();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.elenco_articoli);
try {
task.execute();
final RefreshableListView list = (RefreshableListView) findViewById(R.id.refreshablelistview);
adapter= new LazyAdapter(this, fill_data(task.get()));
mListView = list;
list.setAdapter(adapter);
list.setOnUpdateTask(new OnUpdateTask() {
public void updateBackground() {
refresh_data();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void updateUI() {
adapter.notifyDataSetChanged();
}
public void onUpdateStart() {
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*
TODO
*/
}
});
}
#Override
public void onDestroy()
{
mListView.setAdapter(null);
super.onDestroy();
}
public ArrayList<ArrayList<String>> fill_data(ArrayList<Struttura_rss> Datidaxml){
ArrayList<ArrayList<String>> rec = new ArrayList<ArrayList<String>> ();
for(int i=0;i<Datidaxml.size();i++){
Struttura_rss p = Datidaxml.get(i);
title.add(p.getTitle());
images.add(p.getImage());
description.add(p.getArticolo());
author.add(p.getAuthor());
}
rec.add (title);
rec.add (images);
rec.add (description);
rec.add (author);
return rec;
}
public void refresh_data(){
try {
Parser task = new Parser();
task.execute();
adapter= new LazyAdapter(this, fill_data(task.get()));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
If you look at the example, you should understand easily...
Link: https://github.com/chrisbanes/Android-PullToRefresh/blob/master/sample/src/com/handmark/pulltorefresh/samples/PullToRefreshListActivity.java
On the refreshn you have to download only the new items (by exemple by sending a timestamp or an id to the webservice)
#Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
try {
//Go to webservice
} catch (InterruptedException e) {
}
return mStrings;
}
And then, parse only the new items and add them here:
#Override
protected void onPostExecute(String[] result) {
mListItems.addFirst("Added after refresh...");
mAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
EDIT: if you cannot download only the needed items (RSS feed) download evrything and add the correct items in onPostExecute.
if(!isALreadyInTheList(currentItem))
mListItems.addFirst(currentItem);
else
doNothing();

Listview swaps text inside of EditText when kaypad is invisible or visible

Problem : in Listview swaps text inside of EditText. it happen when keyboard invisible or visible. as shown in below images.
Code : ListView Activity Class :
public class DayPlannerFormActivity extends Activity {
private TextView txtHeader;
private Context mContext;
private ListView lvDayplannerFrom;
private FormDayPlannerAdapter adapter;
private Activity activity;
final Handler mHandler = new Handler();
private Vector<DayPlannerForm> list = new Vector<DayPlannerForm>();
final Runnable mUpdateResults = new Runnable() {
public void run() {
updateResultsInUi();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dayplanner);
mContext = this;
activity = this;
txtHeader = (TextView) findViewById(R.id.txtHeader);
txtHeader.setText(R.string.haivlate);
lvDayplannerFrom = (ListView) findViewById(R.id.lvDayplanner);
startfetchOperation();
}
private void updateResultsInUi() {
adapter= new FormDayPlannerAdapter(activity,list);
lvDayplannerFrom.setAdapter(adapter);
}
protected void startfetchOperation() {
Thread t = new Thread() {
#Override
public void run() {
getData();
}
};
t.start();
}
private void getData() {
try{
list.clear();
DayPlannerForm dpf = new DayPlannerForm("Task Name 1","","");
list.add(dpf);
dpf =new DayPlannerForm("Task Name 2","","");
list.add(dpf);
mHandler.post(mUpdateResults);
} catch (Exception e){
mHandler.post(mUpdateResults);
}
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
try{
if(lvDayplannerFrom != null)
lvDayplannerFrom.setAdapter(null);
} catch (Exception e){}
}
}
Code : List View Adapter Class
public class FormDayPlannerAdapter extends BaseAdapter {
private Activity mActivity;
private static Vector<DayPlannerForm> list;
private static LayoutInflater inflater;
private Context mContext;
public FormDayPlannerAdapter ( Activity _activity,Vector<DayPlannerForm> _list) {
mActivity = _activity;
mContext = _activity;
list = _list;
inflater = (LayoutInflater)mActivity.getSystemService(mActivity.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder{
public TextView txtTaskName;
public CheckBox chbAction;
public EditText edtDecription;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.dayplanner_listitem_form, null);
holder=new ViewHolder();
holder.txtTaskName=(TextView)vi.findViewById(R.id.txtTaskName);
holder.chbAction = (CheckBox) vi.findViewById(R.id.chbAction);
holder.edtDecription = (EditText) vi.findViewById(R.id.edtDecription);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.txtTaskName.setText(list.get(position).getTaskName());
return vi;
}
}
How to resolve this problem
Problem solve bye Adding android:windowSoftInputMode="adjustPan" this attribute in activity tag in the manifest.xml
On first look i think that problem occurring by the the following code:-
DayPlannerForm dpf = new DayPlannerForm("Task Name 1","","");
list.add(dpf);
dpf =new DayPlannerForm("Task Name 2","","");
list.add(dpf);
You should take different objects of DayPlannerForm Class for each Task Name.
DayPlannerForm dpf1,dpf2;
dpf1 = new DayPlannerForm("Task Name 1","","");
list.add(dpf1);
dpf2 =new DayPlannerForm("Task Name 2","","");
list.add(dpf2);
I think this might solve your problem.

Using viewpager in my application

I want to use viewpager in my application.I'm tried to do this everyday in one month but i can't achieve the solution.I want to create pages with same listview concept but different datas.Here is my code:
public final class TestFragment extends ListFragment {
private static final String KEY_CONTENT = "TestFragment:Content";
ArrayList <HashMap<String, Object>> imageliste = new ArrayList<HashMap<String, Object>>();
public class MyCustomAdapter extends ArrayAdapter<HashMap<String, Object>> {
//Bitmap bm;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String,Object>> imageliste) {
super(context, textViewResourceId,imageliste);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
View row = convertView;
if(row==null){
LayoutInflater inflater=LayoutInflater.from(getActivity());
row=inflater.inflate(R.layout.list, parent, false);
}
TextView label=(TextView)row.findViewById(R.id.text1);
label.setText((CharSequence) imageliste.get(position).get("Baslik"));
TextView label2=(TextView)row.findViewById(R.id.text2);
int boyut =imageliste.get(position).get("Desc").toString().length();
label2.setText((CharSequence) imageliste.get(position).get("Desc").toString().substring(0, (boyut/3)*2)+"...");
ImageView icon=(ImageView)row.findViewById(R.id.img);
icon.setImageDrawable((Drawable) imageliste.get(position).get("Resim"));
return row;
}
}
public String getURLContent(String url)
{
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
ResponseHandler<String> resHandler = new BasicResponseHandler();
String page = httpClient.execute(httpGet, resHandler);
return page;
} catch (ClientProtocolException e) {
return "";
} catch (IOException e) {
return "";
}
}
public ArrayList<HashMap<String, Object>> getImageLinks(String strng){
ArrayList<HashMap<String, Object>> myBooks2 = new ArrayList<HashMap<String, Object>>();
String html = getURLContent(strng);
Document doc = Jsoup.parse(html);
Elements divs = doc.getElementsByClass("postBox");
for (Element div : divs) {
Element masthead = div.select("img[src].attachment-post-thumbnail").first();
String linkHref = masthead.attr("src");
Element masthead2 = div.select("h1").first().select("a").first();
String baslik = masthead2.text();
Element masthead3 = div.select("div.textPreview").first().select("p").first();
String desc = masthead3.text();
//Drawable drawable = LoadImageFromWebOperations();
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("Resim", LoadImageFromWebOperations(linkHref));
hm.put("Baslik", baslik);
hm.put("Desc", desc);
myBooks2.add(hm);
}
return myBooks2;
}
private Drawable LoadImageFromWebOperations(String url){
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
public class backgroundLoadListView extends AsyncTask<String, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(getActivity());
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
// adapter = new MyCustomAdapter( getActivity().getApplicationContext(), R.layout.list, imageliste);
//adapter.notifyDataSetChanged();
dialog.dismiss();
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog.setMessage("Yükleniyor...");
dialog.show();
}
#Override
protected Void doInBackground(String... arg) {
// TODO Auto-generated method stub
imageliste=getImageLinks(arg[0]);
return null;
}
}
public class backgroundLoadListView2 extends AsyncTask<Void, Void, Void> {
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.customslidingtabhost, null);
ListView listView1=(ListView)view.findViewById(R.id.list);
//MyCustomAdapter adapter = new MyCustomAdapter(getActivity(), R.layout.list, imageliste);
//listView1.setAdapter(adapter);
int[] colors = {0xFFFFFFFF, 0xFF87CEEB, 0xFFFFFFFF}; // red for the example
listView1.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
listView1.setDividerHeight(2);
listView1.setBackgroundColor(Color.WHITE);
((PullToRefreshListView) listView1).onRefreshComplete();
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
imageliste=getImageLinks("http://www.teknoinfo.net/kategoriler/haberler/teknoloji-haberleri");
return null;
}
}
public static TestFragment newInstance(String content) {
TestFragment fragment = new TestFragment();
return fragment;
}
private String mContent = "???";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
//new backgroundLoadListView().execute("http://www.teknoinfo.net/haberler");
MyCustomAdapter adapter = new MyCustomAdapter( getActivity().getApplicationContext(), R.layout.list, imageliste);
View view = inflater.inflate(R.layout.customslidingtabhost, null);
final ListView v=(ListView)view.findViewById(R.id.list);
/*((PullToRefreshListView) v).setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
new backgroundLoadListView2().execute();
}
});*/
int[] colors = {0xFFFFFFFF, 0xFF87CEEB, 0xFFFFFFFF}; // red for the example
v.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
v.setDividerHeight(2);
v.setBackgroundColor(Color.WHITE);
v.setAdapter(adapter);
((PullToRefreshListView) v).setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
//new backgroundLoadListView2().execute();
// new backgroundLoadListView().execute("http://www.teknoinfo.net/haberler");
}
});
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}
It is hard to work through all that code you posted (most of which has not to do with your question), but as far as I can tell you have not even set up a ViewPager from your code or it is hidden in some XML file that you didn't post.
What you need to do is create a ViewPager instance. For example in your fragment's XML layout file, like:
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
Now, in your Fragment you create an adapter that specifies the pages that you want (similarly to how a ListAdapter specifies items to a ListView). For that, create a class (you can do that inline in the Fragment code) that inherits from PagerAdapter. For example, something like:
private class MyPagerAdapter extends PagerAdapter implements TitleProvider {
private ListView pagerListView1;
private ListView pagerListView2;
public MyPagerAdapter() {
LayoutInflater inflater = getActivity().getLayoutInflater();
pagerListView1 = (ListView) inflater.inflate(R.layout.fragment_pagerlist, null);
pagerListView2 = (ListView) inflater.inflate(R.layout.fragment_pagerlist, null);
}
#Override
public int getCount() {
return 2;
}
#Override
public Object instantiateItem(View container, int position) {
switch (position) {
case 0:
((ViewPager) container).addView(pagerListView1, 0);
return pagerListView1;
case 1:
((ViewPager) container).addView(pagerListView2, 0);
return pagerListView2;
}
return null;
}
#Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == (View) object;
}
#Override
public void finishUpdate(View container) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View container) {
}
#Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
}
Note that I hard-coded 2 pages/lists in this pager. You can fill those as you would with any other ListView. The layout of the pages is just a simple ListView that is inflated from the fragment_pagerlist XML file, which looks something like:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#color/BackgroundLight" />
Finally, you bind the ViewPager's adapter somewhere in your onActivityCreated method:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewPager pager = (ViewPager) getView().findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter());
}
Note that this does not yet give you a ViewPagerIndicator. Check Jake Wharton's excellent library for that.
If you want the code to a fully implemented and working version, with multiple lists (and other views) in a ViewPager and a ViewPagerIndicator, take a look at the open source RateBeer for Android project; specifically http://code.google.com/p/ratebeerforandroid/source/browse/RateBeerForAndroid/src/com/ratebeer/android/gui/fragments/SearchFragment.java#486 for a real-world PagerAdapter.

Categories

Resources