How to show JSON data in fragments? - android

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.

Related

Fetching List Item Click

I have 2 activity in my app.
From First activity I take data to second activity's listview. In second activity I have a listview to show datas. Normally when I create a list and values myself I can create click event but now datas coming from other activity, and the datas uncertain.. How can I create click event for coming datas?(sorry for my very bad grammar)
It's my first activity;
Intent veri = new Intent(elementler.this,sonuclar.class);
veri.putStringArrayListExtra("logoveri", clickeddata);
startActivity(veri);
//I sent data for second activity's list
It's my list class;
//fetch values
Intent veri = getIntent();
veriler = veri.getStringArrayListExtra("logoveri");
//there are a lot of possibility in "veriler" maybe user select banana maybe apple maybe car maybe computer we can not know what he select..
//create a list with fetch data
ListView sonuclistesi=(ListView) findViewById(R.id.sonuclistesi);
ArrayAdapter<String> veriadaptoru=new Listeozellikleri(this,veriler);
sonuclistesi.setAdapter(veriadaptoru);
You can give listView onitem click,
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// do your stuff
}
});

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

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.

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