Java/Android, is it possible to get contents of an Object? - java

As the title suggests. I've done a bit of research and tried Jackson which seems like it should have worked but I had ridiculous amount of errors while trying to use it so that seemed a no go.
Basically my problem is this, I have my list view populated by an adapator as so:
friendsArrayAdapter = new FriendsArrayAdapter(
NetPlay.this, R.layout.rowlayout, friends);
listView.setAdapter(friendsArrayAdapter);
friendsArrayAdapter.notifyDataSetChanged();
and Then I want to handle onClick events within this listview, to copy the contents of that list view to a variable:
listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Object o = listView.getItemAtPosition(position);
Object o = arg0.getItemAtPosition(position);
//mText.setText(o);
}
});
Now this all works fine, but the value returned is the actual Object ID/Reference
Shows as something#Friends.21312. What I want is the actual contents which should be a Name + ID.
Is this even possible or do I need to redo my whole list view population?

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//friends is the array that is given to the Adapter.
//given friends = Friend[]
Friend f = friends[position];
f.getName();
f.getID();
}
This is only a guess, please post your whole code for better help!

Related

How to simulate selection on AutoCompleteTextView

I have an AutoCompleteTextView object with the name myAutoCompleteTextView.
myAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parentView, View selectedItemView, int position, long id) {
//do something
}
}
Now I try to simulate a specific selection of an item on it like that:
myAutoCompleteTextView.setText("some item from the array of this AutoCompleteTextView");
The result is that I see the required item in the AutoCompleteTextView,
but nothing happens. Only the text shown over there.
When the user choose manually the item everythings is fine.
Can someone help me how can I simulate a selection?
I gues u call a function or something in your onItemClick function.
why dont u just call this code outside your listener ? Or am I missing something here?
something like
public void onItemClick(AdapterView<?> parentView, View selectedItemView, int position, long id) {
MyObject myObject = parentView.getItemAtPosition(position);
doStuffWithObject(myObject);
}
And u can call the function outside your adapter with any reference.
Is that what you are looking for?
This code as well as place the same order:
codselect.CommandText = "select* from team where nome Like '" + TextBox2.Text + "%'"

android how do i find out which view was clicked in a listview

im quite a newby to android and java. I created a my own adapter and listView and i want to know how do i find out which view or row was pressed in my listView so i can do a specific task for row was picked (task includes sending data and info to another activity).
I thought that maybe the position variable contains the name of the row clicked so i tried it but when a row is clicked the app crashes
hangarList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getApplicationContext(), position,Toast.LENGTH_SHORT);
}
});
can someone help
it could be that position is an integer, and it's expecting a string, and also the show() needs to be called on your toast to see the message.
hangarList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getApplicationContext(), position+"",Toast.LENGTH_SHORT).show();
}
});
First, you're on the right track regarding finding out which row was clicked.
The error lays in your use of Toast.makeText(). There are two versions of that method:
makeText(Context context, int resId, int duration)
makeText(Context context, CharSequence text, int duration)
Now in your vode you do Toast.makeText(..., position, ...) which invokes the first version of the method (as position is an integer), but because position is not a resource id the app crashes.
So, you have to use the second version and to do that, pass a CharSequence (or String) as second parameter: Toast.makeText(..., position+"", ...)
Btw. to get the actual value of the row, you can use hangarList.getItemAtPosition(position).
The views that make up the rows in the ListView are indexed like an array. The position value is the index of the row or view that was pressed. You can use the position value to look up the information associated with the clicked view.
Your app is crashing because Toast.makeText(Context, int, int) expects a string resource id integer for its second argument. When you give it the position as the id, it can't find that string resource and throws an exception. To display the index of the view that was clicked try:
hangarList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getApplicationContext(),
"View at position " + position + " clicked.",
Toast.LENGTH_SHORT);
}
}
Application crashes because of your Toast.You should use Toast like
Toast.makeText(context, text, duration).show();
And you can fix it in simple way just like this:
String positionString=Integer.toString(position);
Toast.makeText(getApplicationContext(), positionString,Toast.LENGTH_SHORT).show();
And there is no problem without Toast.As you say which item of listview is clicked?
For controlling which item is clicked You can use position variable of method.
Regards.. Omer

making onItemSelected visible to entire class

