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
})
Related
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.
Problem description: I wanted a "delete" function which could perform delete/remove of the selected entry in a listview and at the same time delete the residing video file string in the Video_List directory then it refresh the content of the listview?
I'm rather new in android/java can someone help me with it? Do scroll down to evaluate the problem i'm facing please!! Can someone tell me what is the specific code i should add into my current codes to perform the above mention function??
Since you have stored your selection into "item" object then in deleteFile() method you need to retreive the file path from that object, for that to work add the line:
model.absolutePath =
mfile.getAbsolutePath();
in getVideoFiles() method 'for' loop.
also before onCreate state:
ListViewAdapter lv;
then in getVideoFiles at the end state:
lv = new ListViewAdapter(this, R.layout.row, videoItems);
setListAdapter(lv);
finally in deleteFile() you need to state:
File myFile = new File(item.absolutePath);
lv.notifyDataSetChanged();
and that should work!
You defined an override onListItemClick but this code is never been called. You should also register the a listener to the view you're using. Check how android handle user interface events.
newListView.setOnItemClickListener(this);
Do you want to delete the adapter or do you want to delete a row/entry in the list? If latter, then update videoItems and call notifyDataSetChanged on your adapter. If you really want to delete the adapter, then just set it to NULL or have it refer to some other ListAdapter instance and the GC will take care of the rest.
#Override // create contextuel menu
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Action");
menu.add(0,100,1,"delete");
}
//////////////////////////////////////////////////
#Override // Select an item
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case 100:
public void onClick(DialogInterface dialog, int id) {
db.delete_item(info.id);
//here update list view
}
});
////////////
public boolean delete_item(long id){
return db.delete("name_table", "_id="+id, null)>0;}
////////////////
Problem description: I wanted a "delete" function which could perform delete/remove of the selected entry in a listview and at the same time delete the residing video file string in the Video_List directory then it refresh the content of the listview?
I'm rather new in android/java can someone help me with it? Do scroll down to evaluate the problem i'm facing please!! Can someone tell me what is the specific code i should add into my current codes to perform the above mention function??
Since you have stored your selection into "item" object then in deleteFile() method you need to retreive the file path from that object, for that to work add the line:
model.absolutePath =
mfile.getAbsolutePath();
in getVideoFiles() method 'for' loop.
also before onCreate state:
ListViewAdapter lv;
then in getVideoFiles at the end state:
lv = new ListViewAdapter(this, R.layout.row, videoItems);
setListAdapter(lv);
finally in deleteFile() you need to state:
File myFile = new File(item.absolutePath);
lv.notifyDataSetChanged();
and that should work!
You defined an override onListItemClick but this code is never been called. You should also register the a listener to the view you're using. Check how android handle user interface events.
newListView.setOnItemClickListener(this);
Do you want to delete the adapter or do you want to delete a row/entry in the list? If latter, then update videoItems and call notifyDataSetChanged on your adapter. If you really want to delete the adapter, then just set it to NULL or have it refer to some other ListAdapter instance and the GC will take care of the rest.
#Override // create contextuel menu
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Action");
menu.add(0,100,1,"delete");
}
//////////////////////////////////////////////////
#Override // Select an item
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case 100:
public void onClick(DialogInterface dialog, int id) {
db.delete_item(info.id);
//here update list view
}
});
////////////
public boolean delete_item(long id){
return db.delete("name_table", "_id="+id, null)>0;}
////////////////
Problem description: I wanted a "delete" function which could perform delete/remove of the selected entry in a listview and at the same time delete the residing video file string in the Video_List directory then it refresh the content of the listview?
I'm rather new in android/java can someone help me with it? Do scroll down to evaluate the problem i'm facing please!! Can someone tell me what is the specific code i should add into my current codes to perform the above mention function??
Since you have stored your selection into "item" object then in deleteFile() method you need to retreive the file path from that object, for that to work add the line:
model.absolutePath =
mfile.getAbsolutePath();
in getVideoFiles() method 'for' loop.
also before onCreate state:
ListViewAdapter lv;
then in getVideoFiles at the end state:
lv = new ListViewAdapter(this, R.layout.row, videoItems);
setListAdapter(lv);
finally in deleteFile() you need to state:
File myFile = new File(item.absolutePath);
lv.notifyDataSetChanged();
and that should work!
You defined an override onListItemClick but this code is never been called. You should also register the a listener to the view you're using. Check how android handle user interface events.
newListView.setOnItemClickListener(this);
Do you want to delete the adapter or do you want to delete a row/entry in the list? If latter, then update videoItems and call notifyDataSetChanged on your adapter. If you really want to delete the adapter, then just set it to NULL or have it refer to some other ListAdapter instance and the GC will take care of the rest.
#Override // create contextuel menu
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Action");
menu.add(0,100,1,"delete");
}
//////////////////////////////////////////////////
#Override // Select an item
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case 100:
public void onClick(DialogInterface dialog, int id) {
db.delete_item(info.id);
//here update list view
}
});
////////////
public boolean delete_item(long id){
return db.delete("name_table", "_id="+id, null)>0;}
////////////////
Problem description: I wanted a "delete" function which could perform delete/remove of the selected entry in a listview and at the same time delete the residing video file string in the Video_List directory then it refresh the content of the listview?
I'm rather new in android/java can someone help me with it? Do scroll down to evaluate the problem i'm facing please!! Can someone tell me what is the specific code i should add into my current codes to perform the above mention function??
Since you have stored your selection into "item" object then in deleteFile() method you need to retreive the file path from that object, for that to work add the line:
model.absolutePath =
mfile.getAbsolutePath();
in getVideoFiles() method 'for' loop.
also before onCreate state:
ListViewAdapter lv;
then in getVideoFiles at the end state:
lv = new ListViewAdapter(this, R.layout.row, videoItems);
setListAdapter(lv);
finally in deleteFile() you need to state:
File myFile = new File(item.absolutePath);
lv.notifyDataSetChanged();
and that should work!
You defined an override onListItemClick but this code is never been called. You should also register the a listener to the view you're using. Check how android handle user interface events.
newListView.setOnItemClickListener(this);
Do you want to delete the adapter or do you want to delete a row/entry in the list? If latter, then update videoItems and call notifyDataSetChanged on your adapter. If you really want to delete the adapter, then just set it to NULL or have it refer to some other ListAdapter instance and the GC will take care of the rest.
#Override // create contextuel menu
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Action");
menu.add(0,100,1,"delete");
}
//////////////////////////////////////////////////
#Override // Select an item
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case 100:
public void onClick(DialogInterface dialog, int id) {
db.delete_item(info.id);
//here update list view
}
});
////////////
public boolean delete_item(long id){
return db.delete("name_table", "_id="+id, null)>0;}
////////////////