Package has already posted 50 toasts. Not showing more - android

Hello I am working on an app and in which at one point I need to take input from user and store it in database and then fetch that data and put it on different TextView. Problem is I did stored the data in database and when i trying to fetch it My app shows a pop up i.e : App is not responding and logcat shows this:
Package has already posted 50 toasts. Not showing more.
I have used 6 database operations before this pullAccStt can this be a problem.
DatabseAdapotor
public long updateAccSettTable(String loactionName, String hospitalname, String PatientId, String FirstName, String MiddleName, String LastName, String DOB,
String Gender, String Weight, String Height, String MobNo, String emailId, String Ethinicity, String patientConcentStatus, String DevicePatientId,
String CoSensorId, String DeviceId, String VideoId){
SQLiteDatabase db= helper.getWritableDatabase();
ContentValues contentValuesAccSett= new ContentValues();
contentValuesAccSett.put(DatabaseHelper.PatientId,PatientId);
contentValuesAccSett.put(DatabaseHelper.LocationName,loactionName);
contentValuesAccSett.put(DatabaseHelper.HospitalName,hospitalname);
contentValuesAccSett.put(DatabaseHelper.FirstName,FirstName);
contentValuesAccSett.put(DatabaseHelper.MiddleName,MiddleName);
contentValuesAccSett.put(DatabaseHelper.LastName,LastName);
contentValuesAccSett.put(DatabaseHelper.DateOfBirth,DOB);
contentValuesAccSett.put(DatabaseHelper.Gender,Gender);
contentValuesAccSett.put(DatabaseHelper.Weight,Weight);
contentValuesAccSett.put(DatabaseHelper.Height,Height);
contentValuesAccSett.put(DatabaseHelper.MobileNo,MobNo);
contentValuesAccSett.put(DatabaseHelper.EmailId,emailId);
contentValuesAccSett.put(DatabaseHelper.Ethnicity,Ethinicity);
contentValuesAccSett.put(DatabaseHelper.P_ConsentStatus,patientConcentStatus);
contentValuesAccSett.put(DatabaseHelper.DevicePId,DevicePatientId);
contentValuesAccSett.put(DatabaseHelper.DeviceId,DeviceId);
contentValuesAccSett.put(DatabaseHelper.CoSensorId,CoSensorId);
contentValuesAccSett.put(DatabaseHelper.VideoId,VideoId);
long id1= db.insert(DatabaseHelper.Accoun_setting_Table,null,contentValuesAccSett);
if (id1<0){
Message.message(helper.context,"Unsuccessfull");
}else {
Message.message(helper.context,"Account setting Updated");
}
db.close();
return id1;
}
public String pullAccSett(){
String patientId = null;
Message.message(helper.context,"patient id is: ");
SQLiteDatabase db=helper.getWritableDatabase();
String query="Select * From "+DatabaseHelper.Accoun_setting_Table+" ;";
Cursor AccCursor=db.rawQuery(query,null);
Message.message(helper.context,"query "+query);
while(AccCursor.moveToFirst()) {
int index1 = AccCursor.getColumnIndex(DatabaseHelper.PatientId);
patientId=AccCursor.getString(index1);
}
AccCursor.close();
db.close();
return patientId ;
}
Activity Class calling this
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class AccountSettingFragment extends Fragment {
FloatingActionButton fabEdit;
DatabaseAdaptor databaseAdaptor=null;
TextView PatientId;
private Context context;
public AccountSettingFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_account_setting, container, false);
fabEdit=(FloatingActionButton)view.findViewById(R.id.btnFabEdit);
PatientId=(TextView)view.findViewById(R.id.patientId);
context=getActivity();
databaseAdaptor=new DatabaseAdaptor(context);
databaseAdaptor.pullAccSett();
fabEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AccountsSettingEdit.class);
getActivity().startActivity(intent);
}
});
return view;
}
}
Message Class
import android.content.Context;
import android.widget.Toast;
/**
* Created by frnds on 3/2/2016.
*/
public class Message {
public static void message(Context context,String message){
Toast.makeText(context,message,Toast.LENGTH_LONG).show();
}
}
And when i call this pullAccSett(); my app hangs and goes to not responding.
and log cat shows message:
Package has already posted 50 toasts. Not showing more.
What is the reason behind this.

Your while loop i thinks will runs for infinite time. because every time it will be true and you have no break statement.
so change your code like this.
try {
if (AccCursor.moveToFirst()) {
while (AccCursor.moveToNext()) {
//code for fetch data from cursor
}
}
AccCursor.close();
}catch (Exception e) {
Log.e("Getdata", e.getMessage(), e);
}

There is some enhancement for the code above. If cursor is in position -1, AccCursor.moveToNext will move it to position 0, so you can do better something like:
try {
while(AccCursor.moveToNext()) {
//Execute code
}
AccCursor.close();
}catch(final Exception lException) {
Log.e("Getdata", "Exception looping Cursor: " + lException.getMessage());
}

