Constructor parameter "this" working from activity but from fragment - java

I used a constructor like this:
public class CustomList extends ArrayAdapter<String> {
private String[] ids;
private String[] names;
private String[] urls;
private String[] fileTypes;
private String[] date_times;
private Activity context;
public CustomList(Activity context, String[] ids, String[] names, String[] str_url,String[] fileType ,String[] dateTime){
super(context, R.layout.list_view_layout, ids);
this.context = context;
this.ids = ids;
this.names = names;
this.urls = str_url;
this.fileTypes = fileType;
this.date_times = dateTime;
}
And able to use this class from main activity like this
CustomList cl = new CustomList(this, ParseJSON.ids,ParseJSON.names,ParseJSON.url,ParseJSON.file_type,ParseJSON.uploaded_at);
But whenever I am trying to pass the first parameter of constructor "this" error message showing. So what will be the first parameter whenever using that calass from a fragment
This is actually what I have done in a fragment
if (arrayListPollingSite.size() > 0) {
int pollingSiteCount = arrayListPollingSite.size();
pollingSiteNameArray = new String[pollingSiteCount];
HashMap<String, String> pollDataMap = new HashMap<String, String>();
for (int i = 0; i < pollingSiteCount; i++) {
pollDataMap = arrayListPollingSite.get(i);
pollingSiteNameArray[i] = pollDataMap.get(i);
// this line showing error
CustomList c1=new CustomList(getActivity(),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID));
}
// listView.setAdapter(cl);
}

'this' means always the current context.
the fragment's context and the activity's context are different.
so you should give the activity's context to the constructor: try 'getActivity()'

You can call this from the Activity class.
But in a fragment you must call the activity that contain this fragment with getActivity(), like this:
CustomList cl = new CustomList(getActivity(), ParseJSON.ids,
ParseJSON.names,ParseJSON.url,ParseJSON.file_type,
ParseJSON.uploaded_at);
Harunduet said:
I have tried according to the following way;
pollingSiteNameArray = new String[pollingSiteCount];
CustomList c1=new CustomList(getActivity(),pollingSiteNameArray[1],pollingSiteNameArray[2],pollingSiteNameArray[3],pollingSiteNameArray[4],pollingSiteNameArray[5]);
it show error
Harunduet said:
if (arrayListPollingSite.size() > 0) {
int pollingSiteCount = arrayListPollingSite.size();
pollingSiteNameArray = new String[pollingSiteCount];
HashMap<String, String> pollDataMap = new HashMap<String, String>();
for (int i = 0; i < pollingSiteCount; i++) {
pollDataMap = arrayListPollingSite.get(i);
pollingSiteNameArray[i] = pollDataMap.get(i);
// this line showing error
CustomList c1=new CustomList(getActivity(),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID));
}
// listView.setAdapter(cl);
}
Where are you exe this code? Try to move to onActivityCreated method, in your fragment.

try this
CustomList cl = new CustomList(getActivity(), ParseJSON.ids,ParseJSON.names,ParseJSON.url,ParseJSON.file_type,ParseJSON.uploaded_at);

Related

how to pass interface in fragment throw adapter?

In Home Fragment
userListAdapter = new UserListAdapter(getActivity(), response.getTray(), ?);
RVUserList.setAdapter(userListAdapter);
in Adapter
public UserListAdapter(Context context, ArrayList<TrayModel> list, UserListInterface listInterface) {
this.context = context;
this.trayModelArrayList = list;
this.userListInterface = listInterface;
}
and also implement UserListInterface in fragment
userListAdapter = new UserListAdapter(getActivity(), response.getTray(), new UserListInterface(){
// interface methods
});
RVUserList.setAdapter(userListAdapter);
userListAdapter = new UserListAdapter(getActivity(), response.getTray(), HomeFragment.this::userListClick);
RVUserList.setAdapter(userListAdapter);

ListView doesn't refresh when I delete something

