I have a ListView that lists all the files in a folder into the ListView as an array. I seem to have a problem with long selection of the ListView's single item in that, when i long click an exception is thrown that Attempt to invoke virtual method 'void android.widget.TextView.setTextColor(int)' on a null object reference
I want to achieve a change of color for the selected child view Background and the Text color of the TextView inside that View...
Here is the complete code i tried to implement my AdapterView.OnLongItemClickListener
public class video_player extends AppCompatActivity implements GestureDetector.OnGestureListener {
ListView filelist;
List files;
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medias_combine);
filelist = (ListView) findViewById(R.id.filelist);
files=new ArrayList<>();
//Defining folder to read files from
File store = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File[] myfiles=store.listFiles();
//Adding filenames to the ArrayList
for(int i=0;i<myfiles.length;i++){
//Adding a file filter
if(myfiles[i].isFile()&& myfiles[i].getName().contains(".mp4")){
files.add(myfiles[i].getName());
}
//Build the ListView
ListAdapter myadapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, files);
filelist.setAdapter(myadapter);
//ListView Item long item click listener
filelist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
for(int i=0;i<filelist.getCount();i++){
//Problem is in this loop
if(position==id){
view.setBackgroundColor(Color.WHITE);
TextView mytext=(TextView)filelist.getChildAt(position);
mytext.setTextColor(Color.parseColor("#6600cc"));
}else{
view.setBackgroundColor(Color.parseColor("#6600cc"));
TextView mytext=(TextView)filelist.getItemAtPosition(position);
// TextView text=(TextView)parent.getItemAtPosition(position);
mytext.setTextColor(Color.WHITE);
}
}
return true;
}
});
}
}