Android, cannot be cast in setOnItemClickListener - java

I want to use this line of code I've seen in several examples:
lvRadios.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) lvRadios.getItemAtPosition(position);
int idRadio = cursor.getInt(cursor.getColumnIndex("ID"));
Log.i("ID", String.valueOf(idRadio));
ShowResults(position, view);
}
});
But I get the following error:
java.lang.ClassCastException: java.lang.String cannot be cast to android.database.Cursor
I'm using Android 6.0 with Android Studio 2.1.2
I hope you can help me.
Regards.

Your error is telling you that you're trying to turn a string into a cursor. For the operation you're performing, you wouldn't get a cursor. You would get a UI element or something tied to a UI element.
Comment out what you already have inside of the onItemClick and replace it with:
String radioString = (String) lvRadios.getItemAtPosition(position);
//Debug: Show a toast to see what information you're getting
Toast.makeText(getApplicationContext(), radioString, Toast.LENGTH_LONG);
Then, you can do what you want once you know the result.

Related

Getting a button to work in the list view

I am new to android and my code has got a bit messy. I have successfully created a list view extended from item_layout.xml. When I click on the list view It works exactly how I want it. However in each item of the list view I have a button that when clicked I want the item of the list to delete.
When researching I have come across that you need to create a customer adapter to do this however I have come so far in the project that I wouldn't even know where to start.
This code it used successfully to when the list items are clicked it works. This is just put in the Main Activity class
mylist.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
}
}
);
I populate the list using this function just outside the main activity class. It is needed to be written like this as It gets the items from a database and has to be called depending on different circumstances
private void populatelistView() {
Cursor res = userDb.getAllRows();
String[] fromFeildnames = new String[]{ DatabaseUser.KEY_1, DatabaseUser.KEY_2};
int[] toViewIds = new int[]{R.id.textViewNum, R.id.textViewItem};
SimpleCursorAdapter myCursorAdaptor;
myCursorAdaptor = new SimpleCursorAdapter(getBaseContext(), R.layout.item_layout, res, fromFeildnames, toViewIds, 0);
//ListView mylist = (ListView) findViewById(R.id.listViewID);
mylist.setAdapter(myCursorAdaptor);
}
I would like to be able to get the button on each items to work by not changing much of what I have already written. I have tried just using the following code. But because it is in a different xml layout it display an error of null reference towards the item button
delete.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
Toast.makeText(getApplicationContext(), "Button " + position, Toast.LENGTH_SHORT).show();
}
}
);
Please could someone help me make the button work without changing much code or give me a step by step tutorial on how to add an adapter but make my populateListView function do the same thing.
Ps. I have looked at so many tutorials about list adapters but can't find ones that are for my specific need

Android CustomListView OnClick

Hello Everyone Iam was Making a AndroidStudio List view app and I noticed custom list view is much better so I decided to switch into custom !
I have problem with the onclick listener of my CustomListView I cant get the position of clicked item so I cant gather info from that
Because :
This is My Listview OnClick ListenerCode (From internet)
list.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// here the position element of this method will give you the row number on which the click happened.
//Then you can get individual values like name[position], address[position] and then pass these values through an intent
}
});
It Dosent Work And Gives a Error on OnItemClickListener() !
Saying :
Error:(118, 37) error: incompatible types: <anonymous android.support.v7.internal.widget.AdapterViewCompat.OnItemClickListener> cannot be converted to android.widget.AdapterView.OnItemClickListener
Error:(120, 37) error: cannot find symbol class AdapterView
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
I used ALT+ENTER to fix The Error it Creates a
AdapterViewCompat.
Behind the OnItemListClickListener() and Suggest to implent methods so I Do ALT+ENTER and it will be like :
list.setOnItemClickListener(new AdapterViewCompat.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// here the position element of this method will give you the row number on which the click happened.
//Then you can get individual values like name[position], address[position] and then pass these values through an intent
}
#Override
public void onItemClick(AdapterViewCompat<?> adapterViewCompat, View view, int i, long l) {
}
});
it Gives Error on first OnItemClick Funtion I though its a Duplicate of second one so I delete that but when I do it give error on second one too !
Saying :
Error:(119, 37) error: incompatible types: <anonymous android.support.v7.internal.widget.AdapterViewCompat.OnItemClickListener> cannot be converted to android.widget.AdapterView.OnItemClickListener
SO the code from internet dosent work
Question : OnClickListener For CustomListView maybe?
ListView's setOnItemClickListener listener takes android.widget.AdapterView.OnItemClickListener as param and you're trying to set this android.support.v7.internal.widget.AdapterViewCompat.OnItemClickListener there and hence the error.
Change it to right listener type and the error should be resolved.
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
//implement methods
})