Related

How to add a button to Firebase Recycler view in Android?

I want to add a button for each item in the Firebase recycler view.
I've created the button, it's being showed, and i'm receiving the Toast when I press it.
But - How can I get the relevant item on it's list ?
For example - When I press the 'DELETE' I want to delete that certain mission 'aspodm'. (Sorry for the large picture, how do I make it smaller?)
This is how the database looks like (vfPvsk... is the user id, and 773f... is random uuid for mission id):
And that is the code of the relevant fragment. (The Toast when clicking the button is activated - I just don't know how to get to the relevant mission in order to delete it)
package com.airpal.yonatan.airpal;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
/**
* A simple {#link Fragment} subclass.
*/
public class PendingFragment_User extends Fragment {
private String TAG = "dDEBUG";
private RecyclerView mPendingList;
private DatabaseReference mMissionsDb;
private FirebaseAuth mAuth;
private String mCurrent_user_id;
private View mMainView;
Query queries;
public PendingFragment_User() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMainView = inflater.inflate(R.layout.fragment_pending_user, container, false);
mPendingList = (RecyclerView)mMainView.findViewById(R.id.pending_recycler_user);
mAuth = FirebaseAuth.getInstance();
mCurrent_user_id = mAuth.getCurrentUser().getUid();
mMissionsDb = FirebaseDatabase.getInstance().getReference().child("Missions").child(mCurrent_user_id);
queries = mMissionsDb.orderByChild("status").equalTo("Available");
mMissionsDb.keepSynced(true);
mPendingList.setHasFixedSize(true);
mPendingList.setLayoutManager(new LinearLayoutManager(getContext()));
// Inflate the layout for this fragment
return mMainView;
}
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Mission, PendingFragment_User.MissionsViewHolder> firebaseMissionsUserRecyclerAdapter = new FirebaseRecyclerAdapter<Mission, PendingFragment_User.MissionsViewHolder>(
Mission.class,
R.layout.missions_single_layout,
PendingFragment_User.MissionsViewHolder.class,
queries
) {
#Override
protected void populateViewHolder(PendingFragment_User.MissionsViewHolder missionViewHolder, final Mission missionModel, int missionPosition) {
// Log.d(TAG, "inside populateViewHolder" + missionModel.getType() + " , " + missionModel.getDescription());
missionViewHolder.setMissionName(missionModel.getType());
missionViewHolder.setMissionDescription(missionModel.getDescription());
missionViewHolder.setMissionStatus(missionModel.getStatus());
missionViewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Toast.makeText(view.getContext(), "esto es un boton"+ view.getNextFocusUpId(), Toast.LENGTH_SHORT).show();
Log.d(TAG,"The button was pressed");
}
});
}
};
mPendingList.setAdapter(firebaseMissionsUserRecyclerAdapter);
}
public static class MissionsViewHolder extends RecyclerView.ViewHolder {
View mView;
Button button ;
public MissionsViewHolder(View itemView) {
super(itemView);
mView = itemView;
button = (Button)mView.findViewById(R.id.pending_single_button);
}
public void setMissionName(String name){
TextView mMissionNameView = mView.findViewById(R.id.mission_single_name);
mMissionNameView.setText(name);
}
public void setMissionStatus(String status){
TextView mMissionStatusView = mView.findViewById(R.id.mission_single_status);
mMissionStatusView.setText(status);
if (status.equals("Available")){
mMissionStatusView.setTextColor(Color.parseColor("#008000"));;
} else {
mMissionStatusView.setTextColor(Color.parseColor("#FF0000"));;
}
}
public void setMissionDescription(String description){
TextView mMissionDescriptionView = mView.findViewById(R.id.mission_single_description);
mMissionDescriptionView.setText(description);
}
}
}
Try using a subclass rather than anonymous one. Then, you'll be able to access the getItem(int position) method of the adapter. Something along the lines of:
private class MissionAdapter extends FirebaseRecyclerAdapter<Mission, PendingFragment_User.MissionsViewHolder> {
public MissionAdapter(Query queries){
super(Mission.class, R.layout.missions_single_layout, PendingFragment_User.MissionsViewHolder.class, queries);
}
#Override
protected void populateViewHolder(PendingFragment_User.MissionsViewHolder missionViewHolder, final Mission missionModel, final int missionPosition) {
Log.d(TAG, "inside populateViewHolder" + missionModel.getType() + " , " + missionModel.getDescription());
missionViewHolder.setMissionName(missionModel.getType());
missionViewHolder.setMissionDescription(missionModel.getDescription());
missionViewHolder.setMissionStatus(missionModel.getStatus());
missionViewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Mission clickedMission = MissionAdapter.this.getItem(missionPosition);
if (clickedMission != null){ // for the sake of being extra-safe
Log.d(TAG,"The button was pressed for mission: " + clickedMission.getType());
}
}
});
}
}
I made it print a log, but you should be able to do anything you want with the retrieved Mission object in there.

