ListView doesn't show progressbar when the data loaded - android

When I start activity with ListView it start to load some information from the Internet and after apply adapter, but during loading ListVIew doesn't show me a ProgressBar ring. When I trying to use SherlockListActivity instead ListView I have same problem.
<?xml version="1.0" encoding="utf-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/comments"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
public class CommentsActivity extends SherlockActivity{
#InjectView(R.id.comments) ListView mCommentsListView;
private String mPostId;
private CommentsAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comment_activivty);
ButterKnife.inject(this);
Intent intent = getIntent();
mPostId = intent.getStringExtra("POST_ID");
LC lc = new LC();
lc.execute(mPostId);
}
public void setupAdapter(){
mAdapter = new CommentsAdapter(this);
mCommentsListView.setAdapter(mAdapter);
}
private class LC extends LoadComments{
#Override
protected void onPostExecute(Void result) {
//setup adapter after loading comments
setupAdapter();
}
}
}

Add ProgressBar to your layout file:
<ProgressBar
android:id="#+id/progress_bar"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Inject the ProgressBar in your activity:
#InjectView(R.id.progress_bar) ProgressBar mProgressBar;
Now you have to hide it when the adapter is set:
public void setupAdapter(){
mAdapter = new CommentsAdapter(this);
mCommentsListView.setAdapter(mAdapter);
mProgressBar.setVisibility(View.GONE);
}

Do it like this,
#Override
protected Void OnPreExecute(){
ProgressDialog prgDialog = new ProgressDialog(getContext());
prgDialog.setTitle("Loading");
prgDialog.setMessage("please wait")
prgDialog.setCancelable(false);
prgDialog.show();
}
on post Execute
private class LC extends LoadComments{
#Override
protected void onPostExecute(Void result) {
//setup adapter after loading comments
prgDialog.dismiss();
setupAdapter();
}
}

Related

android layout not displayed

I have trouble displaying the layout of my main activity :
I create an ActivityA with an ImageView.
In onCreate(), I launch an AsyncTask, which retrieves content from Internet, and opens an ActivityB.
When I launch my application, it displays ActivityB right away.
public class ActivityA extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTask mytask = new MyTask();
mytask.execute();
}
}
My MainActivity xml file is the following :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/loadingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_home_page" />
<ProgressBar
android:id="#+id/marker_progress"
style="?android:attr/progressBarStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical|center_horizontal"
android:indeterminate="true" />
</RelativeLayout>
The ProgressBar is used to show the loading process of the AsyncTask.
thanks for helping
public class ActivityA extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTask mytask = new MyTask();
mytask.execute();
}
}
private class MyTask extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
// code for retrieve contents from Internet
}
#Override
protected void onPostExecute(String result) {
// cancel ProgressBar and start activity B here like
Intent i = new Intent(A.this, B.class);
startActivity(i);
}
#Override
protected void onPreExecute() {
// showing ProgressBar
}
}
don't forgot to mention A activity as a launcher.
I hope it will help you to solve this problem.

How to Handle Error inflating class?

I added a new Progress bar lib to my project. I added it through the maven. Then i used the progress bar in my layout file. While running the App it shows this Error.
Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.github.castorflex.android.circularprogressbar.CircularProgressBar
This is the Library i am using https://github.com/castorflex/SmoothProgressBar.
My layout File
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame">
<ListView
android:id="#+id/feed_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.github.castorflex.android.circularprogressbar.CircularProgressBar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:id="#+id/progressBar"
app:cpb_color="#FFee44"
app:cpb_colors="#array/colors"
app:cpb_rotation_speed="1.0"
app:cpb_sweep_speed="1.0"
app:cpb_stroke_width="4dp"
app:cpb_min_sweep_angle="10"
app:cpb_max_sweep_angle="300"
/>
</FrameLayout>
My Class
public class VideoActivity extends Activity {
private static final String TAG = "Mine";
private static final int REQ_START_STANDALONE_PLAYER = 1;
private static final int REQ_RESOLVE_SERVICE_MISSING = 2;
public static final String DEVELOPER_KEY = "AIzaSyDcnoqJGI1872s";
public ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
public String mvideoid;
public String mstatus;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_list);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
listView = (ListView) findViewById(R.id.feed_list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
// making fresh volley request and getting json
GsonRequest<FeedResult> gsonRequest = new GsonRequest<FeedResult>(URL_FEED, FeedResult.class,
new Response.Listener<FeedResult>() {
#Override
public void onResponse(FeedResult response) {
feedItems = response.getFeedItems();
listAdapter.setData(feedItems);
listAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.INVISIBLE);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addRequest(gsonRequest, TAG);
getid();
}
}
Try to replace with this :
xmlns:app="http://schemas.android.com/apk/res/com.android.demo"
this value (com.android.demo) replace with your package name.
if you are using any custom component refer it with the same name i.e.
import com.github.castorflex.android.circularprogressbar.CircularProgressBar;
public class VideoActivity extends Activity {
CircularProgressBar progressBar;
.
.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_list);
progressBar = (CircularProgressBar) findViewById(R.id.progressBar);
.
.
.
you are using the default one which isn't present in xml

Why ProgressBar not gone?

This is my xml file:
<ProgressBar
android:id="#+id/progress_index"
style="#android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"/>
<ListView
android:id="#+id/listview_index"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideInset">
</ListView>
I try to mProgress.setVisibility(View.GONE) after mListView.setAdapter() ,but when I check the "Show layout bounds" in the "Developer options".The layout not actually gone.As the picture shows,the ProgressBar is still in the center of the FrameLayout.Why?
Java code:
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mProgress = (ProgressBar) view.findViewById(R.id.progress_index);
mProgress.setVisibility(View.VISIBLE);
mAdapter = new CustomAdapter(getActivity());
new MyTask().execute();
lv.setAdapter(mAdapter);
}
public class MyTask extends AsyncTask<Integer, Void, ArrayList<NewsItem>> {
protected ArrayList<NewsItem> doInBackground(Integer... params) {
// some execution....
}
protected void onPostExecute(ArrayList<NewsItem> result) {
mAdapter.notifyDataSetChanged();
mProgress.setVisibility(View.GONE);
mListView.setVisibility(View.VISIBLE);
}
}
can you try this:
ProgressDialog mProgress = new ProgressDialog(activity.this);
...........
mProgress.dismiss();

