I have a little UI problem with my ViewPager.
My ViewPager is the center of my UI.
I have a NavigationView with 4 items and a Toolbar with 2 items.
Each time an item of the NavigationView is clicked, I replace the adapter. Therefore I have 4 adapters and only one is causing problems : BrowseAdapter (code is below).
This adapter is filled with a list of Events and provide a Fragment for each Event.
Something good to know is that whatever the size of the eventList is, the setAdapter method of the ViewPager takes at least 150ms to execute, whereas for the other Adapters, it takes only 20 to 50ms to load.
I have tried to put all calls to setAdapter in the View.post(new Runnable()); method and in an AsyncTask<Void, Void, Void> :
The post method froze the UI
The AsyncTask changed nothing because I can only call setAdapter on the UI Thread (in the postExecute method) which is basically the same as calling it without the AsyncTask.
I think this is basically an optimization problem but I can't see where is the problem.
Thank you for you help.
Here is the code in question :
MainActivity :
/*
.
. Import stuff
.
.
.*/
public class MainActivity extends AppCompatActivity implements RequestCallback,
ConnectionCallbacks, OnConnectionFailedListener, FBLoginChanged, LocationListener {
/**
* The main content view
*/
private CustomViewPager mViewPager;
/**
* Adapters : One for each feature of the application
*/
private BrowseAdapter mBrowseAdapter;
private CurrentAdapter mCurrentAdapter = new CurrentAdapter(getFragmentManager());
private CalendarAdapter mCalendarAdapter = new CalendarAdapter(getFragmentManager());
private SettingsAdapter mSettingsAdapter = new SettingsAdapter(getFragmentManager());
/**
* The action bar of the application
*/
private Toolbar mToolbar;
/**
* TabLayout provide the new Material Design tab navigation
*/
private TabLayout mTabLayout;
/**
* Side navigation given by a NavigationView instead of a NavigationDrawer
* to support Material Design
*/
private NavigationView mNavigationView;
private DrawerLayout mDrawerLayout;
/**
* List of all events.
*/
private ArrayList<Event> mEventList = new ArrayList<Event>();
/**
* Provide different way of browsing through the events
*/
private enum BrowseType {
DEFAULT,
POPULAR,
RANDOM
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*.
.
. Initialization stuff
.
.*/
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Closing drawer on item click
mDrawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
case R.id.home:
mTabLayout.setVisibility(View.VISIBLE);
mBrowseAdapter.setBrowsingType(BrowseType.DEFAULT);
mViewPager.setAdapter(mBrowseAdapter);
break;
case R.id.favs:
mTabLayout.setVisibility(View.GONE);
mViewPager.setAdapter(mCurrentAdapter);
break;
case R.id.calendar:
mTabLayout.setVisibility(View.GONE);
mViewPager.setAdapter(mCalendarAdapter);
break;
case R.id.setting:
mTabLayout.setVisibility(View.GONE);
mViewPager.setAdapter(mSettingsAdapter);
break;
default:
break;
}
return true;
}
});
mViewPager = (CustomViewPager) findViewById(R.id.pager);
mBrowseAdapter = new BrowseAdapter(this.getFragmentManager(), mEventList);
mTabLayout.setOnTabSelectedListener(new OnTabSelectedListener() {
#Override
// Unused
public void onTabReselected(Tab tab) {
}
#Override
public void onTabSelected(Tab tab) {
switch(tab.getPosition()) {
case 0:
mBrowseAdapter.setBrowsingType(BrowseType.DEFAULT);
break;
case 1:
mBrowseAdapter.setBrowsingType(BrowseType.POPULAR);
break;
default: // Unused
break;
}
mViewPager.setAdapter(mBrowseAdapter);
}
#Override
// Unused
public void onTabUnselected(Tab tab) {
}
});
mViewPager.setAdapter(mBrowseAdapter);
}
}
My adapter :
private class BrowseAdapter extends FragmentStatePagerAdapter {
private ArrayList<Event> eventList = new ArrayList<Event>();
private BrowseType browseType = BrowseType.DEFAULT;
private HashMap<Integer, EventFragment> fragmentReferenceMap = new HashMap<Integer, EventFragment>();
public BrowseAdapter(FragmentManager fragmentManager,
ArrayList<Event> mEventList) {
super(fragmentManager);
}
public void setBrowsingType(BrowseType type) {
this.browseType = type;
this.commitChanges();
}
public Event getEventById(int id) {
for(Event event : eventList) {
if(event.getId() == id)
return event;
}
return null;
}
public void setJSONData(JSONArray jsonArray) {
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject obj = jsonArray.getJSONObject(i);
Event event = new Event();
event.setId(obj.getInt("id"));
event.setName(obj.getString("name"));
event.setShort_description(obj.getString("short_desc"));
event.setLong_description(obj.getString("long_desc"));
event.setPlace(obj.getString("place").split(";")[0]);
Location loc = new Location("");
loc.setLatitude(Double.valueOf(obj.getString("place")
.split(";")[1]));
loc.setLongitude(Double.valueOf(obj.getString("place")
.split(";")[2]));
event.setLocation(loc);
event.setDate(obj.getString("date"));
eventList.add(event);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public String getJSONData() {
JSONArray toReturn = new JSONArray();
for (Event event : eventList) {
JSONObject tmp = new JSONObject();
try {
tmp.put("name", event.getName());
tmp.put("short_desc", event.getShort_description());
tmp.put("long_desc", event.getLong_description());
tmp.put("id", event.getId());
tmp.put("date", event.getDate());
tmp.put("place",
event.getPlace()
+ ";"
+ String.valueOf(event.getLocation()
.getLatitude())
+ ";"
+ String.valueOf(event.getLocation()
.getLongitude()));
toReturn.put(tmp);
} catch (JSONException e) {
e.printStackTrace();
}
}
return toReturn.toString();
}
public boolean addItem(Event item) {
return eventList.add(item);
}
#SuppressWarnings("unused")
public Event removeAt(int position) {
return eventList.remove(position);
}
public void commitChanges() {
this.sort();
this.notifyDataSetChanged();
}
private void sort() {
Log.d("SORT", browseType.name());
Collections.sort(eventList, new Comparator<Event>() {
#Override
public int compare(Event arg0, Event arg1) {
float dis1 = arg0.getLocation().distanceTo(
LocationProvidor.getInstance().getLastLocation());
float dis2 = arg1.getLocation().distanceTo(
LocationProvidor.getInstance().getLastLocation());
int userNumber1 = Integer.parseInt(arg0.getUserNumber());
int userNumber2 = Integer.parseInt(arg1.getUserNumber());
switch(browseType) {
case DEFAULT:
return (int) (dis1 - dis2);
case POPULAR:
return userNumber2 - userNumber1;
case RANDOM:
return new Random().nextInt();
default:
return 0;
}
}
});
}
public void empty() {
eventList.clear();
}
#Override
public EventFragment getItem(int position) {
EventFragment frag = EventFragment.newInstance(eventList.get(position));
frag.setIsCurrents(mCurrentAdapter.containsEventId(eventList.get(position).getId()));
fragmentReferenceMap.put(position, frag);
return frag;
}
#Override
public void destroyItem(View container, int position, Object object) {
super.destroyItem(container, position, object);
fragmentReferenceMap.remove(position);
Log.d("fr.blopper.app", "destroy " + position);
}
public EventFragment getFragment(int position) {
return fragmentReferenceMap.get(position);
}
#Override
public int getCount() {
return eventList.size();
}
}
And CustomViewPager (I have created it only to measure the time taken by setAdapter):
import android.content.Context;
import android.util.AttributeSet;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.support.v4.view.PagerAdapter;
public class CustomViewPager extends ViewPager {
public CustomViewPager(Context c) {
super(c);
}
public CustomViewPager(Context c, AttributeSet as) {
super(c, as);
}
#Override
public void setAdapter(PagerAdapter adapter) {
long start = System.currentTimeMillis();
super.setAdapter(adapter);
Log.d("TAG", "Custom time " + (System.currentTimeMillis() - start));
}
}
Found the answer, it has nothing to do with the ViewPager.
The Fragments provided by the Adapter were making a call to a Google API which was slow to answer.
Related
I am a newbie in Android developing and i try to understand how to use callbacks in interfaces.
I am use "Color-pick-preference" library in my app. I need it to change different settings, for example background color. And i want to change in runtime, without refresh activities.
This is how i do it now:
Color-picker-preference have three main classes:
ColorPickerView (as I understand it show color panel and send user choise via callback to ColorPickerDialog)
ColorPickerDialog (this class show preferebce dialog window includes color panel, submit color button etc. And send user choise data further in ColorPickerPreference)
and ColorPicerPreference wich save user choise in Shared Preferences.
I described very superficially, if you'll excuse me.
Parts of these three files
ColorPickerView
***
***
private OnColorChangedListener mListener;
***
public interface OnColorChangedListener {
public void onColorChanged(int color);
}
***
#Override
public boolean onTouchEvent(MotionEvent event) {
boolean update = false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mStartTouchPoint = new Point((int) event.getX(), (int) event.getY());
update = moveTrackersIfNeeded(event);
break;
case MotionEvent.ACTION_MOVE:
update = moveTrackersIfNeeded(event);
break;
case MotionEvent.ACTION_UP:
mStartTouchPoint = null;
update = moveTrackersIfNeeded(event);
break;
}
if (update) {
if (mListener != null) {
mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
}
invalidate();
return true;
}
return super.onTouchEvent(event);
}
***
public void setOnColorChangedListener(OnColorChangedListener listener) {
mListener = listener;
}
***
public void setColor(int color, boolean callback) {
int alpha = Color.alpha(color);
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
mAlpha = alpha;
mHue = hsv[0];
mSat = hsv[1];
mVal = hsv[2];
if (callback && mListener != null) {
mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
}
invalidate();
}
ColorPickerDialog
***
public class ColorPickerDialog
extends
Dialog
implements
ColorPickerView.OnColorChangedListener,
View.OnClickListener, ViewTreeObserver.OnGlobalLayoutListener ...
***
private OnColorChangedListener mListener;
***
public interface OnColorChangedListener {
public void onColorChanged(int color);
}
***
private void setUp(int color) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mLayout = inflater.inflate(R.layout.dialog_color_picker, null);
mLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
mOrientation = getContext().getResources().getConfiguration().orientation;
setContentView(mLayout);
setTitle(R.string.dialog_color_picker);
mColorPicker = (ColorPickerView) mLayout.findViewById(R.id.color_picker_view);
mOldColor = (ColorPickerPanelView) mLayout.findViewById(R.id.old_color_panel);
mNewColor = (ColorPickerPanelView) mLayout.findViewById(R.id.new_color_panel);
mHexVal = (EditText) mLayout.findViewById(R.id.hex_val);
mHexVal.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mHexDefaultTextColor = mHexVal.getTextColors();
mHexVal.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
String s = mHexVal.getText().toString();
if (s.length() > 5 || s.length() < 10) {
try {
int c = ColorPickerPreference.convertToColorInt(s.toString());
mColorPicker.setColor(c, true);
mHexVal.setTextColor(mHexDefaultTextColor);
} catch (IllegalArgumentException e) {
mHexVal.setTextColor(Color.RED);
}
} else {
mHexVal.setTextColor(Color.RED);
}
return true;
}
return false;
}
});
((LinearLayout) mOldColor.getParent()).setPadding(
Math.round(mColorPicker.getDrawingOffset()),
0,
Math.round(mColorPicker.getDrawingOffset()),
0
);
mOldColor.setOnClickListener(this);
mNewColor.setOnClickListener(this);
mColorPicker.setOnColorChangedListener(this);
mOldColor.setColor(color);
mColorPicker.setColor(color, true);
}
***
#Override
public void onColorChanged(int color) {
mNewColor.setColor(color);
if (mHexValueEnabled)
updateHexValue(color);
}
***
public void setOnColorChangedListener(OnColorChangedListener listener) {
mListener = listener;
}
ColorPickerPreference
public class ColorPickerPreference
extends
Preference
implements
Preference.OnPreferenceClickListener,
ColorPickerDialog.OnColorChangedListener ...
***
#Override
public void onColorChanged(int color) {
if (isPersistent()) {
persistInt(color);
}
mValue = color;
setPreviewColor();
try {
getOnPreferenceChangeListener().onPreferenceChange(this, color);
} catch (NullPointerException e) {
}
notifyChanged();
}
***
protected void showDialog(Bundle state) {
mDialog = new ColorPickerDialog(getContext(), mValue);
mDialog.setOnColorChangedListener(this);
if (mAlphaSliderEnabled) {
mDialog.setAlphaSliderVisible(true);
}
if (mHexValueEnabled) {
mDialog.setHexValueEnabled(true);
}
if (state != null) {
mDialog.onRestoreInstanceState(state);
}
mDialog.show();
}
And what i try to do in my MainActivity
public class MainActivity extends AppCompatActivity implements
ColorPickerDialog.OnColorChangedListener {
public ColorPickerDialog cpd;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//return super.onOptionsItemSelected(item);
Integer id = item.getItemId();
switch (id) {
case R.id.menu_action_settings:
Intent i = new Intent(this, SettingActivity.class);
startActivity(i);
break;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cpd = new ColorPickerDialog(this,Color.BLACK);
cpd.setOnColorChangedListener(this);
setContentView(R.layout.activity_main);
}
public void onColorChanged(int color) {
RelativeLayout main_scroll_view = (RelativeLayout)findViewById(R.id.activity_main_rel_layout);
System.out.println("AIM ALIVE!");
main_scroll_view.setBackgroundColor(color);
}
}
But when i change color in ColorPickerPreference widget -nothing happens. the background color does not change and my System.out.println does not work ?
Sorry for chaotic presentation and thank you for your answers.
I too myself more of a challenge) Solved the problem by simply adding the onResume method in Mainactivity and check the Shared Preference and then setting the background color in the resulting value.
Where do you declare the action so you could show/open the dialog?
SettingActivity
***
public static class SettingsFragment extends PreferenceFragment {
PrefernceAnimation pa;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String settings = getArguments().getString("settings");
if ("colors".equals(settings)) {
addPreferencesFromResource(R.xml.preference_fragment_colors);
} else if ("sounds".equals(settings)) {
addPreferencesFromResource(R.xml.preference_fragment_sounds);
}
else if ("animation".equals(settings)) {
addPreferencesFromResource(R.xml.preference_fragment_animation);
}
}
}
and preference_headers.xml
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header
android:fragment="com.dosomeweb.mika.babypic.SettingActivity$SettingsFragment"
android:title="#string/pref_main_title_colors"
android:summary="#string/pref_main_summery_colors">
<extra
android:name="settings"
android:value="colors"/>
</header>
<header
android:fragment="com.dosomeweb.mika.babypic.SettingActivity$SettingsFragment"
android:title="#string/pref_main_title_sounds"
android:summary="#string/pref_main_summery_sounds">
<extra
android:name="settings"
android:value="sounds"/>
</header>
<header
android:fragment="com.dosomeweb.mika.babypic.SettingActivity$SettingsFragment"
android:title="#string/pref_main_title_animation"
android:summary="#string/pref_main_summery_animation">
<extra
android:name="settings"
android:value="animation"/>
</header>
just in case prference_fragment_colors.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<net.margaritov.preference.colorpicker.ColorPickerPreference
android:key="pref_bg_color"
android:title="#string/pref_title_layout_bg_color"
android:summary="#string/pref_summary_layout_bg_color"
android:defaultValue="#color/colorAccent"/>
</PreferenceScreen>
Why are you creating so many different interface? Why not use 1 interface and link it all together since the callback have the same parameter.
So im trying to update a list with the information provided from a server communicated by JSON.
This is the class doing the communication:
package com.example.simon_000.buddy;
public class TCPConnection {
private RunOnThread thread;
private Receive receive;
private MainActivity ma;
private Socket socket;
private DataInputStream input;
private DataOutputStream output;
private InetAddress address;
private int connectionPort;
private String ip;
private Exception exception;
public static String id;
public static ArrayList<members> memberList = new ArrayList<members>();
public static ArrayList<String> groupsList = new ArrayList<String>();
public static ArrayList<String> namesList = new ArrayList<String>();
public TCPConnection(String ip, int connectionPort, MainActivity ma) {
this.ip = ip;
this.connectionPort = connectionPort;
thread = new RunOnThread();
this.ma = ma;
}
public void connect() {
thread.start();
thread.execute(new Connect());
}
public void disconnect() {
thread.execute(new Disconnect());
}
public void send(String expression) {
thread.execute(new Send(expression));
}
private class Receive extends Thread {
public void run() {
String result;
try {
while (receive != null) {
result = (String) input.readUTF();
newMessage(result);
}
} catch (Exception e) { // IOException, ClassNotFoundException
receive = null;
}
}
}
public void newMessage(final String answer) {
ma.runOnUiThread(new Runnable() {
public void run() {
String message = answer;
String type;
JSONObject jObj = null;
try {
Log.d("TEST", message);
jObj = new JSONObject(message);
type = jObj.getString("type");
if (type.equals("groups")) {
recevieGroups(jObj);
}
else if (type.equals("register")) {
recevieID(jObj);
}
else if (type.equals("members")) {
receiveMembers(jObj);
}
else if (type.equals("location")) {
receiveLocations(jObj);
} else if (type.equals("locations")) {
receiveLocations(jObj);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
public void receiveLocations(JSONObject jObj) throws JSONException {
// { ”type”:”locations”, ”group”:”NAME”, ”locations”:[ {”member”:”NAME”, ”longitude”:”LONGITUDE”, ”latitude”:”LATITUDE” }, … ] }
JSONArray jArray = jObj.getJSONArray("locations");
for (int i = 0; i < jArray.length(); i++) {
members m = new members();
JSONObject jRealObject = jArray.getJSONObject(i);
m.setName(jRealObject.getString("member"));
m.setLongitude(Double.parseDouble(jRealObject.getString("longitude")));
m.setLatitude(Double.parseDouble(jRealObject.getString("latitude")));
memberList.add(m);
Log.d("TEST", " memberNAMES : " + m.getName()+" lng: "+ m.getLongitude()+" lat: "+m.getLatitude());
}
ma.updateMapMarkers(memberList);
}
public void receiveMembers(JSONObject jObj) throws JSONException {
// { “type”:”members”, “group”:”NAME”, “members”:[ {“member”:”NAME”},…] }
JSONArray jArray = jObj.getJSONArray("members");
for (int i = 0; i < jArray.length(); i++) {
String n;
JSONObject jRealObject = jArray.getJSONObject(i);
n = jRealObject.getString("member");
Log.d("TEST", " MembernamesBEFORE_ADD : " + n );
namesList.add(n);
Log.d("TEST", " memberNAMES : " + n);
}
}
public void recevieID(JSONObject jObj) throws JSONException {
id = (jObj.getString("id"));
Log.d("TEST", " ID : " + id);
}
public void recevieGroups(JSONObject jObj) throws JSONException {
JSONArray jArray = jObj.getJSONArray("groups");
for (int i = 0; i < jArray.length(); i++) {
String g;
JSONObject jRealObject = jArray.getJSONObject(i);
g = (jRealObject.getString("group"));
groupsList.add(g);
Log.d("TEST", " groupNames : " + g);
}
}
public Exception getException() {
Exception result = exception;
exception = null;
return result;
}
private class Connect implements Runnable {
public void run() {
try {
Log.d("TCPConnection", "Connect-run");
address = InetAddress.getByName(ip);
Log.d("TCPConnection-Connect", "Skapar socket");
socket = new Socket(address, connectionPort);
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
output.flush();
Log.d("TCPConnection-Connect", "Strömmar klara");
newMessage("CONNECTED");
receive = new Receive();
receive.start();
} catch (Exception e) { // SocketException, UnknownHostException
Log.d("TCPConnection-Connect", e.toString());
exception = e;
newMessage("EXCEPTION");
}
}
}
public class Disconnect implements Runnable {
public void run() {
try {
if (socket != null)
socket.close();
if (input != null)
input.close();
if (output != null)
output.close();
thread.stop();
newMessage("CLOSED");
} catch (IOException e) {
exception = e;
newMessage("EXCEPTION");
}
}
}
public class Send implements Runnable {
private String exp;
public Send(String exp) {
this.exp = exp;
}
public void run() {
try {
output.writeUTF(exp);
output.flush();
} catch (IOException e) {
exception = e;
newMessage("EXCEPTION");
}
}
}
}
This is the "Controller":
package com.example.simon_000.buddy;
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
private myMap tempMapfragment = new myMap();
private Menu menu;
private String inetAddress = "195.178.232.7";
private Integer port = 7117;
private TCPConnection connect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startNavDrawer();
connect = new TCPConnection(inetAddress, port, this);
connect.connect();
}
public void myPosition(String id, double LONGITUDE, double LATITUDE) {
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter);
try {
writer.beginObject().name("type").value("location").name("id").value(id).name("longitude").value(LONGITUDE).name("latitude").value(LATITUDE).endObject();
} catch (IOException e) {
e.printStackTrace();
}
connect.send(stringWriter.toString());
}
public void getMembers(String group) {
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter);
try {
writer.beginObject().name("type").value("members").name("group").value(group).endObject();
} catch (IOException e) {
e.printStackTrace();
}
connect.send(stringWriter.toString());
}
public void getGroups() {
// Aktuella grupper
// { ”type”:”groups” }
// { “type”:”groups”, ”groups”:[ {”group”:”NAME”}, …] }
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter);
try {
writer.beginObject().name("type").value("groups").endObject();
} catch (IOException e) {
e.printStackTrace();
}
connect.send(stringWriter.toString());
}
public void registerGroup(String groupName, String userName) {
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter);
try {
writer.beginObject().name("type").value("register").name("group").value(groupName).name("member").value(userName).endObject();
} catch (IOException e) {
e.printStackTrace();
}
connect.send(stringWriter.toString());
}
public void updateMapMarkers(ArrayList<members> memberList) {
for(members m: memberList){
tempMapfragment.addMarker(m);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
connect.disconnect();
}
private void startNavDrawer() {
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
communication fragment2 = new communication();
switch (position) {
case 0:
// fragment1
// use fragment transaction and add the fragment to the container
fragmentTransaction.replace(R.id.container, fragment2);
fragmentTransaction.commit();
break;
case 1:
// fragment2
fragmentTransaction.replace(R.id.container, tempMapfragment);
fragmentTransaction.commit();
break;
case 2:
// fragment2
break;
default:
// fragment1
// use fragment transaction and add the fragment to the container
fragmentTransaction.replace(R.id.container, fragment2);
fragmentTransaction.commit();
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//Setting maptype
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
This is the fragment, its in the part where i open a Dialog onclick.
package com.example.simon_000.buddy.Fragments;
public class communication extends Fragment {
private ListView list;
private EditText groupet;
private EditText nameet;
private ListView memberList;
private Button btsend, btUpdate;
private GroupAdapter Groupadapter;
private NameAdapter Nameadapter;
public communication() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_communication, container, false);
getGroupInfo();
initiateVariables(view);
return view;
}
private void initiateVariables(View view) {
list = (ListView) view.findViewById(R.id.grouplistView);
// groupsList = new ArrayList<String>();
// namesList = new ArrayList<String>();
groupet = (EditText) view.findViewById(R.id.etgroupName);
nameet = (EditText) view.findViewById(R.id.etUsername);
btsend = (Button) view.findViewById(R.id.btSend);
btUpdate = (Button) view.findViewById(R.id.btUpdate);
groupet.setText("MadKim");
Groupadapter = new GroupAdapter(getActivity(), R.layout.row, TCPConnection.groupsList);
list.setAdapter(Groupadapter);
list.setOnItemClickListener(new listListener());
btsend.setOnClickListener(new Listener());
btUpdate.setOnClickListener(new ListenerUpdate());
}
public void getGroupInfo(){
((MainActivity)getActivity()).getGroups();
}
private class Listener implements View.OnClickListener {
#Override
public void onClick(View view) {
// sc.startThreadCommunication();
String group = groupet.getText().toString();
String name = nameet.getText().toString();
// sending query to server
if(group.isEmpty() || name.isEmpty() ){
Toast.makeText(getActivity(), "You need to fill in all the fields.", Toast.LENGTH_LONG).show();
}else {
((MainActivity)getActivity()).registerGroup(groupet.getText().toString(), nameet.getText().toString());
getGroupInfo();
Groupadapter.notifyDataSetChanged();
//hides keyboard
nameet.clearFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(nameet.getWindowToken(), 0);
//show user info
Toast.makeText(getActivity(), "Successfully registered to: "+groupet.getText().toString()+
" with username: "+nameet.getText().toString(), Toast.LENGTH_LONG).show();
groupet.setText("");
nameet.setText("");
}
}
}
private class ListenerUpdate implements View.OnClickListener {
#Override
public void onClick(View view) {
getGroupInfo();
Groupadapter = new GroupAdapter(getActivity(), R.layout.row, TCPConnection.groupsList);
list.setAdapter(Groupadapter);
Groupadapter.notifyDataSetChanged();
}
}
//REVENUES list onClick LISTENER
private class listListener implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// ListView Clicked item index
int itemPosition = position;
Nameadapter.notifyDataSetChanged();
((MainActivity)getActivity()).getMembers(TCPConnection.groupsList.get(itemPosition).toString());
Nameadapter.notifyDataSetChanged();
//open selected finanse and show more information
Dialog d = new Dialog(getActivity());
String title = getResources().getString(R.string.dialogTitle);
d.setTitle(title);
d.setCanceledOnTouchOutside(true);
//inserting xml file in Dialog
LayoutInflater factory = LayoutInflater.from(getActivity());
View infoLayout = factory.inflate(R.layout.dialog, null);
memberList = (ListView) infoLayout.findViewById(R.id.memberList);
Button updatebt = (Button) infoLayout.findViewById(R.id.updateDialogBt);
updatebt.setOnClickListener(new updateDialogListener());
d.setContentView(infoLayout);
Nameadapter = new NameAdapter(getActivity(), R.layout.rownames, TCPConnection.namesList);
memberList.setAdapter(Nameadapter);
Nameadapter.notifyDataSetChanged();
d.show();
}
private class updateDialogListener implements View.OnClickListener {
#Override
public void onClick(View view) {
Nameadapter = new NameAdapter(getActivity(), R.layout.rownames, TCPConnection.namesList);
memberList.setAdapter(Nameadapter);
Nameadapter.notifyDataSetChanged();
}
}
}
}
This is my error:
10-27 17:49:00.168 24903-24903/com.example.simon_000.buddy E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.simon_000.buddy, PID: 24903
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131034141, class android.widget.ListView) with Adapter(class com.example.simon_000.buddy.customs.GroupAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1555)
at android.widget.AbsListView.onTouchUp(AbsListView.java:3624)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3436)
at android.view.View.dispatchTouchEvent(View.java:7713)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2329)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1568)
at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2277)
at android.view.View.dispatchPointerEvent(View.java:7893)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3950)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3829)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3445)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3414)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3521)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3422)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3578)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3445)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3414)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3422)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5535)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5515)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5486)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5615)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.j
Your adapter.notifyDataSetChanged() must be called from the UI thread. At present it's called from a background thread.
So instead of:
adapter.notifyDataSetChanged();
You should write:
yourActivity.runOnUiThread(new Runnable(){
public void run() {
adapter.notifyDataSetChanged();
}
});
When calling the method from a background thread.
This allows the ListView to update properly with the data attached to the adapter that has been changed.
My application pulls data from a web service that generates different sections for each user. Then I am going to use these sections to create tabs using FragmentPagerAdapter.
I have used an Async task to pull data from the web service. However the overridden methods such as getCount() and getPageTitle() in the FragmentPagerAdapter executed prior to my asynctask and completes its job. How can I prevent this and generate dynamic number of tabs and their title name based on the data fetched from the web service?
In other words how can I create dynamic number of tabs and titles based on the data fetch from the web service
My Code for FragmentPagerAdapter as below. As you can see I have hard coded the amount of tabs as well as their title names.
public class SectionsPagerAdapter extends FragmentPagerAdapter{
private boolean proceedStatus = false;
private String requestURL = "xxxxxxxxxxxxxxxxxxxxxxxx";
//list of fragments need to be added dynamically
public final ArrayList<Fragment> screens = new ArrayList<Fragment>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new SectionFragment();
Bundle args = new Bundle();
args.putInt(SectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "Camera".toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return "SECTION 4";
}
return null;
}
//setting the section title
private void setSectionTitle(){
}
//count the number of sections
private int countNumberofSections(){
int numberOfSection = 0;
return numberOfSection;
}
}
Then I have my Fragment code as below which has the the caller to the Async Task
public static class SectionFragment extends Fragment implements OnTaskCompleted {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private Slider adapter;
private ViewPager viewPager;
public static final String ARG_SECTION_NUMBER = "section_number";
Button thumbUpBut;
Button thumbDownBut;
Button captureButton;
ImageView genImage;
TextView genCaption;
private Camera mCamera;
private CameraPreview mPreview;
private static File mediaFile;
private ProgressDialog progress;
private static String imageSaveLocation;
private static String file_name_without_extension;
private ImageView imageView;
private Button uploadButton;
private Button cancelButton;
private Collection<Place> places = null;
private Collection<Happenings> events = null;
private Collection<General> general = null;
private ArrayList<String> sections;
public int getNumberOfPages(){
return sections.size();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
FeedRequest task = new FeedRequest(this);
task.execute("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
captureButton = (Button) rootView.findViewById(R.id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takePhoto();
}
});
thumbUpBut = (Button) rootView.findViewById(R.id.thumbUp);
thumbUpBut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v("thumbPress", "thumbPressUp");
thumb("up");
}
});
thumbDownBut = (Button) rootView.findViewById(R.id.thumbDown);
thumbDownBut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v("thumbPress", "thumbPressDown");
thumb("down");
}
});
//allocating the activity one to the camera
if(getArguments().getInt(ARG_SECTION_NUMBER) == 1){
mCamera = getCameraInstance();
mPreview = new CameraPreview(this.getActivity(), mCamera);
FrameLayout preview = (FrameLayout)rootView.findViewById(R.id.camera_preview);
preview.addView(mPreview);
//hide buttons
thumbDownBut.setVisibility(rootView.INVISIBLE);
thumbUpBut.setVisibility(rootView.INVISIBLE);
}else{
thumbDownBut.setVisibility(rootView.VISIBLE);
thumbUpBut.setVisibility(rootView.VISIBLE);
captureButton.setVisibility(rootView.INVISIBLE);
}
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
return rootView;
}
//take photo function
private void takePhoto() {
//get coordinates of the location
UserLocation userLocation = new UserLocation();
userLocation.getUserLocation(getActivity());
coordinates[0] = userLocation.longitude;
coordinates[1] = userLocation.latitude;
PictureCallback pictureCB = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera cam) {
new SavePhotoAndMetadata().execute(data);
cam.startPreview();
}
};
mCamera.takePicture(null, null, pictureCB);
}
//get camera instance
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}
//get the media out
private static File getOutputMediaFile() {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory() + "/Android/data/asia.ceynet.realsnap/temp_img");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = (DateFormat.format("dd-MM-yyyy hh:mm:ss", new java.util.Date()).toString());
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
file_name_without_extension = "IMG_" + timeStamp;
imageSaveLocation = mediaFile.toString();
return mediaFile;
}
//saving the image and metadata together
class SavePhotoAndMetadata extends AsyncTask<byte[], String, String> {
#Override
protected String doInBackground(byte[]... data) {
File picFile = getOutputMediaFile();
if (picFile == null) {
return null;
}
byte[] photoData = data[0];
try {
//save the image
FileOutputStream fos = new FileOutputStream(picFile);
fos.write(photoData);
fos.close();
} catch (FileNotFoundException e) {
e.getStackTrace();
} catch (IOException e) {
e.getStackTrace();
}
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
progress = new ProgressDialog(getActivity());
progress.setMessage("Saving Picture..Please wait...");
progress.show();
}
#Override
protected void onPostExecute(String s) {
progress.dismiss();
imagePreviewDialog();
}
}
//save image metadata in async task
class SaveMetadataTask extends AsyncTask<Void, String, Void> {
#Override
protected Void doInBackground(Void... params) {
serializeDeserialize.serializeData("This is for testing", file_name_without_extension, Double.toString(coordinates[0]), Double.toString(coordinates[1]), deviceId, deviceEmail);
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(Void v) {
}
}
//image preview dialog and its functionality
private void imagePreviewDialog(){
//setting the bitmap
Bitmap bmp = BitmapFactory.decodeFile(mediaFile.toString());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Image Preview");
builder.setCancelable(false);
LayoutInflater inflater = getActivity().getLayoutInflater();
ViewGroup vg = (ViewGroup)inflater.inflate(R.layout.sanp_preview_layout, null);
ImageView image = (ImageView) vg.findViewById(R.id.imageView);
image.setImageBitmap(rotateBitmap(bmp));
builder.setView(vg);
//buttons
builder.setPositiveButton("Upload",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if(checkInternetConnection.haveNetworkConnection(sContext)){
//upload the image
uploadImage();
//save image metadata
new SaveMetadataTask().execute();
}else{
Toast.makeText(sContext, "Error! No internet connection detected. Image will be uploaded on an active internet connection", Toast.LENGTH_LONG).show();
new SaveMetadataTask().execute();
}
}
});
builder.setNegativeButton("Discard",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
discardImage(mediaFile.toString());
dialog.dismiss();
}
});
builder.show();
}
private Bitmap rotateBitmap(Bitmap image){
int width=image.getHeight();
int height=image.getWidth();
Bitmap srcBitmap=Bitmap.createBitmap(width, height, image.getConfig());
for (int y=width-1;y>=0;y--)
for(int x=0;x<height;x++)
srcBitmap.setPixel(width-y-1, x,image.getPixel(x, y));
return srcBitmap;
}
//device email
private String getDeviceEmail(){
AccountManager accountManager = AccountManager.get(sContext);
Account[] account = accountManager.getAccountsByType("com.google");
//device email
for(Account accLoop : account){
deviceEmail = accLoop.name;
}
return deviceEmail;
}
//upload image to the server
private void uploadImage(){
//save metadata
//call upload service
Intent intent = new Intent(sContext, HttpUploader.class);
Bundle loc = new Bundle();
loc.putDoubleArray("ss", coordinates);
intent.putExtra("url", PHOTO_UPLOAD);
intent.putExtra("paths", mediaFile.toString());
intent.putExtra("deviceid", deviceId);
intent.putExtra("deviceemail", getDeviceEmail());
intent.putExtra("posttext", "This is for testing");
intent.putExtra("filename", file_name_without_extension);
intent.putExtra("geo", loc);
sContext.startService(intent);
Toast.makeText(getActivity(), "Your image is being uploaded", Toast.LENGTH_LONG).show();
}
//discard image when the discard button is pressed
private void discardImage(String imagePath){
File file = new File(imagePath);
try{
file.delete();
}catch(Exception e){
Log.e("IMAGE_DELETION_ERROR", e.toString());
}
}
#Override
public void onTaskCompleted(boolean status, String message) {
// TODO Auto-generated method stub
Log.e("onTaskCompleted", "success" + status);
if (message == "tumb UP success") {
thumbUpBut.setSelected(true);
thumbDownBut.setSelected(false);
Log.e("tumb", "tumb");
} else if (message == "tumb DOWN success") {
thumbDownBut.setSelected(true);
thumbUpBut.setSelected(false);
Log.e("tumb", "tumb");
}
}
//listener for fetching main objects
#Override
public void onFeedCompleted(ArrayList<Posts> postArray, Multimap<String, Object> multiMap) {
// TODO Auto-generated method stub
numberOfPages = postArray.size();
adapter = new Slider(getActivity(), postArray, getContext());
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(postArray.size());
//saving the keyset
Set<String> keys = multiMap.keySet();
sections = new ArrayList<String>();
//sorting the categories and creating the category list
for(String key:keys){
//getting category list
if(!sections.contains(keys)){
sections.add(key);
}
//sorting categories
if(key.equals("Place")){
places.add((Place) multiMap.get(key));
}else if(key.equals("Events")){
events.add((Happenings) multiMap.get(key));
}else if(key.equals("General")){
general.add((General) multiMap.get(key));
}
}
}
//adding the pages to the adaptor dynamically
public void addPagesDynamically(){
}
}
//create the parent directory
private void createParentDiectory(){
File dir = new File(Environment.getExternalStorageDirectory() + "/Android/data/asia.ceynet.realsnap");
if(!(dir.exists() && dir.isDirectory())) {
dir.mkdirs();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_post:
openPost();
return true;
case R.id.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void openPost() {
/*
Intent i = new Intent(getApplicationContext(), PhotoActivity.class);
startActivity(i);
*/
}
public static void thumb(String type) {
SectionFragment d = new SectionFragment();
PostThumb task = new PostThumb(type, d);
task.execute("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
//broadcast receiver for picture upload
public class MyWebRequestReceiver extends BroadcastReceiver {
public static final String PROCESS_RESPONSE = "asia.ceynet.intent.action.PROCESS_RESPONSE";
#Override
public void onReceive(Context context, Intent intent) {
//String responseString = intent.getStringExtra(HttpUploader.RESPONSE_STRING);
String reponseMessage = intent.getStringExtra(HttpUploader.RESPONSE_MESSAGE);
String responseStatus = intent.getStringExtra(HttpUploader.RESPONSE_STATUS);
String file_to_be_deleted = intent.getStringExtra(HttpUploader.FILE_NAME_WITHOUT_EXTENSION);
Toast.makeText(getApplicationContext(), reponseMessage + " - " + file_to_be_deleted + ".jpg", Toast.LENGTH_LONG).show();
//if uploaded successfully delete or image and metadata
if(responseStatus.equals("true")){
File temp_image_dir = new File(Environment.getExternalStorageDirectory() + "/Android/data/asia.ceynet.realsnap/temp_img/" + file_to_be_deleted + ".jpg");
File metadata_file = new File(Environment.getExternalStorageDirectory() + "/Android/data/asia.ceynet.realsnap/temp_img/" + file_to_be_deleted + ".ser");
try{
temp_image_dir.delete();
metadata_file.delete();
}catch(Exception e){
Log.e("IMAGE_DELETION_ERROR", e.toString());
}
}
}
}
When you finnish pulling the async data, provide the adapter with the new data and call .notifyDataSetChanged() on that adapter instance and the framework will update the pages and count by itself.
If you wish a more detailed explanation post your FragmentPagerAdapter implementation.
First of all, let me apologize if I'm not making myself clear enough because this is one of my first participation(s) here. But I'll be always here to answer queries related to this answer and clear any confusions arose by my statements.
Since you're using fragments, so I'm assuming you must have included your fragments inside an activity (lets say MainActivity.java).
What you need, can be done inside that activity containing fragment.
Here is the example code of onCreate method inside the MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
//Instance of viewpager included inside activity_main.xml
viewPager = (ViewPager) findViewById(R.id.vpMain);
SectionsPagerAdapter adapter = new SectionsPagerAdapter (fragmentManager);
//Adding some fragments right from the beginning, you could ignore it if not needed.
addFragments();
//This `OnPageChangeListener` will do the trick for you.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
//Show the title of fragment
Toast.makeText(MainActivity.this, adapter.screens.get(position), Toast.LENGTH_SHORT).show();
//If fragment being loaded is later than the first one,
// then add one more fragment after the last fragment to the adapter.
// integer currentPosition is declared as a field, outside onCreate method and initially set to 0.
if(position>currentPosition){
currentPosition+=1;
adapter.addFragment(new SectionFragment(), "Fragment"+String.valueOf(position+3));
adapter.notifyDataSetChanged();
}else{
currentPosition--;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
} // onCreate ends here.
Create this method inside your MainActivity (just to add 3 fragments to give your application a head-start.
private void addFragments(){
adapter.addFragment(new SectionFragment());
adapter.addFragment(new SectionFragment());
adapter.addFragment(new SectionFragment());
}
Then modify your SectionsPagerAdapter's getItem and getCount methods as below:
public class SectionsPagerAdapter extends FragmentPagerAdapter{
private boolean proceedStatus = false;
private String requestURL = "xxxxxxxxxxxxxxxxxxxxxxxx";
//list of fragments need to be added dynamically
public final ArrayList<Fragment> screens = new ArrayList<Fragment>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return screens.get(position);
}
#Override
public int getCount() {
return screens.size();
}
//This method will dynamically add a fragment each time it is called.
public void addFragment(Fragment fragment) {
screens.add(fragment);
}
Now, no work related to "adding new fragment to the list" needs to be done inside your SectionFragment class.
I don't know how to custom my own Fragment which inherite another Fragment class.
I'd like to add button and catch every click on them, but I don't how to do that.
As you'll see, I'm using actionBarSherlock and Zxing (a sub-library of Zxing actually: "barcodeFragLibV2") and the navigation drawer.
Here my code:
MainActivity.java:
public class MainActivity extends SherlockFragmentActivity {
private static final String TAG = MainActivity.class.getSimpleName();
/**
* The navigation drawer layout.
*/
DrawerLayout mDrawerLayout;
/**
* The elements list of the menu.
*/
ListView mDrawerList;
/**
* The button to open/close the menu.
*/
ActionBarDrawerToggle mDrawerToggle;
/**
* The helper item of the local database.
*/
private DatabaseHelper dbHelper = null;
/**
* The current fragment title.
*/
String mTitle = "";
/**
* #author LG
* #category Property
*/
private boolean mShowOptionMenu;
private int fragmentStatus = 0;
private Menu mMenu = null;
private MenuItem mGoItem;
private static final int GO_ITEM_ID = 1;
private static final int CLEAR_ITEM_ID = 2;
public String getmTitle() {
return mTitle;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
/**
* Method called when the activity is loaded.
*
* #param savedInstanceState
* the bundle sent.
*/
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "================= onCreate =================");
if (!isTablet()) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
setContentView(R.layout.activity_main);
mTitle = (String) getTitle();
mShowOptionMenu = false;
// result_view = findViewById(R.id.result_view);
// result_view.setVisibility(View.GONE);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
/** Called when drawer is closed */
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
/** Called when a drawer is opened */
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle("Menu");
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
MenuListAdapter adapter = new MenuListAdapter(getBaseContext());
mDrawerList.setAdapter(adapter);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
displayView(position);
}
private void displayView(int position) {
String[] menu = getResources().getStringArray(R.array.menu);
mTitle = menu[position];
MainActivity.this.fragmentStatus = position;
SherlockFragment rFragment = null;
Log.d(TAG, "================= mDrawerList.setOnItemClickListener =================");
if (position == 0)
rFragment = new SearchFragment();
else if (position == 1) {
mShowOptionMenu = true;
rFragment = new AdvanceSearchFragment();
} else if (position == 2) {
rFragment = new QrCodeScannerFragment(); // WORKS
} else if (position == 3)
rFragment = new FavorisFragment();
else if (position == 4)
rFragment = new HistoriqueFragment();
else if (position == 5)
rFragment = new InformationsFragment();
if (rFragment != null) {
Bundle data = new Bundle();
data.putInt("position", position);
rFragment.setArguments(data);
FragmentManager fragmentManager = getSupportFragmentManager();
while (fragmentManager.getBackStackEntryCount() > 0)
fragmentManager.popBackStackImmediate();
FragmentTransaction ft = fragmentManager.beginTransaction();
// ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.replace(R.id.content_frame, rFragment);
ft.addToBackStack(null);
ft.commit();
mDrawerLayout.closeDrawer(mDrawerList);
}
}
}
);
SearchFragment rFragment = new SearchFragment();
Bundle data = new Bundle();
rFragment.setArguments(data);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.content_frame, rFragment);
ft.commit();
}
/**
* Method called after the activity is loaded.
*
* #param savedInstanceState
* the bundle sent.
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Log.d(TAG, "================= onPostCreate =================");
mDrawerToggle.syncState();
}
/**
* Inflate the menu; this adds items to the action bar if it is present.
*
* #param menu
* the menu
*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "================= onCreateOptionsMenu ================= fragSt: " + fragmentStatus);
getSupportMenuInflater().inflate(R.menu.main, (Menu) menu);
if (fragmentStatus == 1) {
Log.i(TAG, "Show optionMenu");
mGoItem = menu.add(0, GO_ITEM_ID, 0, null);
mGoItem.setIcon(R.drawable.abs__ic_go).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mShowOptionMenu = false;
}
return true;
}
/**
* Called whenever we call invalidateOptionsMenu()
*
* #param menu
* the menu
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the drawer is open, hide action items related to the content view
Log.d(TAG, "================= onPrepareOptionsMenu ================= fragSt: " + fragmentStatus);
return super.onPrepareOptionsMenu(menu);
}
/**
* Method called when an menu's item is selected.
*
* #param item
* the item selected
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "================= onOptionsItemSelected ================= fragSt: " + fragmentStatus);
switch (item.getItemId()) {
case android.R.id.home:
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
return true;
}
return false;
}
/**
* Method to get device button actions
*
* #param keyCode
* the code of the button pressed
* #param event
* the event
*/
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "================= onKeyDown ================= fragSt: " + fragmentStatus);
Log.w(TAG, "KeyEven: " + keyCode);
if (keyCode == KeyEvent.KEYCODE_MENU ) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
if (!drawerOpen)
mDrawerLayout.openDrawer(Gravity.LEFT);
else
mDrawerLayout.closeDrawer(Gravity.LEFT);
return true;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.d(TAG, "TouchKeyBack");
}
return super.onKeyDown(keyCode, event);
}
/**
* Method called when the back button is pressed
*/
#Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (mShowOptionMenu == true)
mShowOptionMenu = false;
Log.d(TAG, "onBackPressed");
int tt = fm.getBackStackEntryCount();
// Log.d(TAG, "BackStackEntry name : " +
// fm.getBackStackEntryAt(tt).getName());
if (fm.getBackStackEntryCount() > 0) {
Log.i("MainActivity", "popping backstack");
getSupportFragmentManager().popBackStack();
} else {
Log.i("MainActivity", "nothing on backstack, calling super");
super.onBackPressed();
}
}
/**
* Method to know if the device is tablet or smartphone
*
* #return a boolean indicating if the device is a tablet
*/
public boolean isTablet() {
Log.d(TAG, "================= isTablet =================");
boolean xlarge = ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
/**
* Method called when the activity is deallocated
*/
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "================= onDestroy =================");
if (dbHelper != null) {
OpenHelperManager.releaseHelper();
dbHelper = null;
}
}
/**
* Return the database helper
*/
public DatabaseHelper getHelper() {
if (dbHelper == null) {
dbHelper = (DatabaseHelper) OpenHelperManager.getHelper(this, DatabaseHelper.class);
}
return dbHelper;
}
/**
* Method called when the activity start. Used for Google Analytics
*/
#Override
public void onStart() {
super.onStart();
Log.d(TAG, "================= onStart =================");
EasyTracker.getInstance(this).activityStart(this);
}
/**
* Method called when the activity stop. Used for Google Analytics
*/
#Override
public void onStop() {
Log.d(TAG, "================= onStop =================");
super.onStop();
EasyTracker.getInstance(this).activityStop(this);
}
}
QrCodeScannerFragment.java:
public class QrCodeScannerFragment extends BarcodeFragment {
public static final String TAG = QrCodeScannerFragment.class.getSimpleName();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setScanResultHandler(new IScanResultHandler() {
#Override
public void scanResult(ScanResult result) {
Log.w(TAG, "ScanResult");
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
return super.onCreateView(inflater, container, savedInstanceState);
}
}
BarcodeFragment.java:
public class BarcodeFragment extends SherlockFragment implements SurfaceHolder.Callback {
private static final String TAG = BarcodeFragment.class.getSimpleName();
private static final long BULK_MODE_SCAN_DELAY_MS = 1000L;
private CameraManager cameraManager;
private CaptureFragmentHandler handler;
private Result savedResultToShow;
private ViewfinderView viewfinderView;
private boolean hasSurface;
private Collection<BarcodeFormat> decodeFormats;
private Map<DecodeHintType, ?> decodeHints;
private String characterSet;
private InactivityTimer inactivityTimer;
private AmbientLightManager ambientLightManager;
private IScanResultHandler resultHandler;
public ViewfinderView getViewfinderView() {
return viewfinderView;
}
public Handler getHandler() {
return handler;
}
public CameraManager getCameraManager() {
return cameraManager;
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
hasSurface = false;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FrameLayout frameLayout = new FrameLayout(getActivity());
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
frameLayout.setLayoutParams(layoutParams);
surfaceView = new SurfaceView(getActivity());
surfaceView.setLayoutParams(layoutParams);
viewfinderView = new ViewfinderView(getActivity());
viewfinderView.setLayoutParams(layoutParams);
frameLayout.addView(surfaceView);
frameLayout.addView(viewfinderView);
View v = frameLayout;
inactivityTimer = new InactivityTimer(this.getActivity());
ambientLightManager = new AmbientLightManager(this.getActivity());
return v;
}
SurfaceView surfaceView;
#SuppressWarnings("deprecation")
#Override
public void onResume() {
super.onResume();
// CameraManager must be initialized here, not in onCreate(). This is
// necessary because we don't
// want to open the camera driver and measure the screen size if we're
// going to show the help on
// first launch. That led to bugs where the scanning rectangle was the
// wrong size and partially
// off screen.
cameraManager = new CameraManager(this.getActivity(), getView());
viewfinderView.setCameraManager(cameraManager);
handler = null;
resetStatusView();
SurfaceHolder surfaceHolder = surfaceView.getHolder();
if (hasSurface) {
// The activity was paused but not stopped, so the surface still
// exists. Therefore
// surfaceCreated() won't be called, so init the camera here.
initCamera(surfaceHolder);
} else {
// Install the callback and wait for surfaceCreated() to init the
// camera.
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
ambientLightManager.start(cameraManager);
inactivityTimer.onResume();
decodeFormats = null;
characterSet = null;
}
#Override
public void onPause() {
if (handler != null) {
handler.quitSynchronously();
handler = null;
}
inactivityTimer.onPause();
ambientLightManager.stop();
cameraManager.closeDriver();
if (!hasSurface) {
SurfaceView surfaceView = this.surfaceView;
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.removeCallback(this);
}
super.onPause();
}
#Override
public void onDestroy() {
inactivityTimer.shutdown();
super.onDestroy();
}
public void restart() {
restartPreviewAfterDelay(BULK_MODE_SCAN_DELAY_MS);
}
private void decodeOrStoreSavedBitmap(Bitmap bitmap, Result result) {
// Bitmap isn't used yet -- will be used soon
if (handler == null) {
savedResultToShow = result;
} else {
if (result != null) {
savedResultToShow = result;
}
if (savedResultToShow != null) {
Message message = Message.obtain(handler, IDS.id.decode_succeeded, savedResultToShow);
handler.sendMessage(message);
}
savedResultToShow = null;
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
if (holder == null) {
Log.e(TAG, "*** WARNING *** surfaceCreated() gave us a null surface!");
}
if (!hasSurface) {
hasSurface = true;
initCamera(holder);
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
hasSurface = false;
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
/**
* A valid barcode has been found, so give an indication of success and show
* the results.
*
* #param rawResult
* The contents of the barcode.
* #param scaleFactor
* amount by which thumbnail was scaled
* #param barcode
* A greyscale bitmap of the camera data which was decoded.
*/
public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
inactivityTimer.onActivity();
ScanResult resultHandler = ResultHandlerFactory.parseResult(rawResult);
boolean fromLiveScan = barcode != null;
if (fromLiveScan) {
drawResultPoints(barcode, scaleFactor, rawResult);
}
handleDecodeInternally(rawResult, resultHandler, barcode);
}
/**
* Superimpose a line for 1D or dots for 2D to highlight the key features of
* the barcode.
*
* #param barcode
* A bitmap of the captured image.
* #param scaleFactor
* amount by which thumbnail was scaled
* #param rawResult
* The decoded results which contains the points to draw.
*/
private void drawResultPoints(Bitmap barcode, float scaleFactor, Result rawResult) {
ResultPoint[] points = rawResult.getResultPoints();
if (points != null && points.length > 0) {
Canvas canvas = new Canvas(barcode);
Paint paint = new Paint();
paint.setColor(Color.parseColor("#c099cc00"));
if (points.length == 2) {
paint.setStrokeWidth(4.0f);
drawLine(canvas, paint, points[0], points[1], scaleFactor);
} else if (points.length == 4
&& (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A || rawResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
// Hacky special case -- draw two lines, for the barcode and
// metadata
drawLine(canvas, paint, points[0], points[1], scaleFactor);
drawLine(canvas, paint, points[2], points[3], scaleFactor);
} else {
paint.setStrokeWidth(10.0f);
for (ResultPoint point : points) {
canvas.drawPoint(scaleFactor * point.getX(), scaleFactor * point.getY(), paint);
}
}
}
}
private static void drawLine(Canvas canvas, Paint paint, ResultPoint a, ResultPoint b, float scaleFactor) {
if (a != null && b != null) {
canvas.drawLine(scaleFactor * a.getX(), scaleFactor * a.getY(), scaleFactor * b.getX(), scaleFactor * b.getY(), paint);
}
}
// Put up our own UI for how to handle the decoded contents.
private void handleDecodeInternally(Result rawResult, ScanResult resultHandler, Bitmap barcode) {
viewfinderView.setVisibility(View.GONE);
if (this.resultHandler != null) {
this.resultHandler.scanResult(resultHandler);
}
}
private void initCamera(SurfaceHolder surfaceHolder) {
if (surfaceHolder == null) {
throw new IllegalStateException("No SurfaceHolder provided");
}
if (cameraManager.isOpen()) {
Log.w(TAG, "initCamera() while already open -- late SurfaceView callback?");
return;
}
try {
cameraManager.openDriver(surfaceHolder);
// Creating the handler starts the preview, which can also throw a
// RuntimeException.
if (handler == null) {
handler = new CaptureFragmentHandler(this, decodeFormats, decodeHints, characterSet, cameraManager);
}
decodeOrStoreSavedBitmap(null, null);
} catch (IOException ioe) {
Log.w(TAG, ioe);
displayFrameworkBugMessageAndExit();
} catch (RuntimeException e) {
// Barcode Scanner has seen crashes in the wild of this variety:
// java.?lang.?RuntimeException: Fail to connect to camera service
Log.w(TAG, "Unexpected error initializing camera", e);
displayFrameworkBugMessageAndExit();
}
}
private void displayFrameworkBugMessageAndExit() {
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setTitle(getString(R.string.app_name));
builder.setMessage("Sorry, the Android camera encountered a problem. You may need to restart the device.");
builder.setPositiveButton("OK", new FinishListener(this.getActivity()));
builder.setOnCancelListener(new FinishListener(this.getActivity()));
builder.show();
}
public void restartPreviewAfterDelay(long delayMS) {
if (handler != null) {
handler.sendEmptyMessageDelayed(IDS.id.restart_preview, delayMS);
}
resetStatusView();
}
private void resetStatusView() {
viewfinderView.setVisibility(View.VISIBLE);
}
public void drawViewfinder() {
viewfinderView.drawViewfinder();
}
public IScanResultHandler getScanResultHandler() {
return resultHandler;
}
public void setScanResultHandler(IScanResultHandler resultHandler) {
this.resultHandler = resultHandler;
}
}
I am trying to populate data from my main activity using the adapter below. When i run the activity the screen remains blanked. I believe it has to do with the ArrayList which is null perhaps. Can someone tell me why my data is not being displayed. am on this bug for three days now :/
public class CopyOfSecondWheelAdapter extends AbstractWheelTextAdapter {
ArrayList<convertor_pst> PostList = new ArrayList<convertor_pst>();
public ImageLoader imageLoader;
Convertor main;
public CopyOfSecondWheelAdapter(Context context) {
super(context, R.layout.count_layout, NO_RESOURCE);
setItemTextResource(R.id.country_name);
}
#Override
public View getItem(int index, View cachedView, ViewGroup parent) {
View view = super.getItem(index, cachedView, parent);
ImageView img = (ImageView) view.findViewById(R.id.flag);
imageLoader.DisplayImage(PostList.get(index).getDevise(), img);
System.out.println("get item count:"+getItemsCount() );
TextView text = (TextView)view.findViewById(R.id.lib);
text.setText(PostList.get(index).getQuotite());
return view;
}
#Override
public int getItemsCount() {
return PostList.toArray().length;
}
#Override
protected CharSequence getItemText(int index) {
return PostList.get(index).getDevise().toString();
}
}
UPDATE:
In my Main class i have already an
ArrayList<convertor_pst> PostList = new ArrayList<convertor_pst>();
which is populated.
Here is my main class that is my convertor.class
ArrayList<convertor_pst> PostList = new ArrayList<convertor_pst>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.convertor);
context = this;
text_devise_two = (TextView)findViewById(R.id.text_spacetwo);
final WheelView country = (WheelView) findViewById(R.id.country);
country.setVisibleItems(10);
country.setViewAdapter(new FirstWheelAdapter(this));
edt_validate = (EditText)findViewById(R.id.edt_validate);
current_type_loc = (TextView)findViewById(R.id.current_type_loc);
refresh_header= (TextView)findViewById(R.id.refresh_header);
//set current time
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy");
String formattedDate = df.format(c.getTime());
refresh_header.setText(getResources().getString(R.string.mise_a_jour)+" "+formattedDate);
image_one = (ImageView)findViewById(R.id.image_one);
image_two = (ImageView)findViewById(R.id.image_two);
final WheelView currency = (WheelView) findViewById(R.id.currency);
currency.setVisibleItems(10);
currency.setViewAdapter(new CopyOfSecondWheelAdapter(this));
country.addChangingListener(new OnWheelChangedListener() {
#Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if (!scrolling) {
}
}
});
country.addScrollingListener( new OnWheelScrollListener() {
#Override
public void onScrollingStarted(WheelView wheel) {
scrolling = true;
}
#Override
public void onScrollingFinished(WheelView wheel) {
scrolling = false;
//1.
wheelSelector(country.getCurrentItem());
}
});
currency.addScrollingListener( new OnWheelScrollListener() {
#Override
public void onScrollingStarted(WheelView wheel) {
scrolling = true;
}
#Override
public void onScrollingFinished(WheelView wheel) {
scrolling = false;
//1.
secondWheel(currency.getCurrentItem());
}
});
country.setCurrentItem(1);
currency.setCurrentItem(3);
new loadingTask().execute();
}
/*1. Change currency */
public void wheelSelector (int id){
if (id==0){
current_type_loc.setText("EUR");
image_one.setBackgroundResource(R.drawable.eur);
}else {
current_type_loc.setText("USD");
image_one.setBackgroundResource(R.drawable.usd);
}
}
class loadingTask extends AsyncTask<Void, Void,Void> {
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
pd = ProgressDialog.show(Convertor.this, "", "Chargement en cours..", true);
super.onPreExecute();
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
doc = Jsoup.parse(getxml,"", Parser.xmlParser());
taux = doc.select("taux");
for (int i = 0; i < taux.size(); i++) {
PostList.add(new convertor_pst(taux.get(i).getElementsByTag("devise").text().toString(),
taux.get(i).getElementsByTag("dateCours").text().toString(),
taux.get(i).getElementsByTag("libelle").text().toString(),
taux.get(i).getElementsByTag("quotite").text().toString(),
taux.get(i).getElementsByTag("fixing").text().toString()));
}
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
envelope =
"soap content"
String requestEnvelope=String.format(envelope, "28-03-2013","true");
getxml = Util.CallWebService(URL,SOAP_ACTION,requestEnvelope);
System.out.println(getxml);
return null;
}
}
public void secondWheel(int index){
text_devise_two.setText(PostList.get(index).getDevise());
edt_validate.setText(" "+PostList.get(index).getFixing());
}
/*
*
* (non-Javadoc)
* #see android.app.Activity#onPause()
* check if activity go to background
*/
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (Util.isApplicationBroughtToBackground(Convertor.this)==true){
startActivity(new Intent(Convertor.this,Compte.class));
}
}
}
This is the original wheel adapter class
public class CopyOfSecondWheelAdapter extends AbstractWheelTextAdapter {
ArrayList<convertor_pst> PostList;
public ImageLoader imageLoader;
// Countries names
private String countries[] =
new String[] {"EUR", "USD","EUR", "USD","EUR", "USD","EUR", "USD","EUR", "USD","EUR", "USD"};
// Countries flags
private int flags[] = new int[] {R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd,R.drawable.eur, R.drawable.usd};
/**
* Constructor
*/
Convertor main;
public CopyOfSecondWheelAdapter(Context context) {
super(context, R.layout.count_layout, NO_RESOURCE);
setItemTextResource(R.id.country_name);
}
#Override
public View getItem(int index, View cachedView, ViewGroup parent) {
View view = super.getItem(index, cachedView, parent);
ImageView img = (ImageView) view.findViewById(R.id.flag);
img.setImageResource(flags[index]);
TextView text = (TextView)view.findViewById(R.id.lib);
text.setText("code");
return view;
}
#Override
public int getItemsCount() {
return countries.length;
}
#Override
protected CharSequence getItemText(int index) {
return countries[index];
}
}
As far as I understand
currency.setViewAdapter(new CopyOfSecondWheelAdapter(this));
this line creates the adapter, but you fill it up at this line :
new loadingTask().execute();
which is after, so you must call
yourAdapter.notifyDataSetChanged();
on your adapter to update the data.
Android Developer Help says
notifyDataSetChanged()
Notifies the attached observers that the
underlying data has been changed and any View reflecting the data set
should refresh itself.
So in your case you must
create an adapter (yourAdapter = new CopyOfSecondWheelAdapter ....)
assign it with the setViewAdater (WheelView.setViewAdapter(yourAdapter))
in the "postExecute" of your async task, do a call with yourAdapter.notifyDataSetChanged();
By the way, I am not sure to understand what you are doing, but in case you need to have a set of data displayed at two different locations, you don't need to duplicate (create a copy). The two list display can share the same adapter.
UPDATE
You have done an update to your question and I answer to that update :
In the original adapter the countries are not loaded in the async task. So when you assign the adapter, the display show the correct values because they are present in the adapter at the moment you assign it.
In your case, you load the values in the async task. When you create the adapter it is empty and you assign it empty, so the display shows an empty list. You should notify your display of the data change.
So in the original, no need to notify as the data is the correct one at the time of assignment. In your case you have to implement a notifyDataSetChanged(). Or change the type of adapter you are extending.
If I see it correctly, you have 2 times a variable name PostList which confuses you. One is created in your activity and one in your adapter and ass you call add() to the variable of your activity, the list in your adapter never gets the items.
Create a setter for the list in your adapter and set the list in your onPostExecute().