How to add list view items to an array list? or a work around to achieve the below? - Android Q&A, Help & Troubleshooting

In the below code, where the item’s position and title is getting saved in list_items works fine if list_items and items are ArrayList. For ex;
Code:
ArrayList.add(ArrayList.get(position));
But I am trying to use listview:
Code:
ArrayList.add(Listview.get(position));
The problem exists on line # 10
Code:
1. final Listadapter Adapter = new Listadapter(this,packageList1, packageManager);
2. items.setAdapter(Adapter);
3. items.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL);

4. items.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
5. @Override
6. public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

7. count = count +1;

8. mode.setTitle(count + "items selected");
9. //problem resides in the below line

10. [B]list_items.add(items.get(position));[/B]

11. }

In the above code, on selecting an item from listview I am trying to save it in an Arraylist i.e list_items which could be used in onActionItemClicked method for further action on that respective item.
Any ideas on how to solve or find a workaround to use a listview in the above situation?

Related

[Q] How select preferred network operators automatically?

Hallo,
I want to select preferred network operators automatically from device.
After I search for it, I found following:
Code:
final int EVENT_AUTO_SELECT_DONE = 300;
Message msg = mHandler.obtainMessage(EVENT_AUTO_SELECT_DONE);
phone.setNetworkSelectionModeAutomatic(msg);
So I need an instance of class "Phone". I tried this:
Code:
String phoneFactoryName = "com.android.internal.telephony.PhoneFactory";
String phoneName = "com.android.internal.telephony.Phone";
Class phoneFactoryClass = Class.forName(phoneFactoryName);
Class phoneClass = Class.forName(phoneName);
Method getDefaultPhone = phoneFactoryClass.getMethod("getDefaultPhone");
Object phoneObject = getDefaultPhone.invoke(null);
But the method "getDefaultPhone" can only called by looper.
Next I tried this:
Code:
String phoneAppName = "com.android.phone.PhoneApp";
Class phoneAppClass = Class.forName(phoneAppName);
Method getInstanceMethod = phoneAppClass.getMethod("getInstance");
Object phoneObject = getInstanceMethod.invoke(null);
But I get a ClassNotFoundException.
Can anybody help me, please?
sorry ,i can't help you
Yes it can be instantiated. But you have to overcome a couple of hurdles:
In your AndroidManifest.xml set
Code:
android:sharedUserId="android.uid.phone"
within the manifest-Tag. This is required to prevent a SecurityException from being thrown when protected Intents are sent by the methods you may invoke (like android.intent.action.SIM_STATE_CHANGED).
Set
Code:
android:process="com.android.phone"
in your application-Tag. This is required to allow the invocation of getDefaultPhone / makeDefaultPhone.
To do all this your app must be signed with the system signature key.

[Q] How to get a Reference ID to accept a variable input?

Hi, I'm trying to create an application, and i require to use a variable input for a reference ID to display different arrays here is what i am using at the moment
Code:
public void verbTenses() {
Spinner verbs = (Spinner) findViewById(R.id.verbs);
Spinner tenses = (Spinner) findViewById(R.id.verb_tenses);
[COLOR="RoyalBlue"]ListAdapter adapter = R.array.(verbs.getSelectedItem() + "_" + tenses.getSelectedItem());[/COLOR]
ListView list = (ListView) findViewById(R.id.ListView01);
list.setAdapter(adapter);
}
the error area is highlighted
Thank you
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A

[Q] Referencing to dialog's progressbar within AsyncTask

Hey guys,
I am currently developing a mechanism for my app to store its database into a pdf file.
However, I am a bit stuck with the progressbar.
The progressbar is inside of a dialog.
The dialog is created in the main activity and then the asynctask is called, which should update the dialog's progressbar.
the method to achieve this, looks like this:
Code:
protected void onProgressUpdate(Integer... progress){
ProgressBar progressBar = (ProgressBar)((Activity)mContext).progressDialog.findViewById(R.id.pdfProgressBar);
progressBar.setProgress(progress[0]);
}
progressDialog is handed over within the constructor in the main activity:
(progressDialog = dialog)
Code:
Dialog dialog = new Dialog (this);
dialog.setContentView(R.layout.pdfprogress_dialog);
dialog.setTitle("Creating shopping list...");
dialog.setCancelable(false);
dialog.show();
createPDFTask pdfTask = new createPDFTask(this, elements, dialog);
pdfTask.execute("blabla");
dialog.dismiss();
Now eclipse throws me a funny error:
"progressDialog cannot be resolved or is not a field"
I believe there is soemthing wrong with the syntax of
Code:
ProgressBar progressBar = (ProgressBar)((Activity)mContext).progressDialog.findViewById(R.id.pdfProgressBar);
any idea what?

[Q] NullPointerException when trying to access MainActivity from different class

Hi!
I am trying to develop an android app with a google map v2, location service and some control buttons.
But I don't want to put all these things inside one MainActivity class. So I thought I could split all the code into some more classes. The MainActivity shall controll all the GUI things and react on map or location events...
Now I have the following problem. Inside my onCreate I instanziate the additional classes:
Code:
// Preferences as singleton
pref = Prefs.getInstance(this.getApplicationContext());
pref.loadSettings();
// Set up the location
loc = new Locations(pref);
loc.setCallback(this);
map = new MyMap(pref);
It seems to work fine. But inside the MyMap class every time I start the app a null pointer exception is thrown. When I am calling MyMap() the following code will be executed:
Code:
[...]
private Prefs pref;
private GoogleMap mMap;
[...]
public MyMap(Prefs prefs) {
pref = (Prefs) prefs;
if (mMap == null) {
FragmentManager fmanager = getSupportFragmentManager();
mMap = ((SupportMapFragment) fmanager.findFragmentById(R.id.map)).getMap();
[...]
}
The line with the findFragmentById is the one that causes the exception.
If I write
Code:
SupportMapFragment f = ((SupportMapFragment) fmanager.findFragmentById(R.id.map));
f is allways null. But how can I access the fragments and view elements defined within my MainActivity?
It works if I put the code inside my MainAcitivity.
Every class extends "android.support.v4.app.FragmentActivity"
I tried to save the application context within my Prefs() class, so that I can access it from everywhere.
But I don't know how to use it inside my additional classes.
How to share the "R" across all my classes?
Can someone help me please?
Thank you very much!!
Thorsten
Are you having trouble adding a Map to a Fragment? If so, then you may take a look at this tutorial. I haven't tried it myself since I couldn't install Google Play Services on my development device. If it helps, do write back, as I am definitely going to try it myself soon.

need to replace setUserVisibleHint in AndroidX as it is deprecated, can anybody help

I have 3 fragments attached with viewpager.
I have a single database and all three fragments data is connected to each other so if i make any change in fragment 1 & it should also be reflected in fragment 2(when it is visible to user)
previously i was using
Code:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
getFragmentManager().beginTransaction().detach(this).attach(this).commit();
}
}
but setUserVisibleHint is deprecated so i need to replace it but cloudn't find proper solution.
I tried to used "detach().attach()" in onResume but it goes to infinite loop then.
Note: i tried to use "notifyDataSetChanged()" instead of detach.attach but it doesn't work properly and takes time to reflect data.
I tried to recreate activity but it push the recyclerView on first item. I want my recyclerView to be show on the same state where it previously was like if it was on item no 24 before leaving it then whenever user comeback on same fragment by doing detach.attach data is refreshed and recyclerView is on same position.
Please tell me the alternative solution for detach.attach on every desired fragment is visible to user.
Thanks

Categories

Resources