V
V
Vasiliy_Pankratov2020-04-22 20:52:11
Java
Vasiliy_Pankratov, 2020-04-22 20:52:11

The event handler does not work in the fragment. What to do?

Well, look, I, then, have a fragment with which I display data from a tyrynet in a ListView using a SimpleAdapter. When I hang an event handler on this ListView, nothing happens at all. Here is the code:

public class TopFragment extends BaseFragment {
@BindView(R.id.listView)
ListView listView;
@BindView(R.id.tv_empty)
TextView tvEmpty;
@BindView(R.id.progress_bar)
ProgressBar progressBar;

private MenuItem mSearchMenuItem;

String url = "https://api.xn--41a.ws/";
private final static Type TYPE = new TypeToken<List<GetSong>>(){}.getType();
private final static Gson GSON = new Gson();

ArrayList<Map<String, Object>> data = new ArrayList<>();
final String ATTRIBUTE_NAME_TITLE = "title";
final String ATTRIBUTE_NAME_PERFORMER = "performer";

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);


}

@Override
protected int getLayoutId() { return R.layout.fragment_top; }

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (getContext() == null) {
        return;
    }
    refresh("");
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getContext(),"Clicked",Toast.LENGTH_SHORT).show();
        }
    });
}


@Override
public void onStart() {
    super.onStart();
    //refreshSongList();
}

@Override
public void onStop() {
    //cancelSongLoadTask();
    super.onStop();
}



@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    mSearchMenuItem = menu.findItem(R.id.action_search);

    final SearchView searchView = (SearchView) mSearchMenuItem.getActionView();
    searchView.setQueryHint(getString(R.string.search_for_songs));
    searchView.setMaxWidth(Integer.MAX_VALUE);


    final SearchView.OnQueryTextListener onQueryTextListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            refresh(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            return true;
        }
    };


    mSearchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            searchView.setOnQueryTextListener(onQueryTextListener);
            tvEmpty.setVisibility(View.GONE);
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            searchView.setOnQueryTextListener(null);
            //refresh("");
            return true;
        }
    });

    mSearchMenuItem.setVisible(true);
}


void refresh(String srch){
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            // Not UI

            data.clear();
            Link link = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).build().create(Link.class);
            Object obj = null;
            try {
                obj = link.getSongs(srch).execute().body().get(0);
            } catch (IOException e) {
                e.printStackTrace();
            }
            List<GetSong> result = GSON.fromJson(GSON.toJson(obj), TYPE);
            if(result != null) {
                for (int i = 0; i < result.size(); i++) {
                    Map<String, Object> m = new HashMap<>();
                    m.put(ATTRIBUTE_NAME_TITLE, result.get(i).getTitle());
                    m.put(ATTRIBUTE_NAME_PERFORMER, result.get(i).getArtist());
                    data.add(m);
                }


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // UI
                        String[] from = {ATTRIBUTE_NAME_TITLE, ATTRIBUTE_NAME_PERFORMER};
                        int[] to = {R.id.song_name, R.id.song_artist_and_duration};
                        SimpleAdapter sAdapter = new SimpleAdapter(getContext(), data, R.layout.item_top, from, to);
                        listView.setAdapter(sAdapter);
                        listView.setVisibility(View.VISIBLE);
                        progressBar.setVisibility(View.GONE);
                        tvEmpty.setVisibility(View.GONE);
                    }
                });

            }
        }
    },0);
}


}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question