How do I refresh my cursor from another activity?

How do I refresh my cursor from another activity?
In my main activity I start a cursor to query phone contacts and display in a listview.
In this main activity I also have a menu, 'Add new Contact', which starts the add new contact activity. A new contact gets added correctly (I can see it in other Contacts apps on my phone), but it is not visible in my listview when the user goes back to main activity.
Is there a way to refresh the cursor from 'add new contact' activity, so the user can see it in the listview when they go back to main activity?
I use AsyncTask in my main activity, if that's any useful info. I read about swapcursor and changecursor but it didn't work when I tried to use them. Here's my 'add new contact' code :
(I call changecursor before the "Contact Saved" toast, but I'm sure it's not done correctly.
package com.example.chris.contactlistcustomlistview;
import android.content.ContentProviderOperation;
import android.content.OperationApplicationException;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Created by Chris on 06/05/2016.
*/
public class AddContact extends AppCompatActivity {
EditText nameofcontact;
EditText numberofcontact;
public String contactname;
public String contactnumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcontact);
nameofcontact = (EditText) findViewById(R.id.edittextname);
numberofcontact = (EditText) findViewById(R.id.edittextnumber);
}
public void createButton(View view) {
contactname = nameofcontact.getText().toString();
contactnumber = numberofcontact.getText().toString();
if (contactname.length() == 0) {
Toast.makeText(this, "Please enter a name",
Toast.LENGTH_LONG).show();
return;
}
ArrayList<ContentProviderOperation> contentProviderOperations = new ArrayList<ContentProviderOperation>();
//insert raw contact using RawContacts.CONTENT_URI
contentProviderOperations.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());
//insert contact display name using Data.CONTENT_URI
Log.d("ffff","wwww");
contentProviderOperations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,contactname ).build());
//insert mobile number using Data.CONTENT_URI
contentProviderOperations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contactnumber).withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());
try {
//apply the changes
getApplicationContext().getContentResolver().
applyBatch(ContactsContract.AUTHORITY, contentProviderOperations);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
// SelectContactAdapter.changeCursor(cursor);
Toast.makeText(this, "Contact saved",
Toast.LENGTH_SHORT).show();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
//This clears the edittext next time user starts the application, rather than
// having the same numbers there, which the user probably doesn't want anymore
protected void onResume() {
final EditText editText = (EditText) findViewById(R.id.edittextname);
super.onResume();
editText.setText("");
}
}
And here's my MainActivity code :
package com.example.chris.contactlistcustomlistview;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class MainActivity extends Activity implements PopupMenu.OnMenuItemClickListener {
// ArrayList
ArrayList<SelectContact> selectContacts;
List<SelectContact> temp;
// Contact List
ListView listView;
// Cursor to load contacts list
// Cursor phones, email;
Cursor pCur;
// Pop up
// ContentResolver resolver;
SearchView search;
SelectContactAdapter adapter;
String phoneContactId;
String name;
String phoneNumber;
CharSequence nameofcontact;
// String phoneNumber;
// *****18-04-2016***
Cursor cursor;
ListView mainListView;
ArrayList hashMapsArrayList;
// String contactid;
// *****
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectContacts = new ArrayList<SelectContact>();
// resolver = this.getContentResolver();
listView = (ListView) findViewById(R.id.contacts_list);
LoadContact loadContact = new LoadContact();
loadContact.execute();
search = (SearchView) findViewById(R.id.searchView);
//*** setOnQueryTextListener ***
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
adapter.filter(newText);
return false;
}
});
}
// Load data on background
class LoadContact extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... voids) {
if (cursor != null) {
cursor.moveToFirst();
}
try {
// get a handle on the Content Resolver, so we can query the provider,
cursor = getApplicationContext().getContentResolver()
// the table to query
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
// Null. This means that we are not making any conditional query into the contacts table.
// Hence, all data is returned into the cursor.
// Projection - the columns you want to query
null,
// Selection - with this you are extracting records with assigned (by you) conditions and rules
null,
// SelectionArgs - This replaces any question marks (?) in the selection string
// if you have something like String[] args = { "first string", "second#string.com" };
null,
// display in ascending order
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
// get the column number of the Contact_ID column, make it an integer.
// I think having it stored as a number makes for faster operations later on.
int Idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
// get the column number of the DISPLAY_NAME column
int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
// get the column number of the NUMBER column
int phoneNumberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// int photoIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI);
cursor.moveToFirst();
// We make a new Hashset to hold all our contact_ids, including duplicates, if they come up
Set<String> ids = new HashSet<>();
do {
System.out.println("=====>in while");
// get a handle on the contactid, which is a string. Loop through all the contact_ids
String contactid = cursor.getString(Idx);
// if our Hashset doesn't already contain the contactid string,
// then add it to the hashset
if (!ids.contains(contactid)) {
ids.add(contactid);
HashMap<String, String> hashMap = new HashMap<String, String>();
// get a handle on the display name, which is a string
name = cursor.getString(nameIdx);
// get a handle on the phone number, which is a string
phoneNumber = cursor.getString(phoneNumberIdx);
// String image = cursor.getString(photoIdIdx);
// System.out.println("Id--->"+contactid+"Name--->"+name);
System.out.println("Id--->" + contactid + " Name--->" + name);
System.out.println("Id--->" + contactid + " Number--->" + phoneNumber);
SelectContact selectContact = new SelectContact();
// selectContact.setThumb(bit_thumb);
selectContact.setName(name);
selectContact.setPhone(phoneNumber);
// selectContact.setEmail(id);
// selectContact.setCheckedBox(false);
selectContacts.add(selectContact);
}
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
// if (cursor != null) {
// }
}
cursor.close();
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//into each inflate_listview, put a name and phone number, which are the details making
// our SelectContact, above. And SelectContacts is all these inflate_listviews together
// This is the first property of our SelectContactAdapter, a list
// The next part, MainActivity.this, is our context, which is where we want the list to appear
adapter = new SelectContactAdapter(selectContacts, MainActivity.this);
listView.setAdapter(adapter);
// Select item on listclick
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
nameofcontact = ((TextView)view.findViewById(R.id.name)).getText();
// }
// Creates a new Intent to edit a contact
Intent intent = new Intent(Intent.ACTION_EDIT);
startActivity(intent);
listView.setFastScrollEnabled(true);
}
});
}}
//the is the arrow image, it opens the activity for edit or new contact
public void EditorCreateContact(View v) {
}
#Override
protected void onStop() {
super.onStop();
}
// this is for the settings menu
public void settingsPopUp(View view) {
PopupMenu popup = new PopupMenu(this,view);
popup.setOnMenuItemClickListener(MainActivity.this);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_actions, popup.getMenu());
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
// from the popup_actions.xml, identify the New_contact id and make it
// open the AddContact class
case R.id.New_contact:{
Intent intent = new Intent(this, AddContact.class);
startActivity(intent);
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivity(intent);
// finish(); // Call once you redirect to another activity
}
// Toast.makeText(getBaseContext(), "You slected New Contact",Toast.LENGTH_SHORT).show();
//
return true;
}
return false;
}
}
First of all declare the line
final EditText editText = (EditText) findViewById(R.id.edittextname);
in OnCreate() ... as it is a bad practice to initialize the view every time at on resume of activity.
and query the data in OnResume of your activity. Thats a better way to refresh the data as you come back to your activity.
You can refresh your contacts in onResume() method as i mentioned in comments, for that you can do the following in onResume() itself
LoadContact loadContact = new LoadContact();
loadContact.execute();
you can remove the above from onCreate() as it would be redundant to call as onResume() will be called every time as the Activity LifeCycle.
If you use this approach just clear ArrayList selectContacts by doing selectContacts.clear() in doInBackground() at the first line, other wise it will list contacts twice and thrice and so onn...
Also apart from these, as mentioned in another answer in same thread you can use, startActivityForResult() and in onActivityResult() method you can update contact list.
You could use startActivityForResult() instead of startActivity and override onActivityResult() to requery your cursor on success. After the toasting, setResult() to RESULT_OK and finish the AddContact activity.
setResult(RESULT_OK);
finish();