I'm new to this android/java stuff. I have this onItemSelected that will toast what has been selected in the spinner. I want to have the string resumeTableName accessible throughout my entire class that has the value of the selected spinner object. Right now it toasts the selected value however at other places in my class the resumeTableName remains null. I thought the public modifier would make it visible. How do I make this visible, do I use some sort of return?
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
String resumeTableName = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), resumeTableName,
Toast.LENGTH_LONG).show();
}
Use a static String resumeTableName; declared in your class and
in your onItemSelected write
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
resumeTableName = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), resumeTableName,
Toast.LENGTH_LONG).show();
}
In this way the value of resumeTableName updated by the method will be the same for the whole class.
If You are new in Android programming there is a nice solution for the problems with visibility of variables.
You have to create class in your package:
public class GlobalVar extends Application{
private int dummy1;
public int getDummy1() {
return dummy1;
}
public void setDummy1(int dummy1) {
this.dummy1 = dummy1;
}
}
then in any place in Your application You can get/set this data, by using:
GlobalVar gV = (GlobalVar)GetApplicationContext();
and then You just modify them by using Getters/Setters or any public methods from this class. I think it's very good solution for set of variables that You use often from different places of code.

Adding onItemClickListener to each listview item while items are still being added

I'm not really sure how to explain this problem, but I'll do my best. I have an app that records sound and after you're done recording, you have to rename the file and then the file is added to listview in another activity.
I have tab layout, so adding files to listview is a little bit more complicated, here's how I do it:
Recording activity:
if (getParent() instanceof FileNameProvider) {
((FileNameProvider) getParent()).onNewFileName(newFileName);
}
Tab layout activity:
public void onNewFileName(Editable filename) {
LocalActivityManager activityManager = getLocalActivityManager();
getTabHost().setCurrentTabByTag("Library");
RecordedLibrary recLib = (RecordedLibrary) activityManager.getActivity("Library");
recLib.setFileName(filename);
}
And finally, I get the new filename in my library (listview) activity:
public void setFileName(final Editable filename) {
Log.d("2", "Set filename from first activity " + filename);
}
So, everytime I set a filename, it's automatically added to listview with this code:
public void setFileName(final Editable filename) {
Log.d("2", "Set filename from first activity " + filename);
//LISTVIEW (declared globally)
fileNames.add(filename.toString());
listView = (ListView) findViewById (R.id.mainListView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, fileNames);
listView.setAdapter(listAdapter);
Now what I want to do next is to also automatically add onItemClickListener for each item added. I know I could do this with switch statement like this:
switch (position){
case 0:
//code
break;
}
But this isn't possible in my case because everytime I record a file, this file has a different path, since there's a different name. This is how I tried to do it:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast toast = Toast.makeText(getApplicationContext(), filename, Toast.LENGTH_SHORT);
toast.show();
}
});
This doesn't work either because everytime I add a new file to the listview, the filename variable changes, so this code works only as long as I only add one item to listview. As soon as I add the second item to listview, toast will show the second file's name no matter which item I click.
I hope everyone understand's the problem. Let me know if I should add any more info.
SOLUTION:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast toast = Toast.makeText(getApplicationContext(), fileNames.get(arg2), Toast.LENGTH_SHORT);
toast.show();
}
});
As you can see, I simply changed the 'filaname' to 'fileNames.get(arg2)', please see this answer for more details.
You should NOT create and set a new ListAdapter each and every time you set a file name.
Adapters should only be created once and attached to a ListView once per lifecycle, which is probably why the second time you add a file, only the second file name is being returned. You are overriding the previous Adapter and data each time you call setFileName.
You don't need to add an onClickListener to every object in your list. As you indicated above that you're use a seperate activity to house your ListView I would (1) make that activity extend ListActivity (2) use onListItemClick
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
This way if you keep track of which file is added in which order (above you mention you're using an ArrayList to store file names) you could use the position or id value (depending on what kind of adapter is backing your list) to retrieve the file name. You can play around with which collection works best for retrieving the file data (though the array list probably works fine if you're adding and removing these values in a predictable order).
You can also do this using the onItemClickListener you have in place. Arg2 is the position of the item in the list. Just take that value and get the file you need from the ArrayList storing the file extention.

Accessing every textview within gridview and setting colour to white

Sorry if this is a weird question, I just really don't understand how to get this working.
basically, I want a behavior so that when you click one of my gridview items, it highlights red, This works fine, but I need it to only have 2 highlighted letters at a time from the click method.
Outside this method is
public void onItemClick(AdapterView<?>parent, View v, int position, long id)
{
//selection.setText(Words[position]);
}
This is inside my method which gets fired
public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3)
{
Toast.makeText(getApplicationContext(), ""+arg2,Toast.LENGTH_SHORT).show();
((TextView) arg1).setTextColor(getResources().getColor(android.R.color.holo_red_light));
((TextView) arg1).setTextSize(9);
counter++;
if(counter == 2)
{
//this needs to select everything in the gridview and set to white
}
}
Does anyone have any ideas how I would go about it ( if it's even possible)
Sorted it.
if(counter == 3)
{
gridView.setAdapter(adapter);
// do the check if its a word or not
counter = 0;
}
works like a charm

Categories