I have an custom adapter that is giving me problem. When I add or edit something it updates the listview but when I delete it doesn't.
Code when I remove something:
private ArrayList<Teams> m_orders = null;
private TeamsAdapter m_adapter;
private ListView lstv;
...
private void deleteTeam(int indexRemove){
hasKeys.remove(hasKeys.indexOf(m_orders.get(indexRemove).getTeamName()));
Menu.teams.remove(m_orders.get(indexRemove).getTeamName());
m_orders.remove(indexRemove);
m_adapter.notifyDataSetChanged();
}
I tried use a Runnable, but without success.
private Runnable returnRes = new Runnable() {
#Override
public void run() {
if(m_orders != null && m_orders.size() > 0){
m_adapter.notifyDataSetChanged();
for(int i=0;i<m_orders.size();i++)
m_adapter.add(m_orders.get(i));
}
m_adapter.notifyDataSetChanged();
}
};
My onCreate method:
lstv = findViewById(R.id.teamsList);
m_orders = new ArrayList<Teams>();
this.m_adapter = new TeamsAdapter(this, R.layout.row, m_orders);
lstv.setAdapter(this.m_adapter);
You have to remove an item from the ArrayList which is inside the Adapter class. So you need to write your delete function inside the adapter class. In this way will able to delete items and update listview by calling notifyDataSetChanged() method.
You are creating an m_adapter with m_orders dataset, but you are removing item from m_teams dataset, make no sense, they are different instances.
final List<Teams> mTeams = new ArrayList<>();
TeamsAdapter mAdapter;
onCreate() {
mAdapter = new TeamsAdapter(this, R.layout.row, mTeams);
ListView listView = (ListView) findViewById(R.id.teamsList);
listView.setAdapter(mAdapter);
}
addTeams(Collection<Teams> items) {
mTeams.addAll(items);
mAdapter.notifyDataSetChanged();
}
deleteTeam(int index) {
mTeams.remove(index);
mAdapter.notifyDataSetChanged();
}

Android: FastScrolling SectionIndexer getSections() is called only once

