0

When I split the char how can use spannable or ClickableSpan to get values?

this is a code

StringBuilder sb = new StringBuilder();
            String data = newFeedModel.getFeedTags();
            String[] items = data.split("#");
            for (String item : items)
            {
                if(item.trim().length() == 0){
                    sb.append("");
                }else{
                    sb.append("#"+item+"\n");
                }
            }

            holder.tags.setText(sb.toString());

I will split all "#" char, like facebook function.

#aaa#bbbccc#xcvsdfsefsefa#22

it will split like this

#aaa
#bbbccc
#xcvsdfsefsefa
#22

but how to know when I split the char then can got correct gata?

Update:

public void onBindViewHolder(ViewHolder holder, int position) {
if(!TextUtils.isEmpty(newFeedModel.getFeedTags())){
            holder.tags.setVisibility(View.VISIBLE);
            String data = newFeedModel.getFeedTags();
            holder.bind(data);
        }
}

in MyClickableSpan

class MyClickableSpan extends ClickableSpan {

    String clicked;
    Context context;

    public MyClickableSpan(Context context, String string) {

        super();
        this.context = context;
        clicked = string;
    }

    public void onClick(View tv) {
        Toast.makeText(context.getApplicationContext(), ""+clicked, Toast.LENGTH_LONG).show();

    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false); // set to false to remove underline
    }

}

how can send the data to live search when user click the tags.

this is my live code

public void fetchSearch(String key)
    {
        apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
        Call<List<SearchModel>> call = apiInterface.getSearch(key);

        call.enqueue(new Callback<List<SearchModel>>() {
            @Override
            public void onResponse(Call<List<SearchModel>> call, retrofit2.Response<List<SearchModel>> response) {
                searchModelList = response.body();
                adapter = new NewSearchAdapter(getActivity(), searchModelList);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<List<SearchModel>> call, Throwable t) {

            }
        });
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.search, menu);

        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                fetchSearch(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                fetchSearch(newText);
                return false;
            }
        });

        super.onCreateOptionsMenu(menu, inflater);
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (item.getItemId()) {
            case R.id.message:
                Intent intent = new Intent(getContext(), NotificationMessageActivity.class);
                startActivity(intent);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
James
  • 3
  • 3
  • looking for https://stackoverflow.com/questions/19908547/create-clickable-link-in-text-view-in-android? – Raghunandan Nov 13 '20 at 08:23
  • @Raghunandan I have update my context. – James Nov 13 '20 at 13:41
  • what went wrong?. – Raghunandan Nov 14 '20 at 03:40
  • @Raghunandan it's no function. cannot work – James Nov 16 '20 at 02:44
  • it shoudl work. can you post the code to reproduce the same which is not workign – Raghunandan Nov 16 '20 at 03:56
  • @Raghunandan I update the context, I don't why get null – James Nov 16 '20 at 04:08
  • where are you overriding onClick ? – Raghunandan Nov 16 '20 at 04:09
  • @Raghunandan my code writing in adpater. I update the context, when I click the #aaa, it's show #22 when I click the #bbbccc, it's show #22.... – James Nov 16 '20 at 04:48
  • it does not matter where you are writing the code. You have not used the code in the link above properly. The span is a clickable span. Your `MyClickableSpan` is not a clickable span. – Raghunandan Nov 16 '20 at 04:57
  • I made a sample https://gitlab.com/raghunandan2005/recyclerviewtest. it works. also take a look at material chips and flow layoutmanager in case need them. – Raghunandan Nov 16 '20 at 05:35
  • @Raghunandan thx, bro!!! I got it!!! Last question, how can I send the data to my live search? I hope I can send the data to HomeFragement lifesearch when the user click the tags. I have realized live search function, But I don't know how can sned date. Can you check the context? I have update it. – James Nov 16 '20 at 06:59
  • search for how to communicate data to fragment. you can use interface as a callback to activtiy and then communicate to fragment. – Raghunandan Nov 16 '20 at 13:34

0 Answers0