Can't get list of contacts phone book - android

I have a function to show Dialog.
public Dialog sendSMS(){
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialogsms);
dialog.setTitle("Send SMS");
dialog.setCancelable(true);
final Spinner spn = (Spinner)findViewById(R.id.spn_contatcs);
final TextView tenso = (TextView)findViewById(R.id.txt_phone);
final ArrayList<String> ten = new ArrayList<String>();
final ArrayList<String> so = new ArrayList<String>();
Cursor phones = _ketquatimkiem.this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
while (phones.moveToNext()){
String phoneName=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ten.add(phoneName);
so.add(phoneNumber);
}
phones.close();
ArrayAdapter<String> arrayAdapter_Contacts = new ArrayAdapter<String>(_ketquatimkiem.this,android.R.layout.simple_spinner_item,ten);
arrayAdapter_Contacts.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn.setAdapter(arrayAdapter_Contacts);
spn.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
tenso.setText("Phone Num: "+so.get(arg2).toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
return dialog;
}
Call in onCreate()
//when i click item of listview i get quickactiondialog
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
final QuickActionDialog quickAction = new QuickActionDialog(arg1.getContext(), QuickActionDialog.VERTICAL);
//when click item of quickactiondialog id == sms i show dialog
quickAction.setOnActionItemClickListener(new QuickActionDialog.OnActionItemClickListener() {
#Override
public void onItemClick(QuickActionDialog source, int pos, int actionId) {
if(actionId == ID_SMS){
Dialog dialog= sendSMS();
dialog.show();
}
}
});
But i get error: E/AndroidRuntime(15562): java.lang.NullPointerException at spn.setAdapter(arrayAdapter_Contacts);
I test on real device ss gt-5570. Sorry i use english not good :(

In your sendSMS() method you are accessing the layout of your Activity
final Spinner spn = (Spinner)findViewById(R.id.spn_contatcs);
That must be null because your Activity layout does not contain the Spinner. You have to do something like the following (see content.findViewById)
Dialog dialog = new Dialog(this);
View content = View.inflate(this, R.layout.dialogsms, null);
// your contact stuff
Spinner spn = (Spinner) content.findViewById(R.id.spn_contatcs);
spn.setAdapter(arrayAdapter_Contacts);
dialog.setContentView(content);

Related

I want to get the selected value from List view in alert dialog

I have ArrayList of object (citiesInSpinner) each object have two value(Id, Name)
I have already get it in alert dialog
I use this function to alert dialog:
public void test()
{
FillSpinner();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SearchFligtsActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.custom, null);
alertDialog.setView(convertView);
alertDialog.setTitle("Select City");
ListView lv = (ListView) convertView.findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,citiesInSpinner);
lv.setAdapter(adapter);
/* alertDialog.setItems(citiesInSpinner, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
/////
}
});*/
alertDialog.show();
/**/
}
now I want to make something to get the ((ID)) of item (I mean the Id of City)
I tried to do that but I failed...
any help Please !!
and thank you
Simply use
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
City city = citiesInSpinner.get(position)
//get your id -> city.Id
}
});

Spinner only saving first word to string

I am trying to get the spinner to display the item which i have selected. But it is only displaying the first word even if i choose the ones below. Here is the code i am using
ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
getApplicationContext(), R.layout.spinner_item, R.id.textView1, al);
spFacilityType.setAdapter(aa1);
spFacilityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
int index = arg0.getSelectedItemPosition();
position = index;
}
});
final String Strspinner = spFacility.getItemAtPosition(position).toString();
Use onItemSelected instead of onNothingSelected.
do the code in onItemSelected()..
String s= spFacilityType.getSelectedItem().toString();
now s will show the selected item

Changing TextView value with change in selectedItem on Spinner

