For the past two weeks, I have been struggling to produce a solution to store my checkbox values next to each of my listview items when I exit and open the app. Essentially, I have a custom BaseAdapter class which populates the listview with installed applications on the users phone and a checkbox next to each installed app. When I scroll down the checkbox states stay;however, when I click back or exit and come back it doesn't stay.
I used SharedPreferences in almost every way possible. My last way was using a for loop to create a SharedPreference for each one and restore the boolean in the for loop as well and to store a boolean true and false (for checked and unchecked respectively) in if and else statements.
Here is my code for my BaseAdapter class using SharedPreferences:
package com.ibc.android.demo.appslist.app;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import com.spicycurryman.getdisciplined10.app.R;
import java.util.List;
//
public class ApkAdapter extends BaseAdapter {
//Pastebin link: http://pastebin.com/LGRicg4U , http://pastebin.com/c4WfmhMK , http://pastebin.com/gFuuM4dY, http://pastebin.com/4Q7EP9G4
// http://pastebin.com/Te2g072w, http://pastebin.com/NLT5iUiA ,
SharedPreferences sharedPrefs;
List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;
boolean[] itemChecked;
String PACKAGE_NAME;
public ApkAdapter(Activity context, List<PackageInfo> packageList,
PackageManager packageManager) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
itemChecked = new boolean[packageList.size()];
}
private class ViewHolder {
TextView apkName;
CheckBox ck1;
TextView packageName;
}
public int getCount() {
return packageList.size();
}
public Object getItem(int position) {
return packageList.get(position);
}
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.installed_apps, null);
holder = new ViewHolder();
holder.apkName = (TextView) convertView
.findViewById(R.id.appname);
holder.ck1= (CheckBox)convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
//holder.ck1.setTag(packageList.get(position));
} else {
holder = (ViewHolder) convertView.getTag();
}
// ViewHolder holder = (ViewHolder) convertView.getTag();
PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
// Make sure to define it again!
PACKAGE_NAME = packageInfo.packageName;
final String appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
appIcon.setBounds(0, 0, 80, 80);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);
holder.apkName.setText(appName);
holder.ck1.setChecked(false);
if (itemChecked[position])
holder.ck1.setChecked(true);
else
holder.ck1.setChecked(false);
Log.d("just loaded??", PACKAGE_NAME);
Log.d("just loaded 2?", appName+position);
// CHANGE UP EVERYTHING! MAKE THIS SHIT WORK, TIGGA!
for(int i= 0; i<packageList.size(); i++){
sharedPrefs = context.getSharedPreferences(String.valueOf(i), Context.MODE_PRIVATE);
holder.ck1.setChecked(sharedPrefs.getBoolean(String.valueOf(i),false));
}
holder.ck1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = context.getSharedPreferences(String.valueOf(position), Context.MODE_PRIVATE).edit();
if (holder.ck1.isChecked()) {
itemChecked[position] = true;
holder.ck1.setChecked(true);
Log.i("This is", " checked: " + position);
editor.putBoolean(String.valueOf(position), true);
Log.d("put true", appName+position);
editor.apply();
} else {
itemChecked[position] = false;
holder.ck1.setChecked(false);
Log.i("This is", " not checked: " + position);
editor.putBoolean(String.valueOf(position), false);
Log.d("put false", appName+position);
editor.apply();
}
}
});
return convertView;
}
}
I have also created a DatabaseHandler class and Object class for using SQLite database to store my checkbox values instead after unsuccessfully storing it with SharedPreferences.
Here are the pastebin links so I don't clog the post:
DatabaseHandler class: http://pastebin.com/NzKhBiZ3
Object class: http://pastebin.com/Jp3BLXba
I know that there are many posts, blogs, and links on using custom adapters/listviews and saving checkboxes and button states etc using SQLite and SharedPreferences, but I assure you I have done my fair share of research for weeks, but I haven't been able to find something that works for my specific case.
Some examples:
Saving State of the checkbox in a custom list view with Checkboxes
http://blog.csdn.net/qu213/article/details/9289349
What would be the most optimal option in my particular case and How would I produce a solution in my situation to successfully save the checkbox states next to each installed app listview item that would be saved after I exit the application and re-enter? Some code would really be of help.
Thank you for your time.
Let me know if you need me to include any xml files.
I think I may have solved your problem. Please try thise code and see if it works.
for(int i= 0; i<packageList.size(); i++){
PACKAGE_NAME = packageInfo.packageName;
sharedPrefs = context.getSharedPreferences(PACKAGE_NAME, Context.MODE_PRIVATE);
Log.d("just got sharedpref??", PACKAGE_NAME);
holder.ck1.setChecked(sharedPrefs.getBoolean(PACKAGE_NAME,false));
Log.d("just got boolean??", PACKAGE_NAME);
}
holder.ck1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = context.getSharedPreferences(packageInfo.packageName, Context.MODE_PRIVATE).edit();
if (holder.ck1.isChecked()) {
itemChecked[position] = true;
holder.ck1.setChecked(true);
Log.i("This is", " checked: " + position);
editor.putBoolean(packageInfo.packageName, true);
Log.d("put true", packageInfo.packageName);
editor.apply();
} else {
itemChecked[position] = false;
holder.ck1.setChecked(false);
Log.i("This is", " not checked: " + position);
editor.putBoolean(packageInfo.packageName, false);
Log.d("put false", packageInfo.packageName);
editor.apply();
}
}
});
I passed in the packagenames for the keys. :)
what i would suggest in your case is to define an object that would deal with storing and retrieving state data. Lets say you have an AppHandler class as your main object and AppState class
for keeping state related data such as your checkboxes.
public class AppHandler {
private List<AppState> lastInstalledApp;
#transient
private Gson gson;
public void retrieve(SharedPreferences pref){
// parse the gson from shared preferences
// create the lastInstalledApp list
String jsonState = pref.getString("APP_STATE", null);
if(jsonState != null)
lastInstalledApp = gson.fromJson(jsonState, lastInstalledApp.getClass());
}
public void store(SharedPreferences pref){
// store the data to shared preferences
}
public List<AppState> getAppList(){
return lastInstalledApp;
}
public static class AppState{
protected boolean lastCheckbox;
protected String name;
public boolean isLastCheckbox() {
return lastCheckbox;
}
public void setLastCheckbox(boolean lastCheckbox) {
this.lastCheckbox = lastCheckbox;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Use shared preferences to store the entire AppHandler object as a JSON string (use GSON for this) so you access the storage file only once and not every time you want to access a single checkbox state. You can retrieve and store this data on your activity (the adapter initiate it with your appHandler.getAppList() data):
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.list_layout);
appHandler = new AppHandler();
appHandler.retrieve(getSharedPreferences());
}
#Override
protected void onDestroy() {
appHandler.store(getSharedPreferences());
super.onDestroy();
}
within the your custom adapter access the specific application by their package names in order to get the state of checkbox. you may even extend ArrayList and add some methods like getAppByName() so you can keep a clear distinction between objects.
when changing the checkbox state update the equivalent boolean value of the AppState object. This is in general terms.
Use sqlite database ...Everytime the user checks/unchecks the value make an entry in the database.
App1 checked
App2 checked
App3 checked
App4 unchecked
Related
I am developing an app which has a text message interface (something like Facebook Messenger, Whatsapp, etc). I want to be able to change the background color of all the chat bubbles (a ListView of TextViews) sent by the user when they pick a new color (in a NavigationView).
However, with the current code that I have, I only am able to change the color after the EditText used to compose the message is clicked again. Or I am able to only edit the first bubble sent, but as soon as the color is changed.
Here's what I tried :
ItemColor1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
Toast.makeText(activity, "Couleur mise à jour", Toast.LENGTH_SHORT).show();
currentTheme = position;
SharedPreferences.Editor editor = pref.edit();
editor.putInt("indexColorSelected",currentTheme);
editor.apply();
chatAdapter.notifyDataSetChanged();
//the following changes only the first message sent
for(int i=0; i<chatAdapter.getCount(); i++){
ChatData message = chatMessageList.get(position);
TextView msg = activity.findViewById(R.id.text);
msg.setText(message.body);
msg.setBackgroundResource(R.drawable.user_color1);
}
}
});
ChatData is a custom Class that I created which looks like this :
public class ChatAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
private ArrayList<ChatData> chatMessageList;
private Context mContext;
ChatAdapter(Activity activity, ArrayList<ChatData> list) {
mContext = activity;
chatMessageList = list;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
...
}
Color drawable:
<corners
android:bottomRightRadius="5dp"
android:radius="40dp"/>
<gradient
android:angle="45"
android:endColor="#01f1fa"
android:startColor="#0189ff"
android:type="linear" />
</shape>
getView() method of the Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
ChatData message = chatMessageList.get(position);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.msglist, parent, false);
TextView msg = vi.findViewById(R.id.text);
msg.setText(message.body);
LinearLayout layout = vi.findViewById(R.id.message_layout);
LinearLayout parent_layout = vi.findViewById(R.id.message_layout_parent);
inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// if message is mine then align to right
if (message.isMine) {
layout.setGravity(Gravity.RIGHT);
int couleurBubble = getCouleurSelectionnee();
switch(couleurBubble){
case R.color.c1: msg.setBackgroundResource(R.drawable.user_color1); break;
case R.color.c2: msg.setBackgroundResource(R.drawable.user_color2); break;
case R.color.c3: msg.setBackgroundResource(R.drawable.user_color3); break;
case R.color.c4: msg.setBackgroundResource(R.drawable.user_color4); break;
case R.color.c5: msg.setBackgroundResource(R.drawable.user_color5); break;
case R.color.c6: msg.setBackgroundResource(R.drawable.user_color6); break;
case R.color.c7: msg.setBackgroundResource(R.drawable.user_color7); break;
case R.color.c8: msg.setBackgroundResource(R.drawable.user_color8); break;
default: break;
}
parent_layout.setGravity(Gravity.RIGHT);
}
// If not mine then align to left
else {
layout.setGravity(Gravity.LEFT);
msg.setBackgroundResource(R.drawable.bot_chat);
parent_layout.setGravity(Gravity.LEFT);
}
return vi;
}
I don't really know where to go from here so any kind of help would be appreciated. If you want me to provide more code, let me know and I will.
Thank you.
I'm sharing how I would do it. Maybe, this can help you.
There's some strange issue because you are calling notifyDataSetChanged(). This would be enough to re-draw all message bubbles.
My ideia is:
Add a int variable to the adapter class (mColorResource). This variable will point to the proper drawable that should be used (like R.drawable.user_color1).
public class ChatAdapter extends BaseAdapter {
int mColorResource;
ChatAdapter(Activity activity, ArrayList<ChatData> list, int initialColorResource) {
mContext = activity;
chatMessageList = list;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// You must receive the color on the construtor
mColorResource = initialColor;
}
// Use this method to update the color (when user select a new color)
public void setColor(int newColorResource) {
mColorResource = newColorResource;
}
public View getView(int position, View convertView, ViewGroup parent) {
...
// Note how this if-else is cleaner now
if (message.isMine) {
layout.setGravity(Gravity.RIGHT);
msg.setBackgroundResource(mColorResource);
parent_layout.setGravity(Gravity.RIGHT);
} else {
layout.setGravity(Gravity.LEFT);
msg.setBackgroundResource(R.drawable.bot_chat);
parent_layout.setGravity(Gravity.LEFT);
}
...
}
}
Then, when a color is selected, find the proper drawable based on the view clicked and pass it to the adapter:
ItemColor1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
Toast.makeText(activity, "Couleur mise à jour", Toast.LENGTH_SHORT).show();
currentTheme = position;
SharedPreferences.Editor editor = pref.edit();
editor.putInt("indexColorSelected", currentTheme);
editor.apply();
// Here, you send the proper drawable...
// I'm not sure how you convert color selected to the drawable
// So, add your logic to convert the button clicked to a drawable here
// like R.drawable.user_color1
chatAdapter.setColor(R.drawable.NAME_OF_THE_COLOR);
// Request to re-draw all items from the list (all bubbles)
chatAdapter.notifyDataSetChanged();
}
});
Also, on your activity, you create the adapter with the last used color. Something like:
#Override
protected void onCreate(#Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
// Set a default color (if user is open you app for the first time)
int chatColor = R.drawable.user_color1;
// Add your logic to read the shared preference and convert that last color used to a valid drawable.
// Like chatColor = pref.getInt(indexColorSelected, R.drawable.user_color1) etc....
// Set the color in the adapter.
chatAdapter = newAdapter(this, mChatDataList, chatColor);
}
I have search now for hours through the internet and have found nothing substantial so far. The thing that I want to do is a multi choice preference view, that disables the last item and reenables it, if it is not alone anymore.
I through so far about taking the super class force read the private variables in there to write my own onPrepareDialogBuilder(AlertDialog.Builder builder). Which is configuring its own OnMultiChoiceClickListener that jumps in, in the moment where has only one item left. The problem here is, that I use a bad practice force read of a private variable and that I have so far no idea how to get the checkbox item and how to disable it. But I think looking even deeper into the Android SDK will solve this problem.
At the end, if nothing works, solving the problem with doing an overwrite the OnPreferenceChangeListener to display a toast if the user has less than one item selected. But user friendliness is a high value, that needs to be earned and that often isn't easy.
Thx.
import android.content.Context;
import android.preference.MultiSelectListPreference;
import android.util.AttributeSet;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import georg.com.flironetest_01.Variables.Units;
/**
* Created by Georg on 16/03/16.
*/
public class UnitMultipleSelectorPreference extends MultiSelectListPreference {
public UnitMultipleSelectorPreference(Context context, AttributeSet attrs) {
super(context, attrs);
List<CharSequence> humanU = new ArrayList<>();
List<CharSequence> machineU = new ArrayList<>();
Units[] all = Units.values(); // Units is a enum with a rewriten to string statement.
for (Units elem : all) {
humanU.add(elem.toString());
machineU.add(elem.name());
}
setEntries(humanU.toArray(new CharSequence[humanU.size()]));
setEntryValues(machineU.toArray(new CharSequence[machineU.size()]));
Set<String> mU = new HashSet<>();
mU.add(Units.C.name());
mU.add(Units.K.name());
setDefaultValue(mU);
}
}
Okay. To answer my own question here after the motto "self is the man": I ended up with programming my own preference panel. Below is the code. If somebody likes to look over it and give some times how to make it even more stable: feel free.
But to sum up what I did: I created my own ArrayAdapter. But DialogPreference didn't allowed me to create my own multi selector. You need to change the final dialog fragment to create a working multi selector list (see here: https://stackoverflow.com/a/17907379/5759814). That is not an easy task if you work with the DialogPreferences. The reason is these few amounts of code:
/**
* Shows the dialog associated with this Preference. This is normally initiated
* automatically on clicking on the preference. Call this method if you need to
* show the dialog on some other event.
*
* #param state Optional instance state to restore on the dialog
*/
protected void showDialog(Bundle state) {
Context context = getContext();
mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE;
mBuilder = new AlertDialog.Builder(context)
.setTitle(mDialogTitle)
.setIcon(mDialogIcon)
.setPositiveButton(mPositiveButtonText, this)
.setNegativeButton(mNegativeButtonText, this);
View contentView = onCreateDialogView();
if (contentView != null) {
onBindDialogView(contentView);
mBuilder.setView(contentView);
} else {
mBuilder.setMessage(mDialogMessage);
}
onPrepareDialogBuilder(mBuilder);
getPreferenceManager().registerOnActivityDestroyListener(this);
// Create the dialog
final Dialog dialog = mDialog = mBuilder.create();
if (state != null) {
dialog.onRestoreInstanceState(state);
}
if (needInputMethod()) {
requestInputMethod(dialog);
}
dialog.setOnDismissListener(this);
dialog.show();
}
As you can see here is a method triggered to change my dialog builder with onPrepareDialogBuilder, but it doesn't seem like that there is any other function triggered afterwards, that would allow me to change the dialog directly after its creation. And the second idea of changing the onPrepareDialogBuilder so that I can init everything there, doesn't really help, because I end up with displayed dialog windows. That lead me to my decision of creating my completely own Preference class. With that decision I loose all those nice prepared functions like onRestoreInstanceState and Co, but I now have an application with a much more persistent flow, that doesn't do any stupid things when I select zero units for my thermal view.
Below the non commented code. I'm sorry, but I think its simple enough for everybody who landing here.
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import georg.com.flironetest_01.Variables.Units;
/**
* Created by Georg on 16/03/16.
*/
public class UnitMultipleSelectorPreference extends Preference implements DialogInterface.OnClickListener, Preference.OnPreferenceClickListener {
String[] human_entries = null;
String[] machine_entries = null;
public SharedPreferences prev;
public UnitMultipleSelectorPreference(Context context, AttributeSet attrs) {
super(context, attrs);
prev = getSharedPreferences();
List<String> humanU = new ArrayList<>();
List<String> machineU = new ArrayList<>();
Units[] all = Units.values();
for (Units elem : all) {
humanU.add(elem.toString());
machineU.add(elem.name());
}
human_entries = humanU.toArray(new String[humanU.size()]);
machine_entries = machineU.toArray(new String[machineU.size()]);
Set<String> mU = new HashSet<>();
mU.add(Units.C.name());
mU.add(Units.K.name());
setDefaultValue(mU);
setOnPreferenceClickListener(this);
}
boolean[] selected = new boolean[0];
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
if (prev == null)
return;
if (human_entries == null || machine_entries == null || human_entries.length != machine_entries.length ) {
throw new IllegalStateException(
"ListPreference requires an entries array and an entryValues array which are both the same length");
}
selected = new boolean[human_entries.length];
for (int i = 0; i < human_entries.length; i++)
selected[i] = prefSet.contains(machine_entries[i]);
String[] stringObj = new String[human_entries.length];
int i = 0;
for(CharSequence ch : human_entries)
stringObj[i++] = ch.toString();
builder.setAdapter(new MyAdapter(getContext(), android.R.layout.simple_list_item_multiple_choice, stringObj), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog mDialog = builder.create();
mDialog.getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
mDialog.getListView().setItemsCanFocus(false);
mDialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Manage selected items here
ListView mParent = (ListView)parent;
if (mParent.getCheckedItemCount() >= 1)
selected[position] = mParent.isItemChecked(position);
if (mParent.getCheckedItemCount() == 0)
mParent.setItemChecked(position, true);
}
});
mDialog.show();
i = 0;
for (boolean select : selected)
mDialog.getListView().setItemChecked(i++, select);
}
#Override
public boolean onPreferenceClick(Preference preference) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getContext());
mBuilder.setTitle(getTitle())
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, this);
onPrepareDialogBuilder(mBuilder);
return true;
}
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(), "W:"+which + " | " + Arrays.toString(selected), Toast.LENGTH_SHORT).show();
switch (which) {
case -1:
if (isPersistent()) {
prefSet = new HashSet<>();
for (int i = 0; i < selected.length; i++) {
if (selected[i])
prefSet.add(machine_entries[i]);
}
getEditor().putStringSet(getKey(), prefSet).apply();
Toast.makeText(getContext(), "W:"+which + " | " + getSharedPreferences().getStringSet(getKey(),null).toString(), Toast.LENGTH_SHORT).show();
}
return;
}
}
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
#Override
public boolean isEnabled(int n) {
return true;
}
}
Set<String> prefSet;
#Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
super.onSetInitialValue(restorePersistedValue, defaultValue);
prev = getSharedPreferences();
if(restorePersistedValue) {
prefSet = prev.getStringSet(getKey(), new HashSet<String>());
} else {
try {
prefSet = (Set<String>)defaultValue;
if(isPersistent())
getEditor().putStringSet(getKey(), prefSet);
} catch (ClassCastException e) {
Log.e("ERROR_CAST", "Error casting the default value to Set<String>.");
}
}
}
}
A really simple solution is to set a setOnPreferenceChangeListener and just return false if the new value would be empty.
All of the code is put into onCreatePreferences.
MultiSelectListPreference infoPreference = findPreference("information");
infoPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (size(newValue) == 0){
return false;
}
return true;
}
});
I'm creating an application which has following functionality.
User selects the sensors which are available in the device.
The UI shows the selected Sensor Name and readings in a Listview.(Values update real time)
I have a custom class to store sensor data (SenseData) and custom view to display sensor name and readings.
I wrote a custom Adaptor class which takes a list of SenseData to populate a list view component in my activity.
What I want to do is, take a list of SenseData.
I'm trying this for about a week and didn't get any solutions.
I'll be very grateful if someone can give me a hint.
EDITED:
Adaptor class
public class SensorAdaptor extends BaseAdapter{
Context context;
List<SenseData> senseData;
public SensorAdaptor(Context context, List<SenseData> senseData){
this.context = context;
this.senseData = senseData;
}
#Override
public int getCount() {
return senseData.size();
}
#Override
public Object getItem(int position) {
return senseData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
if(convertView == null){
view = inflater.inflate(R.layout.view_sensor, parent, false);
holder = new ViewHolder();
holder.name = (TextView) view.findViewById(R.id.senseName);
holder.data = (TextView) view.findViewById(R.id.senseReading);
view.setTag(holder);
}else{
view = convertView;
holder = (ViewHolder) view.getTag();
}
SenseData data = senseData.get(position);
holder.name.setText(data.getSensorName());
holder.data.setText(data.getSensorValues());
return null;
}
public class ViewHolder{
TextView name;
TextView data;
}
}
onSensorChanged method
#Override
public void onSensorChanged(SensorEvent event) {
this.event = event;
//SupportedSensors contains the predefined sensor types. sensor is a SenseData type object
Intent filter = new Intent(new
SupportedSensors().getType(event.sensor.getType()));
filter.putExtra("value", event.values[0]);
ctx.sendBroadcast(filter); //ctx is the context
}
Then I receive broadcasts with filters which have sensor names as action. But I couldn't find out a way to return a list in Broadcast receiver. SenseData class has setters and getters to set and get data.
public class SensorBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
switch(intent.getAction()){
case "accelerometer":
System.out.println("Accelerometer");
break;
case "gyroscope":
System.out.println("Gyroscope");
break;
case "gravity":
System.out.println("Gravity");
break;
case "proximity":
System.out.println("Proximity");
break;
case "pressure":
System.out.println("Pressure");
break;
case "light":
System.out.println("Light");
break;
case "magnetometer":
System.out.println("Magnetometer");
break;
case "rotation":
System.out.println("Rotation Vector");
break;
}
}
The receiver is registered in the Activity
IntentFilter filter = new IntentFilter("accelerometer");
SensorBroadcastReceiver receiver = new SensorBroadcastReceiver();
registerReceiver(receiver, filter);
I can add a List<SenseData> sensordatalist to SensorBroadcastReceiver but how to get values real time?
Thank you very much.
I was able to solve my problem by using the following method....
Create a class RealTimeSensorReader which reads the sensors.
public void onSensorChanged(SensorEvent event) {
RealTimeSensor realTimeSensor = new RealTimeSensor();
realTimeSensor.setName(AvailableSensors.getType(event.sensor.getType()).toUpperCase());
realTimeSensor.setValueX(event.values[0]+"");
realTimeSensor.setValueY(event.values[1]+"");
realTimeSensor.setValueZ(event.values[2]+"");
System.out.println(values);
TempStore.sensor.put(AvailableSensors.getType(event.sensor.getType()), realTimeSensor);
Intent intent = new Intent();
intent.setAction("sensor");
context.sendBroadcast(intent);
adaptor.notifyDataSetChanged();}
Define a separate class TempStore which contains two static fields
a ConcurrentHashMap and a LinkedList which are for store sensor
data and to populate the ListView separately.
public class TempStore {
public static ConcurrentMap<String, RealTimeSensor> sensor = new ConcurrentHashMap<>();
public static ArrayList<RealTimeSensor> realTimeSensors = new ArrayList<>();
In onSensorChanged method I put the sensor values with the sensor
name as the key to the HashMap. And send a broadcast whenever a
sensor is changed.
Created a broadcast listener which listen to that broadcast, and
clear the current LinkedList values and add the values set of the
HashMap.
TempStore.realTimeSensors.clear();
TempStore.realTimeSensors.addAll(TempStore.sensor.values());
What I need to know is whether this method is correct or wrong.
Thank you very much
This question has been answered before, but the solutions doesn't seem to work for me. I would like to know what the best way is to save an ArrayList.
I generate an ArrayList with all the installed applications on the phone. This list is shown in a ListView where the user can (de)select apps. This is all working fine. What I would like is that the Arraylist gets saved when the user presses a save button or when the activity calls onPause().
When the user returns to the list the user will see the list the way he saved/left it.
Here is my code:
onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_list);
loadApps();
loadListView();
addClickListener();
}
loadApps
private void loadApps(){
manager = getPackageManager();
apps = new ArrayList<AppDetail>();
if(apps.size()==0) {
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> availableActivities = manager.queryIntentActivities(i, 0);
for (ResolveInfo ri : availableActivities) {
AppDetail app = new AppDetail();
app.label = ri.loadLabel(manager);
app.name = ri.activityInfo.packageName;
app.icon = ri.activityInfo.loadIcon(manager);
app.allowed = false;
apps.add(app);
}
Log.i("applist", apps.toString());
}
}
AppDetail.class
public class AppDetail {
CharSequence label;
CharSequence name;
Drawable icon;
Boolean allowed;
loadListView
private void loadListView(){
list = (ListView)findViewById(R.id.apps_list);
adapter = new ArrayAdapter<AppDetail>(this, R.layout.list_item, apps) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.list_item, null);
}
ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
appIcon.setImageDrawable(apps.get(position).icon);
TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_label);
appLabel.setText(apps.get(position).label);
TextView appName = (TextView)convertView.findViewById(R.id.item_app_name);
appName.setText(apps.get(position).name);
if(list.isItemChecked(position)){convertView.setBackgroundColor(getResources().getColor(R.color.green));}
if(!list.isItemChecked(position)){convertView.setBackgroundColor(getResources().getColor(R.color.white));}
return convertView;
}
};
list.setAdapter(adapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
addClickListener
private void addClickListener() {
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int pos,
long id) {
checked = list.getCheckedItemPositions();
ArrayList<AppDetail> allowedApps = new ArrayList<>();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i)) {
allowedApps.add(adapter.getItem(position));
}
}
adapter.notifyDataSetChanged();
Log.i("", allowedApps.toString());
}
});
}
At this moment I'm creating two lists:
List: list of all apps
AllowedApps: list of checked (allowed) apps, to use in an other activity
If you need saving your list when activity is paused, you have several ways to do it. First you need define the private list field in your activity.
private ArrayList<AppDetail> allowedApps;
1) Make AppDetail serializable and use onSaveInstanceState
public class AppDetail implements Serializable {
CharSequence label;
CharSequence name;
Drawable icon;
Boolean allowed;
}
---------------- EDIT -----------------
I would change Drawable icon field for int icon.
In your loadApps() method change the setence app.icon = ri.activityInfo.getIconResource();
In yout loadListView method change the setence appIcon.setImageResource(apps.get(position).icon);
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("allowedApps", allowedApps);
}
Retrieve the list in onCreate method
if (savedInstanceState != null) {
allowedApps = (List<AppDetail>)savedInstanceState.getSerializable("allowedApps");
}else{
allowedApps = new ArrayList<AppDetail>();
}
2) Use onRetainCustomNonConfigurationInstance
Return the list in onRetainCustomNonConfigurationInstance
#Override
public Object onRetainCustomNonConfigurationInstance() {
return allowedApps;
}
Retrieve the list in onCreate method
Object allowedApps= getLastCustomNonConfigurationInstance();
if (allowedApps != null) {
this.allowedApps = (List<AppDetail>) allowedApps;
}else{
this.allowedApps = new ArrayList<AppDetail>();
}
I think you are looking for something like "Parcelable". It can save any ArrayList and retrieve back when you need it just like the Shared Preferences.
Please have a look here,
How to save custom ArrayList on Android screen rotate?
ArrayList is serializable. Save it as a serializable object in file on storage
I am fairly new to Java programming and was following a tutorial located here: http://coenraets.org/blog/android-samples/androidtutorial/
Where I copied the code I am having a problem with here: http://code.google.com/p/androidtutorial/source/browse/trunk/%20androidtutorial/EmployeeDirectory6/src/samples/employeedirectory/EmployeeDetails.java
EDIT: Thank You all. Thanks to #adamcodes for pointing out I totally missed that link where he put out the source code. It looks like he forgot to include that link in the step by step tutorial.
At about 25 lines down I'm getting an error at
protected ArrayList<EmployeeAction> actions;
which says "EmployeeAction cannot be resolved to a type" My question is does the class EmployeeAction have to be created
actions = new ArrayList<EmployeeAction>();
Even if I put "actions = new ArrayList();" in my code? If so what should the class contain?
package samples.employeedirectory;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class EmployeeDetails extends ListActivity {
protected TextView employeeNameText;
protected TextView titleText;
protected ArrayList<EmployeeAction> actions;
protected EmployeeActionAdapter adapter;
protected int employeeId;
protected int managerId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.employee_details);
employeeId = getIntent().getIntExtra("EMPLOYEE_ID", 0);
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT emp._id, emp.firstName, emp.lastName, emp.title, emp.officePhone, emp.cellPhone, emp.email, emp.managerId, mgr.firstName managerFirstName, mgr.lastName managerLastName FROM employee emp LEFT OUTER JOIN employee mgr ON emp.managerId = mgr._id WHERE emp._id = ?",
new String[]{""+employeeId});
if (cursor.getCount() == 1)
{
cursor.moveToFirst();
employeeNameText = (TextView) findViewById(R.id.employeeName);
employeeNameText.setText(cursor.getString(cursor.getColumnIndex("firstName")) + " " + cursor.getString(cursor.getColumnIndex("lastName")));
titleText = (TextView) findViewById(R.id.title);
titleText.setText(cursor.getString(cursor.getColumnIndex("title")));
actions = new ArrayList<EmployeeAction>();
String officePhone = cursor.getString(cursor.getColumnIndex("officePhone"));
if (officePhone != null) {
actions.add(new EmployeeAction("Call office", officePhone, EmployeeAction.ACTION_CALL));
}
String cellPhone = cursor.getString(cursor.getColumnIndex("cellPhone"));
if (cellPhone != null) {
actions.add(new EmployeeAction("Call mobile", cellPhone, EmployeeAction.ACTION_CALL));
actions.add(new EmployeeAction("SMS", cellPhone, EmployeeAction.ACTION_SMS));
}
String email = cursor.getString(cursor.getColumnIndex("email"));
if (email != null) {
actions.add(new EmployeeAction("Email", email, EmployeeAction.ACTION_EMAIL));
}
managerId = cursor.getInt(cursor.getColumnIndex("managerId"));
if (managerId>0) {
actions.add(new EmployeeAction("View manager", cursor.getString(cursor.getColumnIndex("managerFirstName")) + " " + cursor.getString(cursor.getColumnIndex("managerLastName")), EmployeeAction.ACTION_VIEW));
}
cursor = db.rawQuery("SELECT count(*) FROM employee WHERE managerId = ?",
new String[]{""+employeeId});
cursor.moveToFirst();
int count = cursor.getInt(0);
if (count>0) {
actions.add(new EmployeeAction("View direct reports", "(" + count + ")", EmployeeAction.ACTION_REPORTS));
}
adapter = new EmployeeActionAdapter();
setListAdapter(adapter);
}
}
public void onListItemClick(ListView parent, View view, int position, long id) {
EmployeeAction action = actions.get(position);
Intent intent;
switch (action.getType()) {
case EmployeeAction.ACTION_CALL:
Uri callUri = Uri.parse("tel:" + action.getData());
intent = new Intent(Intent.ACTION_CALL, callUri);
startActivity(intent);
break;
case EmployeeAction.ACTION_EMAIL:
intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{action.getData()});
startActivity(intent);
break;
case EmployeeAction.ACTION_SMS:
Uri smsUri = Uri.parse("sms:" + action.getData());
intent = new Intent(Intent.ACTION_VIEW, smsUri);
startActivity(intent);
break;
case EmployeeAction.ACTION_REPORTS:
intent = new Intent(this, DirectReports.class);
intent.putExtra("EMPLOYEE_ID", employeeId);
startActivity(intent);
break;
case EmployeeAction.ACTION_VIEW:
intent = new Intent(this, EmployeeDetails.class);
intent.putExtra("EMPLOYEE_ID", managerId);
startActivity(intent);
break;
}
}
class EmployeeActionAdapter extends ArrayAdapter<EmployeeAction> {
EmployeeActionAdapter() {
super(EmployeeDetails.this, R.layout.action_list_item, actions);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
EmployeeAction action = actions.get(position);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.action_list_item, parent, false);
TextView label = (TextView) view.findViewById(R.id.label);
label.setText(action.getLabel());
TextView data = (TextView) view.findViewById(R.id.data);
data.setText(action.getData());
return view;
}
}
}
Java is statically typed, and everything you want to refer to at compile time needs to exist. So yes - you have to create the EmployeeAction, if you need it. (And you need to import it with import yourpackage.EmployeeAction; at the top of the class)
The type definition if the list <EmployeeAction> is there to enforce compile time safety for the elements of the list. It means "you can only put instances of EmployeeAction in this collection". This is useful when later you access the collection - the compiler can guarantee that the list contains only EmployeeAction instances
Yes it definitly have to exists (EmployeeAction class). For what it should contains, I'd say probably a constructor that take parameters like these : (String location, String phone, EmployeeAction.ACTION*) and then setters and getters for these values.
EmplyeeAction is a not Java known class. Then, it could be imported (if it exists) or created.
The list is irrelevant here. A few lines down, your code says:
actions.add(new EmployeeAction("Call office", officePhone, EmployeeAction.ACTION_CALL));
Yes, you do need a class EmployeeAction to compile that code. It's probably introduced in a part of the tutorial you overlooked or skipped.
you have to have a EmployeeAction class.
since you say that you are new to java it will be good if go through this tutorial