Make an AdapterArray become an IntArray because I can't send to another activity an adapter array.
I'm creating a racing timer in Android Studio Java and I have saved my data about the race (example: time of all turns) in an Array Adapter. But when I try to send it to another Activity the function doesn't accept ArrayAdapter so I created a function to turn ArrayAdapter into an IntArray the problem is that whenever I run that function the program crashes.
I'm looking for an answer to this problem by correcting my code or doing a completely different one if I had to.
public int[] AdapterToInt(ArrayAdapter adapter)
{
int[] a = new int[adapter.getCount()];
for (int i = 0; i < adapter.getCount(); i++)
{
a[i] = Integer.valueOf(adapter.getItem(i));
}
return a;
}
public void onButtonClickSaveTimes(View view) {
int[] array;
array = AdapterToInt(adapter1);
TextView textoexemplo = (TextView) findViewById(R.id.textViewhorario);
Intent myIntent = new Intent(getBaseContext(), Main5Activity.class);
myIntent.putExtra("car1", AdapterToInt(adapter1));
myIntent.putExtra("car2", AdapterToInt(adapter2));
myIntent.putExtra("car3", AdapterToInt(adapter3));
myIntent.putExtra("car4", AdapterToInt(adapter4));
myIntent.putExtra("car5", AdapterToInt(adapter5));
startActivity(myIntent);
}
I expect it to send the ArrayAdapter to another activity or at least making my function work and turn ArrayAdapter into ArrayInt so i can send it through myIntent.putExtra, but the actual output is a app crash.
The problem is likely here:
a[i] = Integer.valueOf(adapter.getItem(i));
Since adapater.getItem(i) returns an object that you likely will not be able to convert into an integer, you are probably going to have issues here.
You could instead use adapter.getItemId(i) which would return a long as opposed to an object.
However, that idea also doesn't seem to great so I would try to see if anything from here works for you.
And if none of that works it probably makes more sense to just send over all the data you need to reconstruct your ArrayAdapter to the other activity.
This link may also help:
How do I pass custom ArrayAdapter Object between Intents with serialization
Related
I know there are alot of duplicates with issues like these but pleas do read mine and help me out.
I am very new to Android development as such I coded these with my own instincts and limited guides available.
I'd like to implement an indexable (A-Z) side panel just like in contacts. All the posts available are array strings with hard-coded entries but mine aren't, I am using DB Browser for SQLite and then populate each data in a cardview.
I have referred to (https://github.com/woozzu/IndexableListView/blob/master/src/com/woozzu/android/widget/IndexableListView.java) and tried all the solutions mentioned related to this post but to no avail. I believe I simply coded it the wrong way as the entries are hard-coded but mine aren't.
I am currently following this guide, (http://androidopentutorials.com/android-listview-with-alphabetical-side-index/) and have amended some codes here and there which I thought is necessary for my situation, such as adding my database into an array. However, there are multiple errors and I don't know what to do.
As such, I have no idea how to solve these errors proceed on. Please do help me out and provide me with, perhaps a step-by-step tutorial or even better video guides so that I could follow through.
Below are what I have tried:
Declaration
//indexable list view
Map<String, Integer> mapIndex;
List<String> dbList = new ArrayList<>();
OnCreate
//indexable list view
String[] dbList= database.getKeyword();
Arrays.asList(dbList);
dbList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, dbList));
etIndexList(dbList);
displayIndex();
//indexable list view
private void getIndexList(String[] dbList){
mapIndex = new LinkedHashMap<String, Integer>();
for(int i = 0; i < dbList.length; i++){
String db = dbList[i];
String index = db.substring(0,1);
if(mapIndex.get(index) == null)
mapIndex.put(index, i);
}
}
private void displayIndex(){
LinearLayout indexLayout = (LinearLayout) findViewById(R.id.side_index);
TextView textView;
List<String> dbList = new ArrayList<String>(mapIndex.keySet());
for (String index : dbList){
textView = (TextView) getLayoutInflater().inflate(R.layout.side_index_item, null);
textView.setText(index);
textView.setOnClickListener(this);
indexLayout.addView(textView);
}
}
public void onClick(View view){
TextView selectedIndex = (TextView) view;
dbList.setSelection(mapIndex.get(selectedIndex.getText()));
}
The errors are:
If you require any more codes do let me know and I will update this post. Thank you in advance.
follow this lib for indexing list no need to write extra function this lib manage all the case
Answer to the errors that you are getting:
You are calling dbList.setAdapter(): dbList is not a ListView or RecyclerView
Call setAdapter() method on appropriate ListView or RecyclerView variable.
You are passing this to setOnClickListener() method: this is pointing to your KnowledgeActivity and not a View.OnClickListener.
Solution: Either implement View.OnClickListener in your activity or call new OnClickListener like this,
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}});
database.keyword() returns List and you are trying to cast it to String[]
I am implementing a Room database (because I want to move away from Loaders) and I have a query that selects objects based on the IN operator:
#Query(SELECT * FROM table WHERE icon IN(:icons))
LiveData<List<Result>> getResults(String[] icons);
The issue is that the :icons array is generated dynamically during runtime, first I generate placeholders for it and then replace them with the values, like so:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] iconsArray = generateIconArray();
mViewModel = ViewModelProviders.of(this, new MyViewModelFactory(getActivity().getApplication(), iconsArray)).get(MyViewModel.class);
mViewModel.getResults().observe(this, new Observer<List<Result>>() {
#Override
public void onChanged(#Nullable List<Result> results) {
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
mRecyclerView.setLayoutManager(layoutManager);
GridAdapter adapter = new GridAdapter(getActivity(), results);
mRecyclerView.setAdapter(adapter);
}
});
}
The problem is that I receive an empty RecyclerView, but when I pass an array with a single icon, the query works normally.
Is it not possible to achieve the same in Room because it checks queries during compile time?
And if not, instead of that should I just use db.query(...) in my repository constructor?
I know this sort of question was asked here, but it also says that they are working on it, and I couldn't find any signs of it.
EDIT:
In case someone stumbles upon this question, I made a small mistake while implementing this feature, the code provided in onActivityCreated actually works. It's also worth mentioning that it works with String[] and lists, but not with a single string: "ab,cd,ef".
I made a small mistake while implementing this feature, the code provided in onActivityCreated actually works. It's also worth mentioning that it works with String[] and List, but not with a single String: "ab,cd,ef", which was my mistake.
I have a main class that holds two array lists of class objects. Long story short, I'm calling an activity via startActivityForResult to get a return of the position of a spinner item selection. I have two boolean variables in the main activity that basically tell the application which list is being worked with: booleans listASelected and listBSelected. If listASelected = true, then listBSelected = false and visa-versa. In the result activity I basically generate the spinner this way:
MainActivity mainAct = new MainAtivity();
Spinner dropdown = (Spinner)findViewById(R.id.mainMenu);
for(int i = 0;i<mainAct.pickThreeNumbers.size(); i++){
optionsPickThree.add(mainAct.pickThreeNumbers.get(i).getNumbers(1)+mainAct.pickThreeNumbers.get(i).getNumbers(2)+mainAct.pickThreeNumbers.get(i).getNumbers(3));
}
for(int r = 0; r<mainAct.pickFourNumbers.size();r++){
optionsPickFour.add(mainAct.pickFourNumbers.get(r).getNumbers(1)+mainAct.pickFourNumbers.get(r).getNumbers(2)+mainAct.pickFourNumbers.get(r).getNumbers(3)+mainAct.pickFourNumbers.get(r).getNumbers(4));
}
if(mainAct.pickThreeSelected){
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, optionsPickThree);
}else{
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, optionsPickFour);
}
dropdown.setAdapter(adapter);
Then, I use this to do my setResult.
Intent output = new Intent();
if(mainAct.pickThreeSelected){
output.putExtra("slot", position);
output.putExtra("list", "PickThree");
}else{
output.putExtra("slot", position);
output.putExtra("list", "PickFour");
}
setResult(1,output);
finish();
}
The issue is that nomatter what it always works as if mainAct.pickThreeSelected = true, even if I know it is false because other sections of the app are functioning properly. What I assume is happening is that the class object is accessing the variable's initialized values instead of their current values at the time of execution. That, or I'm an idiot and there's a different way to accomplish this.
Any help, fellas? Thanks in advance!
Make pickThreeSelected as static. That way you will update the same instance everytime.
What is the default value that you have set to the mainAct.pickThreeSelected??If its true,make it as false.Then if user selects the listA change the value to true.
When jumping from Activity to activity via Cordova, plugin, i want to be able to put additional information into a bundle and pass it along with the intent. I seem to have issues getting the current activity and then getting a value via
public int getCounter(){return counter;}
I have a Activity Definition which, onCreate will set counter to a value from the passed in bundle.
I have a Cordova plugin then which i am working with, which will carry out the intent to next activity.
Echo Class is a mediary which will jump between acticities based on html clicks.
//In the Class: public class Echo extends CordovaPlugin
private void launchSecondActivity(String data, CallbackContext cc){
Bundle b = new Bundle();
int tmp = ((SecondaryActivity)cordova.getActivity()).getCounter();
tmp++;
b.putInt("id", tmp);
Intent i = new Intent(cordova.getActivity().getApplicationContext(), SecondaryActivity.class);
i.putExtras(b);
cordova.getActivity().startActivity(i);
cc.success();
}
It seems that it causes a seg fault of sorts, when i am trying to assign counter to tmp, in the line:
int tmp = ((SecondaryActivity)cordova.getActivity()).getCounter();
Is there something i am doing wrong? I am trying to get the currently active activity, and then call a public function in that activity.
End Goal: I am trying to take an int, and keep passing it into intents, incremented. So that way, it will know how deep into the activity chain it is. I am working on nested state saving and curious as to the depth i am at, at any given time. The tmp being passed into the activity will be incremented each time, so it will maintain a depth.
Instead of creating a class with get counter, storing the int in the Bundle and passing it to the next activity would be far easier.
Example:
//send item to plugin.
int item = 0; // or whatever it is
Bundle b = new Bundle();
b.put("ident", item);
Intent i = new Intent();
i.putExtras();
//...
And then on the next activity.
public void onCreate(Bundle savedInstanceState)
{
int item = getIntent().getExtras().getInt("ident");
}
Then you can do with it what you will. The answer overall while the previous activity as the old number. What does that mean? It means that you can increment and do whatever you want with it when passing it to the next activity. It is copied, so you can incrememnet and store it recursively. Allowing you to even say something like:
if (item ==0){/*set new source for Activity*/}
else if(item == 1){/*set source 2*/}
//etc.
I'm making an Android app and my implementation of adapter.add() seems to crash the app. I had to use Log tags to find that my code strangely becomes stuck in a loop. Here's the code:
int i =0;
if(cards.size()>0){
Log.i("KOOL","Checked arraylist size =" + cards.size());
while(i < cards.size()){
Log.i("KOOL","Inside while loop");
adapter.add(cards.get(i));
i++;
}
Log.i("KOOL","Added data to adapter");
adapter.notifyDataSetChanged();
Log.i("KOOL","Finished OnActivityResult");
}
The app gets up to the Log tag "Inside while loop" and repeats it until the app crashes. I know that cards.size() is 2 right before the while loop so i see no reason for the app to crash. Am I using the adapter correctly? Please help!
Your question is missing some context. Luckily I know that your adapter is of type ArrayAdapter and its being initialized with cards. When your loop starts, cards.size() is 2. But then you add something to it, making cards.size() be 3. And then... I guess you get the point. You either add something to your adapter/list pair through the adapters add, or through your list add. If you add it to both, then you are adding it two times.
Here is the code for ArrayAdapter::add:
public void add(T object) {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.add(object);
} else {
mObjects.add(object);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
}
where mOriginalValues is the very same list you used to construct the adapter. mObjects is a filtered version of them.