0

I cannot find a solution. I have no errors, yet after clicking on the list item the onItemClick method does not work. I put the onItemClick method in various places in the code. I thought maybe the problem is that there is a GridLayout in the ListView and I click GridLayout and not ListView?

Activity class:

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import butterknife.BindView;
import butterknife.ButterKnife;

import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.niemiec.reliablealarmclock.R;
import com.niemiec.reliablealarmclock.view.activity.addAlarm.sound.file.adapter.MusicListAdapter;


public class MySoundsActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>, MySoundsContractMVP.View {

    private MusicListAdapter adapter;

    private ActionBar actionBar;
    private MySoundPresenter presenter;

    @BindView(R.id.sounds_list_view)
    ListView filesListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sounds);
        ButterKnife.bind(this);
        actionBar = getSupportActionBar();
        addBackArrow();
        createMySoundsPresenter();

        showMusicList();
    }

    private void showMusicList() {
        getSupportLoaderManager().initLoader(1, null, this);

        filesListView.setOnItemClickListener((AdapterView<?> adapterView, View view, int position, long id) -> {
            Toast.makeText(MySoundsActivity.this, "TEKST!", Toast.LENGTH_SHORT).show();
            System.out.println("TEKST!");
        });
    }

    private void createMySoundsPresenter() {
        presenter = new MySoundPresenter();
        presenter.attach(this);
    }

    private void addBackArrow() {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @NonNull
    @Override
    public Loader<Cursor> onCreateLoader(int id, @Nullable Bundle args) {
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        CursorLoader cl =  new CursorLoader(this, uri, null, null, null, null);
        cl.setSortOrder(MediaStore.MediaColumns.TITLE + " ASC" );
        return cl;
    }

    @Override
    public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
        cursor.moveToFirst();
        adapter = new MusicListAdapter(this, cursor);
        filesListView.setAdapter(adapter);
    }

    @Override
    public void onLoaderReset(@NonNull Loader<Cursor> loader) {

    }
}

MusicListAdapter class:

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.niemiec.reliablealarmclock.R;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

public class MusicListAdapter extends BaseAdapter {
    private Cursor cursor;
    private Context context;
    private LayoutInflater inflater;

    public MusicListAdapter(Context context, Cursor cursor) {
        this.context = context;
        this.cursor = cursor;
        inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return cursor.getCount();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder = null;

        cursor.moveToPosition(position);

        if (view == null) {
            view = inflater.inflate(R.layout.music_list_row, parent, false);
            viewHolder = new ViewHolder();

            viewHolder.title = view.findViewById(R.id.title_text_view);
            viewHolder.author = view.findViewById(R.id.author_text_view);
            viewHolder.playButton = view.findViewById(R.id.play_image_button);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.author.setText(getAuthor());
        viewHolder.title.setText(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
        viewHolder.playButton.setImageResource(R.drawable.ic_baseline_play_circle_outline_24);

        return view;
    }

    private String getAuthor() {
        String author = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST));
        if (author.equalsIgnoreCase("<unknown>")) {
            author = context.getResources().getString(R.string.author_unkown);
        }
        return author;
    }

    class ViewHolder {
        TextView title;
        TextView author;
        ImageButton playButton;
    }
}

  • In MusicListAdapter, getView() method you could perhaps try view.setOnClickListener(new OnClickListener() { public void onClick(View v) { DO YOUR CLICK ACTION HERE } }); I believe this could replace the onItemClick method. I seem to remember having problems with that as well, but I use the RecyclerView nowdays (which replaced ListView) and onClickListener or onTouchListener for my child-views. – Vanheden Jun 16 '21 at 04:22
  • @Vanheden thank you for the hint but unfortunately it didn't work. Every time I want to retrieve an object using the View object it returns null. Maybe I'm doing something wrong. – Arkadiusz Niemiec Jun 17 '21 at 23:02
  • Well then I suppose your problem may be that the Object getItem(int position) method is returning null in your BaseAdapter? You probably need to make your cursor return the correct object or is this intentional? – Vanheden Jun 19 '21 at 03:48
  • I would recommend that you consider using a RecyclerView instead of ListView. This may not be the root of your problem or the solution but this is the new and more efficient way to make ListView-like or GridLayoutViews. If you are new to coding and this is your first list, it is better to learn the most up to date solution so you don't have to update it later on. Also then I would be more sure what I'm talking about regarding how that works. https://stackoverflow.com/questions/26728651/recyclerview-vs-listview – Vanheden Jun 19 '21 at 03:57

0 Answers0