Android - Creating an alert dialog in listview from selected item - android

Ive been struggling to get an alert dialog to pop up when the user clicks an item in the list view. I can do a toast message but cant seem to get an alert dialog to work. I then want to have a button in the alert dialog to allow me to take the item just selected and display it on the next screen where it will be show contact details etc for the one selected. If anyone can give me any insight into any good techniques or tips on how to do this it would be awesome! Im a very new programmer and ive tried everywhere.
Below is my code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] taxi = getResources().getStringArray(R.array.taxi_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.listtaxi, taxi));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_LONG).show();
}
});
}
}
The array is in the strings.xml file.
Any help would be very much appreciated.

Replace
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_LONG).show();
with
Toast.makeText(lv.getContext(), ((TextView) view).getText(), Toast.LENGTH_LONG).show();

Your code in general is correct. I'd make sure your code for posting a dialog works correctly first. Try it out in a way outside of the list, and then see if it works in the onClick() statement itself.

Try using YourClass.this instead of getApplicationContext().

Related

Save Google Form via Android App

I created an Android app where users can save data to Google Spreedsheet using Google Form. I followed this POST which I found is very useful for me, but I'm having some trouble. I'm not able to save data of RadioButton , CheckBox, Date....To upload data we need Key-Value pair.. for TextView its easy to find as ID field is using Inspect Element, But for RadioButton , CheckBox there are multiple IDs.. How to set data for that..
Can anyone please tell me, how to make it possible??
I wanted to work with Radion Buttons, but as there are number of Id's for different radio buttons. So i just opted for "Choose from a List" in google form. There in list you will have one Id for spinner and multiple options. Take the Entry_Id from form and attached with your spinner id. Here is the code
s = (Spinner) findViewById(R.id.spinner);
list.add("Male");
list.add("Female");
ArrayAdapter<String> ad = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, list);
ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(ad);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
result = list.get(position);
Toast.makeText(getApplicationContext(), "you selected:" + result, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
And in Your Post() method add this lines
gender = String.valueOf(s.getSelectedItem()); String data ="entry_1008323309=" + URLEncoder.encode(gender);
Hope this Will help you out.

check boxes in listview not working

I am using listview in which each item (of list view) has a checkbox and a textview.
when i am clivking on listview thw listener doesn't executes.
here is the code.
ListView lv = (ListView) findViewById(R.id.list);
final CustomListArrayAdaptor aa = new CustomListArrayAdaptor(this,data1);
lv.setAdapter(aa);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
{
TextView tv=(TextView)v.findViewById(R.id.text);
String s=tv.getText().toString();
Toast.makeText(getApplicationContext(), "Item Selected :"+s,Toast.LENGTH_LONG).show();
}
});
It doesnt show the toast "Item Selected" when clicked on any item.
This part of your code is correct. Upload the other files code also.
As much I know this could be the problem of focus.Add (android:focusable="false")
if you are defining the check box in xml file or for the java code use the method myCheckBox.setFocusable(false).
As explained here
Android custom ListView unable to click on items,
the click listener only works if no other view is focusable. Setting your CheckBox to focusable="false" should do the trick for you

Why are clicks in my ExpandableListView ignored?

I have an AlertDialog populated by an ExpandableListView. The list itself works perfectly, but for some reason clicks are ignored. Apparently my click handler is never called.
This is the code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select something");
ExpandableListView myList = new ExpandableListView(this);
MyExpandableListAdapter myAdapter = new MyExpandableListAdapter();
myList.setAdapter(myAdapter);
myList.setOnItemClickListener(new ExpandableListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
try {
Toast.makeText(ExpandableList1.this, "You clicked me", Toast.LENGTH_LONG).show();
}
catch(Exception e) {
System.out.println("something wrong here ");
}
}
});
builder.setView(myList);
dialog = builder.create();
dialog.show();
If I instead try to populate the AlertDialog with a plain listview, click events are generated without problems:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");
ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);
modeList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
builder.setView(modeList);
AlertDialog dialog1 = builder.create();
dialog1.show();
What could be the reason why click event fails in my ExpandableListView but not in a normal ListView? I am probably missing something, but I have no idea what that could be.
Ok, the solution was quite simple. Since it is an expandable list, item clicks are captured by the list itself to open the child elements. Thus, the event handler is never called.
Instead you have to implement OnChildClickListener() that - as the name suggests - listens to child clicks!
Thx, I was also wondering about it. But still interesting is info in documentation:
public void setOnItemClickListener (AdapterView.OnItemClickListener l)
"Register a callback to be invoked when an item has been clicked and the caller prefers to receive a ListView-style position instead of a group and/or child position.".
In docs there is clearly mentioned that you should be able to set and handle this listener if you want...
You can actually use
expandableListView.setOnGroupExpandListener to catch the event when expanding
expandableListView.setOnGroupCollapseListener to catch the event when collapsing.
expandableListView.setOnGroupClickListener to catch the event both when expanding or collapsing
link to developer site:
https://developer.android.com/reference/android/widget/ExpandableListView.html

Stuck... need help.. Listview w/ Array Adapter

Ok, so I have this application that takes a adress strings (from values/xml) and parses it your current position) retuning a name, address and distance away. This is then picked up by an arrayadapter and put up on a listview. I cannot for the life of me get the list view to accept an onitemclick to start another activity, where I can launch a different view. I did have it where I was getting the row, name and address to show through to an alert dialog, but in my efforts to get it to launch an activity, I lost that.
So does anyone have any thoughts? I am using the following call to make my list and and arrays. This is stripped down, so assume I have all the imports and proper formatting. I know I am just missing something simple here...
public class Wf extends ListActivity {
private ArrayList<String> DistanceList;
private ArrayAdapter<String> aa;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Bind the ListView to an ArrayList of strings.
DistanceList = new ArrayList<String>();
ListView lv = (ListView)findViewById(R.id.ListView01);
aa = new ArrayAdapter<String>(getApplicationContext(),
R.layout.listbox_layout,
DistanceList);
lv.setAdapter(aa);
//Call to get distance... here
}
public void onListItemClick(ListView parent, View v,int position, long id) {
ListView lv = (ListView)findViewById(R.id.ListView01);
Toast.makeText(this, "You clicked", Toast.LENGTH_LONG).show();
}
From the ListActivity docs:
ListActivity has a default layout that
consists of a single, full-screen list
in the center of the screen. However,
if you desire, you can customize the
screen layout by setting your own view
layout with setContentView() in
onCreate(). To do this, your own view
MUST contain a ListView object with
the id "#android:id/list" (or list if
it's in code)
Your ListView does not have the correct ID. Your code is incomplete but I suspect the listener is not being registered with the ListView.

Changing Java ListView to XML

Alright, so I followed a tutorial on the Android website, and I got a ListView going in my application. But, the example they had did everything in Java basically. How could I transform the following code to XML?
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
I know I can't make the click listener XML, I just put that there for example.
You don't really need to change anything to XML in that particular piece of code. ListActivity will use a full-screen ListView if there is no call to setContentView(). Here is an example of a ListActivity with a custom layout, if that is what you are interested in.
Also, please get rid of getApplicationContext(). Just use this to reference the Activity, which itself is a Context.

Categories

Resources