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();
}
Related
I have a group of items in spinner and on their click I want to show the respective text in EditText . How I can achieve this. I thought of using switch but it doesnot work for strings. I want someone to tell me right approach for doing this.
I want the EditText array(mysuburb) to respond accordingly to the Spinner items(mystate) click .
Code:-
String[] mysuburb =new String[]{"sub1" ,"sub2","sub3","sub4","sub5","sub6"};
String[] mystate= new String[]{"NSW","Victoria","Qld","NT","WA","SA"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.listrow, mystate);
// LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
// TODO Auto-generated method stub
sstate = state.getSelectedItem().toString();
/* String sub= state.getItemAtPosition(0).toString();
if(sub=="sub1")
suburb.setText("sub1") ; */
suburb.setText(arg0.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,int i, long l) {
sstate = state.getSelectedItem().toString();
suburb.setText(sstate);
}
}
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);
I have a button and a spinner (originally hidden). When user presses a button, spinner gets populated with items and becomes visible. Now I would like to add OnItemSelectedListener to the spinner. and I have tried many tutorials with no luck.
This is my OnCreate function
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button load_routes = (Button)findViewById(R.id.load_routes);
Spinner routes = (Spinner)findViewById(R.id.routes_list);
load_routes.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
load_routes(v);
}
});
routes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id)
{
Log.v("routes", "route selected");
}
public void onNothingSelected(AdapterView<?> arg0)
{
Log.v("routes", "nothing selected");
}
});
}
This is my load_routes function
private void load_routes(View v)
{
Spinner routes = (Spinner)findViewById(R.id.routes_list);
List<String> routes_list = RouteParser.get_routes();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes_list);
routes.setAdapter(adapter);
TableRow list_of_routes_row = (TableRow)findViewById(R.id.list_of_routes_row);
list_of_routes_row.setVisibility(View.VISIBLE);
}
This set up does not work. The only way I got this to work is when I setup my listener as routes.setOnItemSelectedListener(this) Then I implement OnItemSelectedListener and include the functions neccessary. But I have multiple spinners and need to create separate listeners for different spinner. Any help will be appreciated. Thanks!
final String[] s2 = getResources().getStringArray(R.array.capteur_size);
final EditText ed = (EditText) findViewById(R.id.editTextCoC);
spinnerCoC = (Spinner) findViewById(R.id.spinnerCoC);
spinnerCoC.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ed.setText(s2[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Declare your Spinner as field instantiate the listener once you do findViewById and use it wherever you want.
Im desperately trying to get my listView to open an alert dialog or a normal dialog that is filled with information. I cant seem to get it to work. I want it to display different information aswell depending on which item on the list is clicked
public class learn_tab1 extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
BASICLIST));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setCancelable(false);
dialog.setTitle("Instructions");
dialog.setIcon(R.drawable.bone_icon);
dialog.setMessage("test");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
dialog.show();
}
}
});
}
Try new AlertDialog.Builder(learn_tab1.this).create(); instead of new AlertDialog.Builder(this).create().
I'm wondering how it ever get complied....
EDIT
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String)parent.getItemAtPosition(position);
// ...
dialog.setMessage(item);
// ...
}
I have been trying to set a new xml layout, when a particular item on this list is clicked.
Am I missing something, because the emulator crashes when clicked?! setContentViewById(R.id.newxml file)
public class intentProject extends ListActivity
{
ListView list;
ArrayAdapter<String> aa;
List<String> data = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data.add("France");
data.add("Japan");
data.add("Russia ");
data.add("Poland");
data.add(" USA");
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data
);
setListAdapter(aa);
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
this.setContentView(R.layout.main2);
}
}
No I am not using a listview in the next xml. It is going to be a plain xml file with 2 buttons. These buttons are going to implement intents.