NullpointerException in onPreExcute of AsyncTask for ProgressBar

I had a NullPointerException when I try to set ProgessBar to visible in onPreExecute AsyncTask (mProgressBar.setVisibility(View.VISIBLE);). I don't know what went wrong! Thanks you all!
<ProgressBar
android:id="#+id/pb_featured_game_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
public class MainActivity extends Activity {
...
private ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
new FetchGamesTask().execute();
private class FetchGamesTask extends
AsyncTask<Integer, Integer, List<GameInfo>> {
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressBar = (ProgressBar)findViewById(R.id.pb_featured_game_progress);
mProgressBar.setVisibility(View.VISIBLE);
}
Before you call the execute on your FetchGamesTask, you should associate the main view with the xml file.
Like this
protected void onCreate(Bundle savedInstanceState) {
// the OS will inflate the main_activity.xml
// file and use it for this activity
setContentView(R.layout.main_activity);
new FetchGamesTask().execute();

Fragment list not getting update

I've got a FragmentActivity in which I fire an AsyncTask that gathers a list of data. This activity has a ListFragment and I want to update the listview as the data keeps coming.
I have another fragment that should also get the data, so I implemented some quick version of observer pattern, so that they both get notified when an element has been added to the list:
private void addElement(MyListElement item) {
myList.add(item);
Collections.sort(myList, Collections.reverseOrder());
notifyObservers();
}
private void notifyObservers() {
for(IListObserver dObserver: observers){
dObserver.updateList(myList);
}
}
But my list is never updated (or the other fragment, for the matter).
Here is the ListFragment code:
public class MyListFragment extends SherlockListFragment implements IListObserver{
private TextView first;
private Context mContext;
private List<MyListElement> myList;
private MyListAdapter myListAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.mContext = getActivity().getApplicationContext();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_listview, container, false);
myList = new ArrayList<SizeDirectory>(0);
myListAdapter = new MyListAdapter(getActivity(), myList);
setListAdapter(myListAdapter);
myListAdapter.notifyDataSetChanged();
return view;
}
#Override
public void updateList(List<MyListElement> myList) {
this.myList = myList;
this.myListAdapter.notifyDataSetChanged();
getListView().requestLayout();
}
}
And this is the AsyncTask:
class ListLoader extends AsyncTask<String, MyListElement, Boolean> {
private String LOG_TAG = ListLoader .class.getSimpleName();
/**
* Maybe show loading indicator
*/
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Boolean doInBackground(String... args) {
MyListElement item = getListItem(...)
publishProgress(item );
return true;
}
/**
* After completing background task Dismiss the progress dialog
**/
protected void onPostExecute(Boolean result) {
if (result) {
//Everything was ok
}
}
#Override
protected void onProgressUpdate(MyListElement... values) {
super.onProgressUpdate(values);
addElement(values[0]);
}
}
The funny thing is, if I put this AsyncTask in my ListFragment, and call updateList from there, then I do get the list updated as the asynctask progresses.
I though maybe this was due to the activity Lifecycle calling the onCreateView of my Fragment AFTER creating activity (and thus resetting the list) but I checked with debugger and it's not the case. I am also assuming this is not related to not being able to update UI from another thread since I do it from onProgressUpdate plus I would've gotten an Exception (or wouldn't I?).
EDIT: layout_listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/firstTextView"
android:layout_width="fill_parent"
android:layout_height="129dp"
android:scrollbars="vertical"
android:text="#string/hello_world" />
<Button
android:id="#+itd/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button" />
</RelativeLayout>
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
After the updateList() call, your list adapter is still holding a reference to the original value of myList . Simply setting MyListFragment.myList won't update the adapter. You need a way of setting MyListAdapter's copy of the list. If this is one of the stock Android adapters you may need to create a new adapter for the new list and set it on the list fragment.

Categories

Resources