Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
3.01 Add toast on item click
Browse files Browse the repository at this point in the history
  • Loading branch information
sdspikes authored and Lyla committed Mar 4, 2015
1 parent 2141b9c commit bd57648
Showing 1 changed file with 10 additions and 0 deletions.
Expand Up @@ -27,8 +27,10 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -112,6 +114,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Get a reference to the ListView, and attach this adapter to it.
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String forecast = mForecastAdapter.getItem(position);

This comment has been minimized.

Copy link
@yevgenko

yevgenko Apr 4, 2015

Why don't we use adapterView here?

String forecast = (String) adapterView.getItemAtPosition(position);

Or am I missing anything?

Thank you in advance.

This comment has been minimized.

Copy link
@tolbxela

tolbxela Jun 24, 2015

Sure, it works also. But then you have to cast the result to String.
You can also get the item direct from the ArrayAdapter:

String forecast = (String)adapterView.getAdapter().getItem(position);

But you still need to cast the result in String.
The original version looks optimal for me in this case.

This comment has been minimized.

Copy link
@yevgenko

yevgenko Jun 26, 2015

You're right, in terms of type casting this one is optimal. My objections was more about design consideration, i.e. accessing outer environment directly within anonymous class leads to the extra coupling between components which I'd prefer to avoid :)

This comment has been minimized.

Copy link
@tolbxela

tolbxela Jun 26, 2015

Yes, you are right. It's a good point!

Toast.makeText(getActivity(), forecast, Toast.LENGTH_SHORT).show();
}
});

return rootView;
}
Expand Down

0 comments on commit bd57648

Please sign in to comment.