I have an activity class that calls my custom adapter class which extends a base adapter. Inside my getView method I have a button.setOnClickListener within which there is a thread which downloads data. I want to set the button text to "downloading" when the user clicks, then after the download is finished, set the button text to "finished". How can I do this?
public class MyActivity extends Activity {
//some code here
private void setAdapter(ArrayList arrayList) {
listView.setAdapter(new UserAdapter(context,arrayList));
}
}
public class UserAdapter extends BaseAdapter {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
holder.button = (Button) view.findViewById(R.id.button);
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Thread(new Runnable() {
#Override
public void run() {
try{
//set button text to "downloading"
//establish an http connection and download data
//after download done, if successfull set button text to downloaded
//if download failed, set button text to failed.
} catch (Exception exception) {
}
}
}
).start();
}
});
return view;
}
private class Holder {
private Button button;
}
}
In order to update a view fron inside the thread use runOnUithread inside the getView method like this. Inside the activity you can use directly but outside the activity you have to use the context.
context.runOnUiThread(new Runnable(){
public void run() {
//If there are stories, add them to the table
try{
}
} catch (final Exception ex) {
Log.i("---","Exception in thread");
}
}
});
For http request you can use asyncTask.
Related
i'm using backbutton as interface from activity but it's not working properly for me because on backpress showing 0 size of arraylist
// here is the activity class from where i'm getting backbutton interface..
public class Multiple_Images extends AppCompatActivity {
#Override
public void onBackPressed() {
if(twice ==true){
Intent intent =new Intent(this,MainActivity.class);
startActivity(intent);
}ImageAdapter imageAdapter =new ImageAdapter(this);
imageAdapter.onBackPress();
Toast.makeText(this, "Press twice", Toast.LENGTH_SHORT).show();
twice =true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
twice =false; } }, 2000); }}
//here is the adapter class here i'm using backbutton
public class ImageAdapter extends BaseAdapter implements onBackPressListener {
ArrayList<String> selectedArraylist ;
#Override
public boolean onBackPress() {
selectedArraylist.clear();
Toast.makeText(context, "All values unselected", Toast.LENGTH_SHORT).show();
return true;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
urimodel=new ArrayList<>();
final ImageView imageGrid ;
Activity activity = (Activity) context;
actionMode = activity.startActionMode(new Actionmode());
final GridModel gridModel=(GridModel) this.getItem(i);
if(view==null) {
view = LayoutInflater.from(context).inflate(R.layout.model, null);
selectedArraylist =new ArrayList<>();
}
final CardView cardView= (CardView)view.findViewById(R.id.cardview_image);
imageGrid = (ImageView) view.findViewById(R.id.grid_image);
// gridText = (TextView) view.findViewById(R.id.grid_text);
imageGrid.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageGrid.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.get().load(gridModel.getImage()).resize(200,200).into(imageGrid);
if (selectedArraylist.contains(gridModel.getImage_text())) {
cardView.setCardBackgroundColor(CARD_SELECTED_COLOR);
}else {
cardView.setCardBackgroundColor(Color.WHITE);
}
return view;
}
}
Simply you can do this inside onBackPressed
#Override
public void onBackPressed() {
if (twice == true) {
super.onBackPressed(); //this backs to the previous activity, if you want to stay with Intent, add finish() after startActivity()
return;
} else {
for (int i = 0; i < list.size(); i++) {
if (gridView.isItemChecked(i)) {
gridView.setItemChecked(i, false);
}
}
//selectedArraylist.clear(); this is clearing your array of selected items
}
twice = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
twice = false;
}
}, 2000);
}
I don't know, why did you put selectedArraylist =new ArrayList<>(); in adapter getView() method. getView() is fired every time, when a new list item is inflated, that mean every time, when you are changing adapters source, scrolling list this method is called, and every time you are initialize you array, and all data inside lost. You should treat an adapter class just like a tool for displaying items, and all actions like above make outside adapter.
pretty much easy,
I give you my own project code, hope it help you.
StudentFragment.java:
private void MultiSelected_Student(int position) {
Student data = adapter_class.getItem(position);
if (data != null) {
if (selectedIds.contains(data)) selectedIds.remove(data);
else selectedIds.add(data);
}
}
private void Remove_MultiSelected() {
try {
selectedIds.clear();
} catch (Exception e) {
e.printStackTrace();
}
}
public void Group_UnSelect() {
Remove_MultiSelected();
MultiSelected = false;
fab.setVisibility(View.GONE);
homeeActivity.studentsMultiSelect = false;
notifyy();
}
private void notifyy() {
adapter_class.notifyDataSetChanged();
}
HomeActivity.java:
public boolean studentsMultiSelect = false;
#Override
public void onBackPressed() {
if (studentsMultiSelect) {
studentFragment.Group_UnSelect();
} else {
super.onBackPressed();
}
}
I'm building a chat application, so I'm using two ListViews: one that shows the online friends and one for the chat itself, that receives the messages and so on. I'm using the XMPP protocol and the Smack Library for Android.
The Smack Library give me Listeners which are activated every time a friend status changes(online/offline) and the other one when the user receives a message. Here's how I declare the adapter and call an AsyncTask when the user press a button:
peopleList = (ListView) findViewById(R.id.peopleList);
adapter = new MyAdapter(this, people);
peopleList.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConnectAndLoad(MainActivity.this).execute();
}
});
Inside the AsyncTask, I connect to the server inside the doInBackground method and inside the onPostExecute I create the listener which adds the user to the array list of the listview and call adapter.notifyDataSetChanged();
public class ConnectAndLoad extends AsyncTask<String, Integer, Boolean> {
private ProgressDialog dialog;
public ConnectAndLoad(Activity activity)
{
this.dialog = new ProgressDialog(activity);
this.dialog.setTitle("Loading..");
this.dialog.setMessage("Connecting to the server..");
dialog.show();
}
#Override
protected Boolean doInBackground(String... arg0) {
MyConnectionManager.getInstance().setConnectionConfiguration(getApplicationContext());
MyConnectionManager.getInstance().connect();
MyConnectionManager.getInstance().login();
return true;
}
protected void onPostExecute(Boolean boo)
{
MyConnectionManager.getInstance().bored();
Roster roster = Roster.getInstanceFor(MyConnectionManager.getInstance().getConnection());
try
{
if (!roster.isLoaded()) roster.reloadAndWait();
}
catch (Exception e)
{
Log.e(TAG, "reload");
}
roster.addRosterListener(new RosterListener() {
public void entriesDeleted(Collection<String> addresses) {
}
public void entriesUpdated(Collection<String> addresses) {
}
public void entriesAdded(Collection<String> addresses) {
}
#Override
public void presenceChanged(Presence presence) {
people.add(new People(presence.getFrom(), presence.getStatus()));
adapter.notifyDataSetChanged();
}
});
dialog.dismiss();
}
}
And below is my Custom Adapter:
public class PeopleAdapter extends ArrayAdapter<People> {
private ArrayList<People> events_list = new ArrayList<>();
Context context;
public PeopleAdapter(Context context, ArrayList<People> users) {
super(context, 0, users);
this.context = context;
this.events_list = users;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
People user = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.people_list, parent, false);
}
TextView tvName = (TextView) convertView.findViewById(R.id.name);
TextView tvStatus = (TextView) convertView.findViewById(R.id.status);
tvName.setText(user.name);
tvStatus.setText(user.status);
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + events_list.get(position).name, Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, ConversationActivity.class);
i.putExtra("user", events_list.get(position).name);
context.startActivity(i);
}
});
return convertView;
}
}
I mean what I want to do I think it's a simple thing, every single chat app does it, is basically update the list view automatically but I'm having two problems:
The listview ONLY updates after I click on it. So it basically works
but I have to click on the listview..
I receive this error every time the list view updates (the app keeps working though):
Exception in packet listener: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
I can give you a simple solution. Make local Activity variable in the ConnectAndLoad class
private Activity activity;
public ConnectAndLoad(Activity activity)
{
...
activity.activity= activity;
}
Instead on directly calling adapter.notifyDataSetChanged(); use
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
adapter.notifyDataSetChanged();
}
});
It seems like presenceChanged() called in another thread. But be careful and make sure you delete RosterListener when activity gets destroyed or it can lead to the memory leaks i.e activity is already destroyed but you keep getting notifications about presence change.
I have deveolped a method to call methods from another calss.I have created an adapter and inside that I have an override getView(). What I need to do is to get values from a listview and display as a report. I have tried this exact code inside another activity in the same project. It works perfectly. But when ever I moved this into another activity it doesn't give any error.But the overrided method is not calling. What should I do to make it run?
public class ViewList extends ActionBarActivity {
ArrayAdapter<Units> unitsAdapterView;
ListView ViewAll;
List<Units> Unit = new ArrayList<Units>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_list);
ViewAll = (ListView) findViewById(R.id.viewList);
populateListView();
viewView();
}
private void viewView(){
Button m=(Button)findViewById(R.id.tryBtn);
m.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// startActivity(new Intent(getApplicationContext(),ViewList.class));
populateListView();
}
});
}
private void populateListView(){
unitsAdapterView =new UnitListAdaptorView();
ViewAll.setAdapter(unitsAdapterView);
// unitsAdapterView.getView();
}
private class UnitListAdaptorView extends ArrayAdapter<Units> {
public UnitListAdaptorView() {
super (ViewList.this, R.layout.list,Unit);
Log.i("","A");
// A();
}
#override
public View getView(int position, View view, ViewGroup parent) {
Log.i("","Resue connceted bulb 5");
if (view == null) {
view = getLayoutInflater().inflate(R.layout.list,parent ,false);
}
Units currentUnit = Unit.get(position);
TextView name = (TextView)view.findViewById(R.id.lblViewName);
name.setText(currentUnit.getName());
TextView bulb=(TextView) view.findViewById(R.id.lblViewBulb);
bulb.setText(currentUnit.getBulbNo());
TextView fan=(TextView) view.findViewById(R.id.lblViewFan);
fan.setText(currentUnit.getFanNo());
return view;
}
}
}
The problem was solved after adding the codes to get all the existing data from database. After adding the below codes to the onCreate method the function is working now!
DatabaseHandler dbHandler = new DatabaseHandler(getApplicationContext());
if (dbHandler.getUnitsCount() != 0)
Unit.addAll(dbHandler.getAllUnit());
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131296513, class xyz.ScrollDetectableListView) with Adapter(class android.widget.HeaderViewListAdapter)]
I am getting above exception sometimes while scrolling through the dynamic listview and then clicking on item.I researched a lot but unable to find the exact reason that why i am getting this error sometimes and how it can be resolved?
private ScrollDetectableListView mFListView;
public FAdapter mFAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_feed_view, container, false);
View headerView = getActivity().getLayoutInflater().inflate(R.layout.view_feed_header, null);
try{
mFListView = (ScrollDetectableListView) rootView.findViewById(R.id.feed_list_view);
mFContainer = (SwipeRefreshLayout) rootView.findViewById(R.id.feed_container);
mFListView.addHeaderView(headerView);
mFListView.setEmptyView(rootView.findViewById(R.id.empty_view));
mFContainer.setColorSchemeResources(R.color.green, R.color.pink, R.color.fbcolor,
R.color.instagramcolor, R.color.googlecolor, R.color.flickrcolor);
mFView = getActivity().getLayoutInflater().inflate(R.layout.view_footer, null);
ImageView rotateImageView = (ImageView) mFooterView.findViewById(R.id.spinner);
Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate);
rotation.setFillAfter(false);
rotateImageView.startAnimation(rotation);
mFContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh()
{
initializeFListView();
}
});
initializeFListView();
mProgressDialog.setVisibility(View.VISIBLE);
mHActivity.setDataChangedListener(new DataChangedListener() {
#Override
public void onDataChanged() {
mFContainer.setRefreshing(true);
mProgressDialog.setVisibility(View.GONE);
initializeFListView();
}
});
}catch(Exception e){}
return rootView;
}
public void initializeFListView()
{
FApi.getTrending(getActivity(), xyz, new APIResponseListener() {
#Override
public void onResponse(Object response) {
setFeedAdapter((List<Video>) response);
}
#Override
public void onError(VolleyError error) {
if (error instanceof NoConnectionError) {
String errormsg = getResources().getString(R.string.no_internet_error_msg);
Toast.makeText(getActivity(), errormsg, Toast.LENGTH_LONG).show();
}
}
});
}
private void setFAdapter(List<Video> response)
{try {
List<Video> videos = response;
mFAdapter = new FAdapter(getActivity(), videos, mProfileClickListener, mCommentClickListener);
mFListView.setOnScrollListener(new EndlessScrollListenerFeedView(getActivity(), mFListView, mFView, mFAdapter, videos, mFType, ""));
mFListView.setAdapter(mFAdapter);
mProgressDialog.setVisibility(View.GONE);
if (mFContainer.isRefreshing()) {
mFContainer.setRefreshing(false);
}
if (mFAdapter.getCount() < mCount) {
mFView.setVisibility(View.GONE);
mFListView.removeFooterView(mFooterView);
}
}catch(Exception e){}
}
}
My suggestion try to set ur list adapter on UI Thread,,,
private void setFAdapter(List<Video> response)
{
try {
List<Video> videos = response;
mFAdapter = new FAdapter(getActivity(), videos, mProfileClickListener, mCommentClickListener);
mFListView.setOnScrollListener(new EndlessScrollListenerFeedView(getActivity(), mFListView, mFView, mFAdapter, videos, mFType, ""));
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
mFListView.setAdapter(mFAdapter);
}
});
mProgressDialog.setVisibility(View.GONE);
if (mFContainer.isRefreshing()) {
mFContainer.setRefreshing(false);
}
if (mFAdapter.getCount() < mCount) {
mFView.setVisibility(View.GONE);
mFListView.removeFooterView(mFooterView);
}
}catch(Exception e){}
}
}
Keep one singleton class object in hand. So that you can synchronize two thread on it. Care to be taken to not to block the ui thread.
Reduce number of interfaces to only one method to start preparing data for your list and only one method to call your notifydatasetchanged/setAdapter on list.
Means there should be only one method like prepareData() which will be executed by a background thread. synchronise this method on your singleton object.
MyListAdaper adapter = null;
// Call this from a background thread
public void prepareData() {
synchronized (SingleTonProvider.getInstance()) {
List<AnyDataTypeYouWant> data = null;
// populate data here by your application logic.
adapter = new MyListAdaper(data);
}
}
And have only one method to refresh list.
// Also Call this from a background thread only
public void refreshList() {
synchronized (SingleTonProvider.getInstance()) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mFListView.setAdapter(adapter);
}
});
}
}
have no other code on any place to prepare data and set data on list.
Call the methods I mentioned from a background thread only.
I just gave general solution to your problem. You have to work on your specific case by yourself.
i want to show an circle before updating the listview , everything is working fine, while doinbackground of async task fetch the data from server it shows the progressbar but while updating the listview it freezes for sometime and then listview is shown, i want to remove that freezing of progressbar before updating, here is my code
public class feeds extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>{
protected void onPreExecute() {
// SHOW THE SPINNER WHILE LOADING FEEDS
linlaHeaderProgress.setVisibility(View.VISIBLE);
}
#Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
new_request_feeds(); //here i am fetching data from server
return fetch;
}
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
// HIDE THE SPINNER AFTER LOADING FEEDS
linlaHeaderProgress.setVisibility(View.GONE);
if(result.size()!=0)
{
adapter=new CustomListAdapter(getActivity(), R.id.list_ongoing, result);
list.setAdapter(adapter);
}
else
{
Toast.makeText(getActivity(), "no feeds", 3000).show();
}// Here if you wish to do future process for ex. move to another activity do here
}
my getview()
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos=position;
System.out.println(position);
View v = convertView;
final ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.feed_row, null);
holder = new ViewHolder();
holder.like=(ImageButton) v.findViewById(R.id.like);
holder.share=(ImageButton) v.findViewById(R.id.share);
holder.report=(ImageButton) v.findViewById(R.id.report);
holder.headline_text = (TextView) v.findViewById(R.id.lar);
holder.topic_text = (TextView) v.findViewById(R.id.mt);
holder.count_likes = (TextView) v.findViewById(R.id.count_likes);
holder.count_shares = (TextView) v.findViewById(R.id.count_shares);
holder.image = (ImageView) v.findViewById(R.id.img1);
holder.image2 = (ImageView) v.findViewById(R.id.img2);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
mSharedPreferences= v.getContext().getSharedPreferences("mypref", 0);
holder.headline_text.setText(" "+entries.get(pos).get(TAG_FFN)+" had a chance with "+entries.get(pos).get(TAG_IFN)+"! ");
holder.topic_text.setText(entries.get(pos).get(TAG_TOPIC));
holder.image.setTag(entries.get(pos).get(TAG_FTID));
holder.image2.setTag(entries.get(pos).get(TAG_ITID));
holder.count_likes.setText(entries.get(pos).get(TAG_LIKERS)+" likes");
holder.count_shares.setText(entries.get(pos).get(TAG_SHARERS)+" shares");
if(entries.get(pos).get(TAG_LIKED).equals("True"))
{
holder.like.setImageResource(R.drawable.like);
holder.like.setTag("True");
}
else
{
holder.like.setTag("False");
}
if(entries.get(pos).get(TAG_SHARED).equals("True"))
{
holder.share.setImageResource(R.drawable.share);
holder.share.setEnabled(false);
}
//=======================setting image of user==========================================//
// Loader image - will be shown before loading image
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage((image_url.getimage(Long.parseLong(entries.get(pos).get(TAG_FTID))))[0],loader, holder.image);
imgLoader.DisplayImage((image_url.getimage(Long.parseLong(entries.get(pos).get(TAG_ITID))))[0],loader, holder.image2);
//====================================================================================//
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if((holder.image.getTag()).equals(mSharedPreferences.getString("USERID", null)))
{
Toast.makeText(v.getContext(), "Your profile", Toast.LENGTH_LONG).show();
}
else
{
Intent i=new Intent(v.getContext(),OtherProfilePage.class);
i.putExtra("Image_id",entries.get(pos).get(TAG_FTID));
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
(v.getContext()).startActivity(i);
}
}
});
holder.image2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(holder.image2.getTag().equals(mSharedPreferences.getString("USERID", null)))
{
Toast.makeText(v.getContext(), "Your profile", Toast.LENGTH_LONG).show();
}
else
{
Intent i=new Intent(v.getContext(),OtherProfilePage.class);
i.putExtra("Image_id", entries.get(pos).get(TAG_ITID));
(v.getContext()).startActivity(i);
}
}
});
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(holder.like.getTag()=="True")
{
holder.like.setImageResource(R.drawable.like_pressed);
holder.like.setTag("False");
new sendlikes().execute("link");
}
else
{ holder.like.setImageResource(R.drawable.like);
holder.like.setTag("True");
new sendlikes().execute("link");
}
}
});
holder.share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.share.setImageResource(R.drawable.share);
holder.share.setEnabled(false);
new sendlikes().execute("http://gangster.cloudapp.net/share/",entries.get(pos).get(TAG_CID),mSharedPreferences.getString("person_id",null));
}
});
return v;
}
imageloader is a class that i used for caching the images.
Try to put this line linlaHeaderProgress.setVisibility(View.GONE); after if and else blocks. Hope this will work for you.
I think the issue could lie in a couple places:
1) You're doing a good deal of pre-processing of data in you Adapter's constructor.
Remember that everything in onPostExecute() runs on the application UI thread, so if your constructor for your adapter does some heavy processing, it could lock up the UI thread.
Creating an adapter can be done in doInBackground and then passed as the result to onPostExecute so that the setAdapter call is the only call there.
2) Your getView() method in your adapter isn't very efficient.
When you call setAdapter, the AdapterView must call getView for every cell that's going to be shown on screen. If there are a lot of cells and you're doing a lot of expensive operations like inflating views or findingViewsById, you could be locking up the UI thread for that initial load.
How's your scrolling performance? If it's lacking, the getView() would be the first place to start and I would watch Romain Guy's great talk on ListView performance for recommendations on how to create a good, efficient adapter.
http://www.youtube.com/watch?v=wDBM6wVEO70
add a listener when onpostexecute is completed in listener onsuccess method in your activity set adpater and then dismiss progress it will work
in you activity add this interface
public interface asyncListener{
public void onSucess(Object object);
public void onFailure(Exception exception);
}
and add a varible
asyncListener as= new asyncListener() {
#Override
public void onSucess(Object object) {
// TODO Auto-generated method stub
}
#Override
public void onFailure(Exception exception) {
// TODO Auto-generated method stub
}
};
send this varible(listenr) in construtor in your asynctask contruct asign this to yourlistener in onpostexecute write yourlistener.onSucess(result);