I've created a ListView which is using FastScroll. (see pic) When the user clicks any of the below Button (viz. All Tracks, Artists, Album), everytime the following custom ArrayAdater is called
ArrayAdapter<String> adapter = new ScrollIndexListAdapter(Listing.this, elements);
//Code for ScrollIndexListAdapter is below
and the same ListView is updated.
PROBLEM: According to my investigation in Android, the getSections() method is called only once (i.e. only when the first time ScrollIndexListAdapter is called).
This time the sections are populated & the fastScrolling works perfectly.
But when I update the ListView by clicking on Artists/Album, the getSections() method is not called. So the older sections are used, and the FastScrolling still shows previews of old alphabets.
So, how can I make sections get updated everytime when the ListView is updated?
There is a setSections() method, but I'm not able to find how to use it.
Code for ScrollIndexListAdapter class:
public class ScrollIndexListAdapter extends ArrayAdapter implements
SectionIndexer {
// Variables for SectionIndexer List Fast Scrolling
HashMap<String, Integer> alphaIndexer;
String[] sections;
private static ArrayList<String> list = new ArrayList<String>();
public ScrollIndexListAdapter(Context context, ArrayList<String> list) {
super(context, android.R.layout.simple_list_item_1, android.R.id.text1,
list);
this.list.clear();
this.list.addAll(list);
/*
* Setting SectionIndexer
*/
alphaIndexer = new HashMap<String, Integer>();
int size = list.size();
for (int x = 0; x < size; x++) {
String s = (String) list.get(x);
// Get the first character of the track
String ch = s.substring(0, 1);
// convert to uppercase otherwise lowercase a -z will be sorted
// after upper A-Z
ch = ch.toUpperCase();
if (!alphaIndexer.containsKey(ch)) {
alphaIndexer.put(ch, x);
}
}
Set<String> sectionLetters = alphaIndexer.keySet();
// create a list from the set to sort
ArrayList<String> sectionList = new ArrayList<String>(
sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
sectionList.toArray(sections);
}
/*
* Methods for AphhabelIndexer for List Fast Scrolling
*/
#Override
public int getPositionForSection(int section) {
String letter = (String) sections[section];
return alphaIndexer.get(letter);
}
#Override
public int getSectionForPosition(int position) {
String letter = (String) sections[position];
return alphaIndexer.get(letter);
}
#Override
public Object[] getSections() {
return sections;
}
}
It seems that latest FastScroll version doesn't have that problem. But, in your case, there's a turn around. When setting the adapter to the ListView, disable and enable the fast scrolling. See the code below:
adapter = new ScrollIndexListAdapter(this, data);
listView.setFastScrollEnabled(false);
listView.setAdapter(adapter);
listView.setFastScrollEnabled(true);

Using Cursor with AsyncTask

I'm trying to download images for each artist that has music on my phone, then show these images in a GridView. I'm using the lastfm-java library that Last.fm recommends using. The method you call to fetch an artists image is getImageURL(ImageSize size), but before you do this, you need to tell it which artist you want to reference with a String parameter. So, in full it would be something like this:
#Override
protected String doInBackground(Object... arg0) {
Artist artist = Artist.getInfo(artistOrMbid, LASTFM_API_KEY);
return artist.getImageURL(ImageSize.EXTRALARGE);
}
Getting all the artists that are on my phone isn't a problem, you just reference MediaStore. You would do something like this:
private void getArtists() {
String[] projection = new String[] {
MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST,
};
String sortOrder = MediaStore.Audio.Artists.DEFAULT_SORT_ORDER;
Cursor c = getActivity().getContentResolver().query(
MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, projection, null, null, sortOrder);
if (c != null) {
int count = c.getCount();
if (count > 0) {
final int ARTIST_IDX = c.getColumnIndex(MediaStore.Audio.Artists.ARTIST);
for (int i = 0; i < count; i++) {
c.moveToPosition(i);
}
}
c.close();
c = null;
}
}
The Adapter for my GridView isn't anything special, it simply extends BaseAdapter.
Note AQuery is a library I'm using that helps cache and load a Bitmap from a URL.
public class GridViewAdapter extends BaseAdapter {
private final String[] imageURLs;
private final LayoutInflater mInflater;
private final Activity mActivity;
public GridViewAdapter(String[] urls, Activity activity) {
imageURLs = urls;
mActivity = activity;
mInflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return imageURLs.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder = null;
// Inflate GridView items
if (convertView == null) {
convertView = mInflater.inflate(R.layout.gridview_items, null);
viewholder = new ViewHolder();
viewholder.mImage = (ImageView)convertView.findViewById(R.id.gridview_image);
convertView.setTag(viewholder);
} else {
viewholder = (ViewHolder)convertView.getTag();
}
AQuery aq = new AQuery(convertView);
aq.id(viewholder.mImage).image(imageURLs[position], false, false, 0, 0, null, 0, 0.75f);
return convertView;
}
}
class ViewHolder {
public ImageView mImage;
}
So in full, my AsyncTask is as follows:
public class LastfmArtistGetImageURL extends AsyncTask<Object, Integer, String[]> implements
Constants {
private static final String tag = LastfmArtistGetImageURL.class.getSimpleName();
private GridViewAdapter mGridAdapter;
// Test
private final String[] imageIds = {
"http://userserve-ak.last.fm/serve/252/71875544.png",
"http://userserve-ak.last.fm/serve/252/6258507.jpg",
"http://userserve-ak.last.fm/serve/252/51274303.png",
"http://userserve-ak.last.fm/serve/252/58672183.png",
"http://userserve-ak.last.fm/serve/252/72029714.png",
"http://userserve-ak.last.fm/serve/252/17666215.jpg",
"http://userserve-ak.last.fm/serve/252/63247381.png",
"http://userserve-ak.last.fm/serve/252/33665463.jpg"
};
private final String artistOrMbid;
private final GridView mGridView;
private final Activity mActivity;
public LastfmArtistGetImageURL(String name, GridView gv, Activity activity) {
artistOrMbid = name;
mGridView = gv;
mActivity = activity;
}
#Override
protected String[] doInBackground(Object... arg0) {
Artist artist = Artist.getInfo(artistOrMbid, LASTFM_API_KEY);
Collection<String> col = new ArrayList<String>();
col.add(artist.getImageURL(ImageSize.EXTRALARGE));
return col.toArray(new String[0]);
}
#Override
protected void onPostExecute(String[] result) {
if (result != null)
mGridAdapter = new GridViewAdapter(imageIds, mActivity);
mGridView.setAdapter(mGridAdapter);
super.onPostExecute(result);
}
}
When I call my AsyncTask, I call it in my getArtists() method like this:
new LastfmArtistGetImageURL(c.getString(ARTIST_IDX), mGridView, getActivity())
.execute();
Problem
When I call this, all of the artists images download, but they download one after the other at position 0 of my GridViewAdapter. In other words, one image loads, then next, and so on all in the first position when I need them to be placed into each available position in the GridView. When I return my test String[] in my AsyncTask everything works like it should. All of the images are placed in order in each available space in the GridView.
Question
My question is, how do I return each artist image I download into my GridView correctly and why are the images currently only being loaded at the first position in my GridViewAdapter?
Edit - Shubhayu's answer
I moved setting my GridViewAdapter into my getArtists() method like so. This results in all the images being downloaded (As says LogCat), but only the last one being set in my GridView.
String[] test = new LastfmArtistGetImageURL(c.getString(ARTIST_IDX),
mGridView, getActivity()).execute().get();
mGridAdapter = new GridViewAdapter(test, getActivity());
mGridView.setAdapter(mGridAdapter);
smoak's answer
This results in only the last artist image (by the default order) being downloaded and applied in my GridView.
String[] test = {c.getString(ARTIST_IDX)};
new LastfmArtistGetImageURL(test, mGridView, getActivity()).execute();
Your AsyncTask looks like you are executing it each time for each Artist. Thus, your AsyncTask returns only one Artist's image and your GridView gets that Artists image, then you run the AsyncTask for the next Artist, GridView gets updated with new image and so on. What you need to do is modify your AsyncTask to take a String array of Artist names and loop over them in the doInBackground to get their image's.
// ... SNIPPED
public LastfmArtistGetImageURL(String[] names, GridView gv, Activity activity) {
artistsOrMbids = names;
mGridView = gv;
mActivity = activity;
}
#Override
protected String[] doInBackground(Object... arg0) {
Collection<String> col = new ArrayList<String>();
for (String nameOrMbid : this.artistsOrMbids) {
Artist artist = Artist.getInfo(artistOrMbid, LASTFM_API_KEY);
col.add(artist.getImageURL(ImageSize.EXTRALARGE));
}
return col.toArray(new String[0]);
}
// .... SNIPPED
And pass in all the artist names:
String[] artists = { "The Black Keys", "Rush", "The Allman Brothers" };
new LastfmArtistGetImageURL(artists, mGridView, getActivity()).execute();
here's what is happening, when you pass the test string it has a list of images and hence the gridview shows them properly. but when you use it to download an image for each artist, things go wrong.
Every time you call
new LastfmArtistGetImageURL(c.getString(ARTIST_IDX), mGridView, getActivity()).execute();
it runs the doInBackground(), completes it and then immediately calls the onPostExecute() where it creates a new adapter and passes your result which basically contains a single image of the single call.
So what u need to do is in your asynctask download all the images and then create a single adapter and pass all the images to it. That is not happening currently.
EDIT
If you see the AsyncTask, you will realize that everytime you call it, the string array returns only one image. So instead of returning a string array, return a string.
Next, I would suggest you use an ArrayList in your Adapter instead of a String array.
In your getArtists(), create an ArrayList and everytime you call
new LastfmArtistGetImageURL(test, mGridView, getActivity()).execute();
add the result to your ArrayList. Once you have looped through all the artists, your ArrayList will contain all the images.
Now set it to the Adapter. (You would have t change the adapter a bit if you change it from string to arraylist.)

Array Adapter NotifyDataSetChanged() NullPointerException Android

I am adding a map to a list and then refreshing the array adapter. This was working perfectly earlier, but now that I am using an addItem() method from two different methods, it throws a NullPointer. I hope my code will clear up what I am saying:
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
String[] from;
String painLevelString, timeOfPainString, textTreatmentString,
painLocation, row1, row2, name;
OnCreate(){
if(getIntent().getStringExtra("newPainLevel")!= null){
createNewEditedEntry();
}
adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
from, to);
listthings.setAdapter(adapter);
}
private void createNewEditedEntry() {
String newPainLevel = this.getIntent().getStringExtra("newPainLevel");
String newPainTime =this.getIntent().getStringExtra("newPainTime");
String newTreatment =this.getIntent().getStringExtra("newTreatment");
painLevelString = newPainLevel;
timeOfPainString = newPainTime;
textTreatmentString = newTreatment;
row1 = "sample1";
row2 = "sample2";
addItem();
//painItems.remove(getIntent().getStringExtra("position"));
//adapter.notifyDataSetChanged();
}
#Override
// on the activityresult,get the string extra, then add the item to the list
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1) {
row1 = data.getStringExtra("com.painLogger.row1");
row2 = data.getStringExtra("com.painLogger.row2");
painLevelString = data.getStringExtra("com.painLogger.painLevel");
painLocation = data.getStringExtra("painLocation");
timeOfPainString = data.getStringExtra("com.painLogger.painTime");
textTreatmentString = data
.getStringExtra("com.painLogger.treatment");
addItem();
}
}
// to add the item, put it in the map, and add the map into the list
private void addItem() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("row_1", row1);
map.put("row_2", row2);
map.put("row_3", painLevelString);
map.put("row_4", painLocation);
map.put("row_5", timeOfPainString);
map.put("row_6",textTreatmentString);
painItems.add(map);
adapter.notifyDataSetChanged(); //Null Pointer **HERE**
}
Just to be clear, createNewEditedEntry() and onActivityResult() are never called at the same time and never clash. They are two completely different occurrences. It was working with just the OnActivityResult, but now when I use CreateNewEditedEntry(), it has stopped working. I have also checked and made sure that none of the Strings that I fetch from my intent are null.
You need to initialize adapter before you call createNewEditedEntry in onCreate:
OnCreate(){
adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout, from, to);
if(getIntent().getStringExtra("newPainLevel")!= null){
createNewEditedEntry();
}
// ...
}
Otherwise, adapter will be null in addItem called from there (last line of createNewEditedEntry).
You should also be aware that your call to addItem can fail if your activity was destroyed by Android while you were in the sub-activity. Have a read of my answer to this question to understand what can happen when resources get low.

Categories

Resources