I am totally at my wits end with trying to change the value of a TextView based on what is selected in the adjoining Spinner.
public class SpinnerSelectItemListener implements OnItemSelectedListener {
private Context context;
public SpinnerSelectItemListener(Context c){
this.context = c;
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
View view = null;
TextView textView = null;
LayoutInflater inflater = LayoutInflater.from(context);
parent.getItemAtPosition(position);
view = new View(context);
view = inflater.inflate(R.layout.common_app_header, null);
textView = (TextView)view.findViewById(R.id.customer_name_value);
textView.setText("John");
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
When I run this is debug mode everything is happening as expected but when all is done, the value of the textView doesn't change on the emulator even when the debugger is showing the new value.
There is definitely something really silly that I am missing. Please help.
EDIT: The situation is something like I selected the id number of an employee from the spinner and depending on the selection, the TextView displaying the employee's name changes. The TextView I want to modify is outside the spinner.
EDIT2: This runs fine when I define the listener inline i.e. I write something like
modelspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
TextView textView = null;
textView = (TextView)findViewById(R.id.customer_segment_value);
textView.setText("Commercial");
textView = (TextView)findViewById(R.id.TIV_value);
textView.setText(R.string.app1_name);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Can someone explain what is wrong with the code that I had written earlier.
See here
http://developer.android.com/guide/topics/ui/controls/spinner.html
Modify this method:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
to
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String value = (String) parent.getItemAtPosition(pos)
textView = (TextView)view.findViewById(R.id.customer_name_value);
textView.setText(value );
}
but I recommend you to move
textView = (TextView)view.findViewById(R.id.customer_name_value);
to the method onCreate of your Activity
I had a similar issue. I fixed it by getting the textview before I got into the onItemSelected. In my case, the spinner was part of a dialog. Inside the onCreateDialog, that is where I fetched the textview.
protected Dialog onCreateDialog(int id) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
switch (id) {
case DIALOG_ADD:
builder.setTitle("Create New Action");
final View textEntryView = getLayoutInflater().inflate(
R.layout.addactionrow, null);
builder.setView(textEntryView);
workingAmount = (TextView) textEntryView
.findViewById(R.id.WorkingActionamount);
Then inside the OnItemSelected I just used the textview and things started working as expected.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view,
int pos, long id) {
spinnerSelectedAssetID = id;
//Get the amount currently held here.....
long x = pfdata.getActionCurrentTotalForAssetByID(spinnerSelectedAssetID);
Log.d("X+", "X="+x);
workingAmount.setText(Long.toString(x));
workingAmount.setVisibility(View.VISIBLE);
Try This Code:
public class MainActivity extends Activity {
String[] text1 = { "SUNDAY", "MONDAY", "TUESDAY",
"WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
int[] val1 = { 0, 1, 2, 3, 4, 5, 6};
Spinner spinner1;
TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView)findViewById(R.id.text1);
spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(onItemSelectedListener1);
}
OnItemSelectedListener onItemSelectedListener1 =
new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String s1 = String.valueOf(val1[position]);
textView1.setText(s1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
};
}
XML CODE
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Try this code.And main key in this is " bResult.setText( spinner1csr.getString( spinner1csr.getColumnIndex(DatabaseHandler.KEY_ID1) ) );"
public class MainActivity extends AppCompatActivity {
Spinner s1, s2, s3;
TextView tex, tex1, bResult;
Cursor spinner1csr, spinner2csr, spinner3csr, spinner4csr, search;
SimpleCursorAdapter sca, sca2, sca3, sca4, sca6;
long spinner1_selected = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Spinner) findViewById(R.id.spinner1);
s2 = (Spinner) findViewById(R.id.spinner2);
s3 = (Spinner) findViewById(R.id.spinner5);
final TextView bResult = (TextView)
findViewById(R.id.barcodeResult);
dbhndlr = new DatabaseHandler(this);
// Get Cursors for Spinners
spinner1csr = dbhndlr.getAllLabelsAsCursor();
//Setup Adapter for Spinner 1
sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, spinner1csr,
new String[] {
DatabaseHandler.KEY_ID
},
new int[] {
android.R.id.text1
},
0
);
s1.setAdapter(sca);
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView << ? > parent, View view, int position, long id) {
// bResult.setText(s1.getSelectedItem().toString());
spinner1_selected = id;
}
#Override
public void onNothingSelected(AdapterView << ? > parent) {}
});
spinner4csr = dbhndlr.getByRowid(spinner1_selected);
sca4 = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
spinner4csr,
new String[] {
DatabaseHandler.KEY_ID1
},
new int[] {
android.R.id.text1
},
0
);

Android Spinner in a dialog give this error android.view.WindowManager$BadTokenException: Unable to add window

i need to show a Dialog in my application. In this dialog there is a Spinner. So i use this code to show the dialog and fill the Spinner:
public class setup4 extends Activity {
public List<String> materie = new ArrayList<String>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup4);
Database d = new Database(this);
SQLiteDatabase db = d.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from materie", null);
if (cursor.moveToFirst()) {
do materie.add(cursor.getString(1)); while (cursor.moveToNext());
}
db.close();
}
//On bottone setup 4
public void onSetup4bottone(View v)
{
AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
customDialog.setTitle("Aggiungi ora scolastica");
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.aggiungi_ora,null);
customDialog.setView(view);
Spinner spinner = (Spinner) view.findViewById(R.id.aggiungi_ora_materia);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item, materie);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String selected = (String) parentView.getItemAtPosition(position);
Toast.makeText(
getApplicationContext(),
"hai selezionato "+selected,
Toast.LENGTH_LONG
).show();
}
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
customDialog.show();
}
}
the spinner loads items correctly but when i click on it to change the value the application crash with this error: android.view.WindowManager$BadTokenException: Unable to add window
I also find this thread Android Spinner Error : android.view.WindowManager$BadTokenException: Unable to add window but i can't understand the solution
SOLVED
Instead of
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
use
LayoutInflater layoutInflater = getLayoutInflater();
Try to add your window in a different context.
Replace your line ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item, materie);
with the following one:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(setup4.this /**Your activity_name.this*/,android.R.layout.simple_spinner_item, materie);
Let me know if it works...
In your spinner.setOnItemSelectedListener(), don't call getApplicationContext() but instead write this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String selected = (String) parentView.getItemAtPosition(position);
Toast.makeText(
setup4.this, //<===THIS LINE IS CHANGED BY ME
"hai selezionato "+selected,
Toast.LENGTH_LONG
).show();
}

How to write onclick event for ListView in Android?

Hi i have the listview the sixitems in it, but when i call alet function on event it doesnt work ? let me know how to write a function on item event on click?
public class PhotoListView extends ListActivity {
String[] listItems = {"HeadShot", "BodyShot ", "ExtraShot", "Video Take1", "Video Take2", "Video Take3", };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, listItems));
}
OnListclick
ListView Shot = getListView();
protected void onListItemClick(View view) {
if(view == Shot){
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("Please Get Ready");
}
ListView Shot = getListView();
In Shot you have the id for the listview and not for each item in the list.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("Please Get Ready").show();
}
Or you could use ListView::setOnItemClickListener
public class PhotoListView extends ListActivity implements OnItemClickListener
ListView shot = getListView();
shot.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("Please Get Ready").show();
}

Categories

Resources