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.
Related
I have different layout xml for listview and header.
I have pointed to listview layout file in my fragment page and declare setItemOnClickListener on that listview to get the data display when clicked on its' item.
However, when I clicked the child items, they are not working and they working when I clicked on header section.
Can someone tell me what is probably goes wrong?
This is my code which I believe is the most basic one. The different will be just in fragment compare to normal activity pages.
ListView lv = (ListView) myView.findViewById(R.id.listview);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getActivity(), "This is my Toast message!",Toast.LENGTH_LONG).show();
}
});
Up front: This is my first attempt at an Android app. I'm in that strange place of not knowing what to search for to find the answer to my question.
What I have accomplished is:
Created a custom class myCustomClass with properties of 'title' and 'youTubeUrl'
Created an ArrayList<myCustomClass>
Added multiple elements to ArrayList<myCustomClass>
Created a custom ArrayAdapter and attached it to the the arraylist.
Added an onItemClickListener to the custom ArrayAdapter.
All of that works good. I would like to show the title in the ListView and then when the user clicks the list view item, I'd like to get a reference to the youtubeUrl property.
Here's what I have for the adapter code:
MyListAdapter myListAdapter = new MyListAdapter(this, R.layout.my_list, elements);
myList.setAdapter(myListAdapter);
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
myListAdapter.notifyDataSetChanged();
Thanks for your help.
You can use the position property in onItemClick to go back to your data source and find the relevant item. From there you should be able to retrieve the Url.
As another poster implied, it depends on what you are using in your adapter. Assuming it's MyCustomClass. You can do something like this in your onItemClick method:
MyCustomClass selection = (MyCustomClass) getListView().getItemAtPosition(position);
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
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().
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.