Attempting to use an incompatible return type - android studio

Can someone help me with this error that I keep getting?
The program that I'm trying to make is online streaming radio.
I need to return the value "value" so i can use it in another java class.
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,ch);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public int onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), mPlayer.class);
i.putExtra("name", ch.get(position));
int value = listView.getItemAtPosition(position);
startActivity(i);
return value;
}
}
The problem occurs in this line:
int value = listView.getItemAtPosition(position);
getItemAtPosition() returns an Object, so you must convert it to an int before assigning it to an int variable. You should do this only if you're sure that your listView contains integers, otherwise you'll get a ClassCastException. Do it like this:
int value = (int)listView.getItemAtPosition(position);
Also, if you startActivity() before using the value anywhere, then it does not make much sense in getting it. Use putExtra to pass it to your next activity if you wish to use it there.

Cannot resolve method 'getText()' on TextView / error: boolean cannot be dereferenced?

I'm getting this error when attempting to call 'getText' on a TextView in an if statement. The strange thing is that when I call the same method on the same view in a Log statement right before the if statement, no error pops up. I've looked at many other similar questions, and I've tried cleaning and rebuilding my build to no avail. When I try to run it, the error that comes up is error: boolean cannot be dereferenced. TextView is not a primitive type, so I believe I should be able to call a method on it. I have the method below, please let me know if any other code is required.
private void doSearch(Cursor query) {
// get a Cursor, prepare the ListAdapter
// and set it
Cursor c = query;
startManagingCursor(c);
String[] from = new String[] {"QUANTITY", "_id"};
int[] to = new int[] {android.R.id.text1};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, from, to);
mListView.setAdapter(cursorAdapter);
Log.e("doSearch method:", "has been called");
mListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, log with the TextView text
Log.e("doSearch method:", "Answer: " + ((TextView) view).getText()); //no error here
if(cMap.containsKey((TextView) view).getText()){ //error here
//start new activity
} else if (chMap.containsKey((TextView) view).getText()){//error here
//start new activity
} else if (aMap.containsKey((TextView) view).getText()){//error here
//start new activity
}
}
});
}
Thanks so much for any help.
You are passing the View as an argument to containsKey:
cMap.containsKey((TextView) view).getText()
should be
cMap.containsKey(((TextView) view).getText())
The error you are getting basically says that cMap.containsKey(view).getText() is not a boolean

Android putExtra issue

I have scoured SE and google and found what I thought were decent examples of how to implement putExtra() in tandem with getStringExtra().
The trouble I seem to be unable to resolve is that my putExtra data never appears to be getting retrieved from my getStringExtra call in the target activity.
I've tried numerous SE examples where others have asked this question countless times and yet it never seems to get me closer to a working base to expand on.
My primary activity's put is as follows;
(First, I tried this with no luck)
// Click handler for group list items
lvGroups.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int gid = groupIds.get(arg2);
Intent intent = new Intent(RadSMS_Activity.this, RadSMS_CreateGroup.class);
intent.putExtra("SELECTED_GROUP_ID", gid);
startActivity(intent);
finish();
}
});
(Then, I tried this. Also with no luck.)
// Click handler for group list items
lvGroups.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int gid = groupIds.get(arg2);
Intent target = new Intent();
target.putExtra("SELECTED_GROUP_ID", gid);
Intent intent = new Intent(RadSMS_Activity.this, RadSMS_CreateGroup.class);
startActivity(intent);
finish();
}
});
My target activity that I want to extract the value from is the following;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.creategroup);
String strGID = getIntent().getStringExtra("SELECTED_GROUP_ID");
selectedGID = new Long(strGID);
// ... additional code would be here
}
Function truncated for brevity's sake.
So, according to everything I've seen so far, it appears I'm doing it right, but when I put a breakpoint at the line where selectedGID gets assigned its value, strGID is always null. This is really beginning to make me crazy.
Can anyone please tell me if I have done something incorrect?
gid is an int.
You are putting an int.
You appear to be trying to retrieve a string.
Consider:
int gid= getIntent().getIntExtra("SELECTED_GROUP_ID",-1);
You are putting an integer value while getting it as a string. It will always return null. Use intent.getExtras().getInt() instead of intent.getStringExtra().

Categories

Resources