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

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.

Related

How to show JSON data in fragments?

I have four swipeable fragments on an activity in tabbed view mode.Each fragment should show JSON data from an API.
There are two problems
The first problem is that only in first fragment I am able to get the data.
And the second problem is that when swiping to next fragment I lost the data received in the first fragment.
I have four layouts for each of the fragment.
I am using custom listview to show the data.
*After creating fragment and getting theJSON data I am using custom list view to show it in the first fragment using aeroFirst method as below but I am not able to do the same in the other fragment. Creating fragments,JSON parsing and aeroFirst method are all in MainActivityclass *
snip of the code
private void aeroFirst(List<Details> mList) {
// layout = (LinearLayout) findViewById(R.id.linear);
for (Details bean : details) {
final String urlChar = bean.getUrl();
if (bean.getResType().equals("Videos"))
{
ListView listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, mList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + urlChar, Toast.LENGTH_SHORT)
.show();
There must be an activity behind all four fragments, right? Store the values in that activity so that all four fragments can access the JSONs. This might not be the best in style, but the other solution involves saving it into shared preference or similar approach.
Another way to do this is to load everything in the activity, and then put the JSONs into arguments as bundles for each fragment.

ListView Selected position with setTextFilterEnabled

I have the following code that starts a listview with 100 options. Also it activates the software keyboard, for easy-searching of the options. If i choose an option, the position of the selected option stores in the string array items[]. The problem is that when i search an option by keyword, select it and then go back, it points at the current position not at the current value.
How to prevent this?
public class MyListActivity extends ListActivity {
String[] items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ListView lView = getListView();
lView.setChoiceMode(2);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
lView.setTextFilterEnabled(true);
items = getResources().getStringArray(R.array.items1);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, items));
}
public void onListItemClick(ListView lv, View v, int position, long id) {
lv.setItemChecked(position, lv.isItemChecked(position));
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
}
I think that you should create your own custom Adapter , and then in that adapter just simply override the getItem(int position) function to return the value that you need. If u haven't done it before then check out for instance this tutorial
As it turns out, having a filterable adapter doesn't play well with any choice mode (single or multiple). The only solution to this is to manually maintain the state of the selected items (a set or a list of strings in your case), and then simply loop through it every time you change the filter, in order to select the items you need. This has already been described in great detail here: Multiple Choice Searchable ListView
You obviously can't use the setTextFilterEnabled() method for this, you simply need to add an EditText in your layout and then set a TextWatcher on that in order to get all the necessary callbacks.

Spinner in ActionBar is not updating ListView

I have two elements:
Spinner INSIDE THE ACTIONBAR (HoloEverywhere (support.v7)) of my Activity set with an onItemSelectedListener().
Listview in my Activity which is filled with an ArrayAdapter
When I choose an Item from the Spinner, the ListView should be updated. But somehow there is a problem with my Spinner I guess. The ListView first updates when I clicked in a TextField of my Activity after I choose a SpinnerItem.
My code:
// Get a databaseConnection and fills the history variable with the data of
// the selected spinnerItem "history" is right filled with all Strings for
// the chosen Spinner from the database -> works correct
public void updateHistory() {
sqlLightDatabase database = new sqlLightDatabase(this);
ArrayList<String> history = database.getArrayList(
mySpinner.getSelectedItem().toString());
// set the adapter to the ListView
ArrayAdapter<String> listenAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, history);
myListView.setAdapter(listenAdapter);
// the code works through, but the ListView is not shown the update
// instantly. But now, when I click a TextField in my Activity, myListView
// shows the new data
}
// Is instantly called when an Item is selected in mySpinner (in my ActionBar)
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id)
{
// the updateHistory function is called but myLitstView doesn't get
// updated with new Data
updateHistory();
//Just for testing. The following is working instantly fine.
drivenDistance_TF.setText("History changed");
}
I tried the last two days to fix this issue, but I have absolute no idea what I could else try to get the ListView instantly updated.
There is no ErrorLog, because there are no errors.
You must call notifyDataSetChanged method to update data in list view
So write this line
listenAdapter.notifyDataSetChanged();
after myListView.setAdapter(listenAdapter);
in updateHistory();

Get reference to an object within a ListView

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);

Android Context menu from listsview

I am new to android development, and not very good at programming in general but, I am working on a tab layout that has a listview per tab. Each tab has it's own java file. I am currently trying to add a context menu that when clicked (not long clicked) on an item in my listview, will bring up a menu so I can choose an option. Right now it just shows a toast displaying the name of the item I clicked. The listview options are currently added to the list via local string declaration, here is an example of on of my tabs:
public class AlbumTab extends ListActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
final String[] CDExplorer_tabs = new String[] {"Client Heirarchy", "Territory", "Sales Credit", "Admin", "General Search"};
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, CDExplorer_tabs));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
would I have to make another string array for each menu I want to popup and somehow connect it to the other string? Or do if statements that decide on which menu to popup based on which listview item is clicked?
First, you if you want each part of the list to have a different menu, make a switch statement in your onItemClick() that depends on your position. switch(position){ //cases and such
Then what you might want to try instead of having a nested ListView is try doing an AlertDialog. Search for adding a list. And then implementing that for each position on your list. Then make sure that you implement another switch inside of the DialogInterface onClick function to call a function to do what you what it to do from the list.//Also make sure to alert.show();.
That would be my recommendation if you want to do it. However, if you want to do nested listviews, its more complicated but possible.
What you need to is implement a custom list adapter for you list. And create a view holder for each that holds a another listview. A good example for that is here.
That should pretty much do it. Mind you that if you decide to do the nested listviews it will be very crowded inside that listview.

Categories

Resources