Android - pass data between from an Activity class to an Adapter class

I have 3 classes: LoginActivity,MapsActivity and MatchAdapter
The first 2 extends AppCompactActivity, the last one ArrayAdapter.
When i make login (if correct, matching on mySQLiteDB) i used to get ID_contact of current user and pass it to MapsActivity with intent in such way:
On my LoginActivity:
String contact=databaseHelper.searchID_Contact(username,password);
Intent intent=new Intent(LoginActivity.this,MapsActivity.class);
intent.putExtra("ID_CONTACT",contact);
startActivity(intent);
On MapsActivity i can easily retrieve this data in such way:
public String getId_contact(String conct){
return conct;
}
#Override
public void onMapReady(GoogleMap googleMap) {
String id_contact1=getIntent().getStringExtra("ID_CONTACT");
String contact=getId_contact(id_contact1);
Toast.makeText(MapsActivity.this, contact, Toast.LENGTH_LONG).show();
}
Till now everything works fine, it appears the id of the current user.
My problem is to pass this data (with intent i don't know how) even to another class named MatchAdapter that extends ArrayAdapter.
I tried this way on MapsActivity:
public class MapsActivity extends AppCompatActivity implements ...{
public String getId_contact(){
String contact=getIntent().getStringExtra("ID_CONTACT");
return contact;
}
So on MatchAdaper trying to retrieve such way:
MapsActivity mapsActivity=new MapsActivity();
String text=mapsActivity.getId_contact().toString();
But nothing..i get NULLPOINTEREXCEPTION...Can someone help me?
Ok...found the solution...On MatchAdapter extends ArrayAdapter
DatabaseHelper databaseHelper=new DatabaseHelper(getContext());
...than OnClick function....
databaseHelper.myfunction();
Well you can access method of activity from an adapter by following way, Call this method from constructor of adapter or anywhere you want.
((ActivityName)context).methodName();
When you create a new instance of MapsActivity, that isn't the same Activity instance you got when you called startActivity(). This is basically why you have a null pointer exception.
More importantly, you should never be manually creating Activity instances using "new". Generally the system creates Activity objects for you via mechanisms like startActivity(), and that is how you should obtain them.
Himanshu's suggestion can work, if your activity does happen to be "hosting" your adapter, but this isn't always guaranteed. A better approach is to pass the ID to your MatchAdapter directly, either in the constructor or as a direct setter function. At the least, you should perform a "instanceof" check to make sure your adapter context is really of type MapsActivity.
That's my MatchAdapter `package vincenzo.futsal4you;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.MatchResult;
public class MatchAdapter extends ArrayAdapter{
List list=new ArrayList();
String text1=null;
MatchAdapter matchAdapter;
static String id_contatto3="";
String fatto3="";
Player player=new Player();
public MatchAdapter(Context context, int resource) {
super(context, resource);
}
public void add(Match object) {
list.add(object);
super.add(object);
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public Object getItem(int position) {
return super.getItem(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
final Context context = null;
final MatchHolder matchHolder;
final String cc=null;
if (row==null){
LayoutInflater layoutInflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=layoutInflater.inflate(R.layout.display_match_row,parent,false);
matchHolder=new MatchHolder();
matchHolder.id_match=(TextView)row.findViewById(R.id.id_partita);
matchHolder.id_contact=(TextView)row.findViewById(R.id.id_contatto);
matchHolder.nome_partita=(TextView)row.findViewById(R.id.nome_partita);
matchHolder.citta=(TextView)row.findViewById(R.id.citta);
matchHolder.indirizzo=(TextView)row.findViewById(R.id.indirizzo);
matchHolder.data=(TextView)row.findViewById(R.id.data);
matchHolder.ora=(TextView)row.findViewById(R.id.ora);
// matchHolder.id_contact=row.findViewById()
matchHolder.join_us = (Button) row.findViewById(R.id.join_us);
row.setTag(matchHolder);
}
else {
matchHolder=(MatchHolder)row.getTag();
}
final Match match=(Match)getItem(position);
// matchHolder.id_contact.setText(mapsActivity.getId_partita().toString());
matchHolder.id_match.setText(match.getId().toString());
matchHolder.nome_partita.setText(match.getName().toString());
matchHolder.citta.setText(match.getCitta().toString());
matchHolder.indirizzo.setText(match.getIndirizzo().toString());
matchHolder.data.setText(match.getData().toString());
matchHolder.ora.setText(match.getOra().toString());
// assert ((MapsActivity) context) != null;
// ((MapsActivity) context).getId_partita();
// final String contact=matchHolder.getId_contatto();
Log.e("BOOOOOO", matchHolder.getId_contatto2());
final String fatto=matchHolder.getId_contatto2();
fatto3=matchHolder.getId_contatto2();
matchHolder.join_us.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//
String fatto2=matchHolder.getId_contatto2();
// final String text3=mapsActivity.getId_partita(cc).toString();
RelativeLayout rl = (RelativeLayout) v.getParent();
RelativeLayout r2 = (RelativeLayout) v.getParent();
// TextView tv = (TextView)rl.findViewById(R.id.nome_partita);
TextView tv = (TextView) rl.findViewById(R.id.id_partita);
TextView tv2 = (TextView) r2.findViewById(R.id.id_contatto);
String id_partita = tv.getText().toString();
String text2 = tv2.getText().toString();
Toast.makeText(getContext(), id_partita, Toast.LENGTH_SHORT).show();
// Toast.makeText(getContext(), matchHolder.setId_contatto(contact), Toast.LENGTH_SHORT).show();
Toast.makeText(getContext(),matchHolder.getId_contatto2(),Toast.LENGTH_SHORT).show();
player.setId_contatto(fatto3);
player.setId_partita(id_partita);
// databaseHelper=new DatabaseHelper(context);
// databaseHelper.insertPlayer2(player);
((MapsActivity)context).getJoinPlayer(player);
Toast pass1=Toast.makeText(getContext(), "One Row JOIN US created !", Toast.LENGTH_SHORT);
pass1.show();
}
});
return row;
}
static class MatchHolder{
TextView id_match,nome_partita,citta,indirizzo,data,ora,id_contact;
Button join_us;
public MatchHolder(){}
public String getId_contatto(String id_contatto) {
return id_contatto;
}
public String getId_contatto2() {
return id_contatto3;
}
public void setId_contatto(String id_contatto) {
id_contatto3 = id_contatto;
}
public MatchHolder(String id_contatto){
id_contatto3=id_contatto;
}
}
}
` So the problem was that i declared a String (id_contatto3) inside a static (inner) class (MatchHolder).Declaring it up to MatchAdapter i solve it somehow, but now i wanna call inside onClick a method that is inside another class (DatabaseHelper that extends SQLiteOpenhelper)..
I can't do ((DatabaseHelper)context).mymethod() So i've done the following "trick"...inside class MapsActivity where i've created a method (JoinPlayer) in such way:
public void JoinPlayer(Player player){
databaseHelper.insertPlayer(player);
}
where insertPlayer(Player) in DatabaseHelper is:
public void insertPlayer(Player player){
try{
db=this.getWritableDatabase();
}
catch(Exception e){
Log.e("ERROR","ERROR");
}
ContentValues contentValues=new ContentValues();
String query="select * from player";
Cursor cursor=db.rawQuery(query,null);
int count=cursor.getCount();
contentValues.put(COLUMN_ID_PLAYER,count);
contentValues.put(COLUMN_ID_MATCH_PLAYER,player.getId_partita());
contentValues.put(COLUMN_ID_CONTACT_PLAYER,player.getId_contatto());
db.insert(TABLE_PLAYER, null, contentValues);
db.close();
}
But Android suggest me to add a null condition(if ((MapsActivity)context)!=null) than ((MapsActivity)context).JoinPlayer(player) but it advise me it will be Always null and that's exactly what I get... I think is the context the main problem but have no clue right now how to solve it. Any Idea?

Out-of-sync ArrayAdapter with Autocomplete

I have 3 classes, I wish to use the autocomplete text box to show user certain data (aka cities) from a web service (rest api). I've used this implementation on various features of my own application, but for some reason, there's a synchronization problem within the textchangedlistener...
CitiesArrayAdapter.java (to show a different view, in my case the "city, state"):
package com.android.lzgo.adapters;
import java.util.ArrayList;
import java.util.List;
import com.android.lzgo.activities.LiftSearchActivity;
import com.android.lzgo.activities.R;
import com.android.lzgo.models.City;
import com.android.lzgo.models.Lift;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class CitiesArrayAdapter extends ArrayAdapter<City> {
private static final String TAG = CitiesArrayAdapter.class.getName();
private final ArrayList<City> cities;
private int viewResourceId;
public CitiesArrayAdapter(Context context, int textViewResourceId, ArrayList<City> results) {
super(context, textViewResourceId, results);
this.cities = results;
this.viewResourceId = textViewResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// assign the view we are converting to a local variable
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(viewResourceId, null);
}
City i = cities.get(position);
Log.d(TAG, "Here is my value: " + i);
if (i != null) {
TextView tt = (TextView) v.findViewById(android.R.id.text1);
Log.d(TAG, "Name: " + i.getName() + ", " + i.getProvince_name());
if (tt != null){
tt.setText("Name: " + i.getName() + ", " + i.getProvince_name());
}
}
// the view must be returned to our activity
return v;
}
}
CitiesResponderFragment.java (this is how I get my values from my rest api):
package com.android.lzgo.fragment;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import com.android.lzgo.activities.LiftSearchActivity;
import com.android.lzgo.definitions.Constants;
import com.android.lzgo.models.City;
import com.android.lzgo.service.LzgoService;
import com.google.gson.Gson;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class CitiesResponderFragment extends LzgoResponderFragment {
private static String TAG = CitiesResponderFragment.class.getName();
private List<City> mCities;
ArrayAdapter<City> adapter;
private String enteredCharacters;
LiftSearchActivity activity;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
activity = (LiftSearchActivity) getActivity();
// This gets called each time our Activity has finished creating itself.
getCities();
}
private void getCities() {
if (mCities == null && activity != null) {
Intent intent = new Intent(activity, LzgoService.class);
intent.setData(Uri.parse(Constants.REST_CITIES_AUTOCOMPLETE));
Bundle params = new Bundle();
params.putString("search", getenteredCharacters());
intent.putExtra(LzgoService.EXTRA_HTTP_VERB, LzgoService.GET);
intent.putExtra(LzgoService.EXTRA_PARAMS, params);
intent.putExtra(LzgoService.EXTRA_RESULT_RECEIVER, getResultReceiver());
// Here we send our Intent to our RESTService.
activity.startService(intent);
}
}
#Override
public void onRESTResult(int code, String result) {
Log.e(TAG, Integer.toString(code));
Log.e(TAG, result);
// Check to see if we got an HTTP 200 code and have some data.
if (code == 200 && result != null) {
mCities = getCitiessFromJson(result);
adapter = activity.getArrayAdapter();
adapter.clear();
for( City city : mCities){
//debugging
Log.d(TAG, "City : " + city.getName());
adapter.add(city);
adapter.notifyDataSetChanged();
}
getCities();
}
else {
Activity activity = getActivity();
if (activity != null && code == 400) {
Toast.makeText(activity, result, Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(activity, "Failed to load lzgo data. Check your internet settings.", Toast.LENGTH_SHORT).show();
}
}
private List<City> getCitiessFromJson(String json) {
ArrayList<City> cityList = new ArrayList<City>();
Gson gson = new Gson();
try {
JSONObject citiesWrapper = (JSONObject) new JSONTokener(json).nextValue();
JSONArray cities = citiesWrapper.getJSONArray("cities");
for (int i = 0; i < cities.length(); i++) {
//JSONObject city = cities.getJSONObject(i);
String jsonCity = cities.getString(i);
City city = gson.fromJson( jsonCity, City.class );
//Log.e(TAG, "Hurray! Parsed json:" + city.getString("name"));
//cityList.add(city.getString("name"));
cityList.add(city);
}
}
catch (JSONException e) {
Log.e(TAG, "Failed to parse JSON.", e);
}
return cityList;
}
public String getenteredCharacters() {
return enteredCharacters;
}
public void setenteredCharacters(String characters) {
this.enteredCharacters = characters;
}
}
LiftSearchActivity.java (My FragmentActivity):
package com.android.lzgo.activities;
import java.util.ArrayList;
import com.android.lzgo.adapters.CitiesArrayAdapter;
import com.android.lzgo.fragment.CitiesResponderFragment;
import com.android.lzgo.models.City;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.DatePicker;
public class LiftSearchActivity extends FragmentActivity{
private static final String TAG = LiftSearchActivity.class.getName();
// User lift input
private AutoCompleteTextView autoCityFrom;
private AutoCompleteTextView autoCityTo;
private DatePicker date;
private CitiesArrayAdapter adapter;
private ArrayList<City> mCities ;
int year , month , day;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lift_search);
mCities = new ArrayList<City>();
adapter = new CitiesArrayAdapter(this,
android.R.layout.simple_dropdown_item_1line, mCities);
autoCityFrom = (AutoCompleteTextView) findViewById(R.id.cityFrom);
autoCityTo = (AutoCompleteTextView) findViewById(R.id.cityTo);
adapter.setNotifyOnChange(true);
autoCityFrom.setAdapter(adapter);
autoCityTo.setAdapter(adapter);
autoCityFrom.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// no need to do anything
}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (((AutoCompleteTextView) autoCityFrom).isPerformingCompletion()) {
return;
}
if (charSequence.length() < 2) {
return;
}
String query = charSequence.toString();
getCities(query);
}
public void afterTextChanged(Editable editable) {
}
});
autoCityTo.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// no need to do anything
}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (((AutoCompleteTextView) autoCityTo).isPerformingCompletion()) {
return;
}
if (charSequence.length() < 2) {
return;
}
String query = charSequence.toString();
getCities(query);
}
public void afterTextChanged(Editable editable) {
}
});
date = (DatePicker) findViewById(R.id.dpResult);
}
public void searchLifts(View view) {
Intent intent = new Intent(this, LiftsResultActivity.class);
//While autocomplete doesn't work hardcore value...
intent.putExtra("from", Integer.toString(9357)); // Sherbrooke
intent.putExtra("to", Integer.toString(6193)); // Montreal
intent.putExtra("date", Integer.toString(date.getMonth()+1) + "-" + Integer.toString(date.getDayOfMonth()) + "-" + Integer.toString(date.getYear()));
startActivity(intent);
}
public void getCities(String query) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
CitiesResponderFragment responder = (CitiesResponderFragment) fm.findFragmentByTag("RESTResponder");
responder = new CitiesResponderFragment();
responder.setenteredCharacters(query);
ft.add(responder, "RESTResponder");
ft.commit();
}
public CitiesArrayAdapter getArrayAdapter() {
// TODO Auto-generated method stub
return adapter;
}
}
I get the correct result and all. But my service doesn't seem to populate my array adapter in my activity, when I try to show my first "city", my adapter contains nothing. I wonder if I have to put a notifydatasetchanged (I tried, but doesn't work). I'm kind of confuse... any pointers?
While debugging the application I noticed that the properties mObjects of
the ArrayAdapter is cleared even if the associated ArrayList has
elements, and then properties mOriginalValues is filled with the
Strings loaded the first time.
Without seeing the full code base(+ data), I don't know if someone can pin point the reason for your code's failure. But I think the problem comes more from the way you setup the whole auto complete related code than some obvious line error. Below I'll try to address some of this issues(from my point of view) :
First of all, in the TextWatcher from LiftSearchActivity you call the getCities() method which adds, each time the user modifies the auto complete text, a Fragment to the Activity. I really doubt you want this, you should probably look at having only one Fragment in the Activity on which to call a refresh(or update) method, passing it the new filter text. If the user rotates the phone those fragments will also be recreated because of the configuration change.
Second, in the CitiesResponderFragment class you call the fragment's getCities() method(if you don't have any data) in onActivityCreated which start an update service(?!). Now related to the first point, you could end up doing a lot of unnecessary queries, for example if the user enter 4 characters and then decides to delete one of the characters as it was incorrect you'll end up with 5 added fragments, from which 3 will make the service start/query for data.
And last, I'm not sure if you understand how the AutoCompleteTextView works under the hood. In order to provide the drop down with the suggestions the AutoCompleteTextView widget will filter its adapter(adapter.getFilter()) and show as suggestions the items that match the filter. I don't know if you set some threshold on the AutoCompleteTextView but initially the auto complete will be empty for the first 3 characters entered as you start with an empty list of items when you first setup the Activity. The first two characters will not show anything because you start with an empty list and you don't add any new fragments(charSequence.length() < 2). The third character will most likely also not show anything, because the overhead of creating the fragment, starting the service and fetching the data will almost for sure be greater then doing the work of the adapter filtering(which will still see the initially empty list). I don't know if you tested, but from this fourth character the adapter should have some elements in it and the filtering should show something. Clearing the adapter and adding new data in it willl only make that data available to the next character entered in the AutoCompleteTextView.
The proper way of doing the filtering would be to further extend your adapter and implement the getFilter() method to return your own Filter implementation which would query the data store for new filtered items. The filtering method runs on a background thread, with a little work I think you could implement your current logic with the Service and the REST callback.
See example of the REST API Autocomplete : https://subversion.assembla.com/svn/rockitsearch-android/
Although it is part of my service, it can also serve as an example of how you can integrate your own REST API in AutoCompleteTextView control. If you are interested in autocomplete 3rd party service, here you go : www.rockitsearch.com

Using method from another class containing Cursor

Working on developing a Class for my android app that will randomly generate NPCs and their stats. Since these methods will be called in different activities figured putting them in their own class would be best. Yet having some trouble getting the cursors they contain to work correctly.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
public class startscreen extends Activity {
private dbhelper mydbhelper;
Intent intent;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startscreen);
mydbhelper = dbhelper.getInstance(startscreen.this);
}
public void onClickNew(View v){
intent = new Intent(getBaseContext(), newgame.class);
startActivity(intent);
}
public void onClickLoad(View v){
//intent = new Intent(getBaseContext(), mainmenu.class);
//startActivity(intent);
Generator obj = new Generator();
obj.randomName();
}
I am calling the method on this bottom button to my main menu for easy testing.
import java.util.Random;
import android.database.Cursor;
import android.util.Log;
public class Generator {
public dbhelper mydbhelper;
public String randomName(){
Cursor name;
Random nameRandom = new Random();
int first = nameRandom.nextInt(171)+1;
name = mydbhelper.getRandomName(first); //This is where I get the error. "NullPointerException"
String firstName = name.getString(name.getColumnIndex(dbhelper.KEY_FIRST));
int last = nameRandom.nextInt(245)+1;
name = mydbhelper.getRandomName(last);
String lastName = name.getString(name.getColumnIndex(dbhelper.KEY_LAST));
String fullName = firstName + " " + lastName;
Log.e(fullName, "was the generated name");
return fullName;
}
}
If I take out the cursors and put in a stat string to return it works fine. So I'm handling this cursors incorrectly. Sadly Singleton and using methods from other classes is on of my short comings so hoping someone here will be able to explain what I'm doing wrong.
public Cursor getRandomName(int number){
return myDataBase.query(NAME_TABLE, new String[]{KEY_ID, KEY_FIRST, KEY_LAST},
KEY_ID + " = " + number , null, null, null, KEY_ID);
}
This is the cursor I'm working with, located in my dbhelper class.
mydbhelper in your Activity class is a different reference than the mydbhelper in your Generator class. You need to call mydbhelper = dbhelper.getInstance() in your Generator class or pass in the reference before you use it.
A quick way to do it would be:
public String randomName(dbhelper mydbhelp) {
// code
name = mydbhelper.getRandomName(first);
// code
name.close
}
Remember to close both your cursor and your database when you're done with it. Close the Cursor first.

Categories

Resources