1

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;
                   }
               });
        }
     }

2 Answers2

0

I think the problem with this line TextView mytext=(TextView)filelist.getChildAt(position); mytext is null because when code tries to get the view from the viewList the assigned value is null so try to find the reason for this.

0

Your getItemAtPosition gets the data associated with the specified position in the list, not a View. Instead of TextView mytext=(TextView)filelist.getChildAt(position); use the view provided, which is actually the item being clicked within your ListView. Try this instead:

  public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            if(position==id){
                view.setBackgroundColor(Color.WHITE);
                TextView mytext=(TextView)view;
                mytext.setTextColor(Color.parseColor("#6600cc"));
            }else{
                view.setBackgroundColor(Color.parseColor("#6600cc"));
                TextView mytext=(TextView)view;
                mytext.setTextColor(Color.WHITE);
            }
        

        return true;
    }

Since,you use android.R.layout.simple_list_item_1 to inflate the view item within the ListView, you can type cast it as a TextView and call setText() on it.

Note: android.R.layout.simple_list_item_1 consist of just a TextView. Refer to this Q&A.

Kalai
  • 503
  • 1
  • 3
  • 12