hanggrian / socialview

Android TextView and EditText with hashtag, mention, and hyperlink support
https://hendraanggrian.github.io/socialview/
Apache License 2.0
322 stars 81 forks source link

setHashtagTextChangedListener and AutoComplete #105

Closed azelas closed 10 months ago

azelas commented 3 years ago

Hello, First of all thanks for this library! I've pretty much set everything up correctly.

  1. I used SocialAutoCompleteTextView - Done

  2. I wanted to use my own REST Api to populate the hashtag list - Done

  3. I made my own adapter that extends ArrayAdapter. The SocialArrayAdapter was filtering the list before the list was made.

  4. I used text.setHashtagTextChangedListener . I didn't really need a filter because as I type something, my app searches.

  5. I'm stuck here, when I click the dropdown list of hashtags. When I click on a hashtag, it doesn't load the hashtag name I set but rather my model name and it searches that model name.

What I'm looking for....Type and have suggestions of hashtags. Click on hashtag in suggestions list and continue typing other non-hashtag words for ex. John #loves playing #tennis. I don't know what I'm missing, do I need something from the SocialArrayAdapter I missed? `

    et_des.setHashtagTextChangedListener(new SocialView.OnChangedListener() {
        @Override
        public void onChanged(@NonNull SocialView view, @NonNull CharSequence text) {
            presenter.getHashtags(text.toString());

        }
    });
    et_des.setOnHashtagClickListener(new SocialView.OnClickListener() {
        @Override
        public void onClick(@NonNull @NotNull SocialView view, @NonNull @NotNull CharSequence text) {
            //do nothing
        }
    });
    et_des.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), list_tags2.get(position).getTag_name(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    et_des.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           // Toast.makeText(getApplicationContext(), list_tags2.get(position).getTag_name(), Toast.LENGTH_LONG).show();
            //Save hashtag to list
        }
    });`
hanggrian commented 3 years ago

I'm still struggling to understand point 5:

I'm stuck here, when I click the dropdown list of hashtags. When I click on a hashtag, it doesn't load the hashtag name I set but rather my model name and it searches that model name.

Can you explain what your model is? Your custom model should override toString method. If you can see in SocialArrayAdapter#convertToString, any hashtagable or mentionable object would be converted to string and that string should pop out in your TextView.

azelas commented 3 years ago

Hi. Thanks for getting back to me.

Some changes that I've made..I put the server request in my performing filter. The issue that I'm having now is that list is shown and disappears very quickly. I don't know what I'm doing wrong. I've debugged it and after showing in getView , the program goes to getCount(), shows the list and after getCount the list disappears.

` public class HashtagAdapter extends SocialArrayAdapter {

private List<TagModel> list;
REST rest;
Context context;

public HashtagAdapter(@NonNull Context context) {
    super(context, R.layout.layout_tag, R.id.tag_name);
    rest = new REST(context);
    this.context = context;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.layout_tag, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    TagModel item = list.get(position);
    if (item != null) holder.textView.setText(item.getTag_name());
    return convertView;
}

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

@Nullable
@Override
public TagModel getItem(int position) {
    return list.get(position);
}

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

@NonNull
@Override
public Filter getFilter() {
    return hashTagFilter;
}

private static class ViewHolder {
    final TextView textView;

    ViewHolder(@NonNull View view) {
        this.textView = view.findViewById(R.id.tag_name);
    }
}

private final Filter hashTagFilter = new Filter() {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();

        if (TextUtils.isEmpty(constraint)) {
            return results;
        }
        final List<TagModel> serverItems = findtagsNow(constraint.toString().substring(1));

        results.values = serverItems;
        results.count = serverItems.size();
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

        if (results != null && results.count > 0) {
            list = (List<TagModel>) results.values;
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();

        }
    }

    @Override
    public CharSequence convertResultToString(Object resultValue) {
        return ((TagModel) resultValue).getTag_name();
    }
};

private List<TagModel> findtagsNow(String userinput) {
    Map<String, String> params = new HashMap<>();
    params.put("table", "get_hashtags");
    params.put("value1", userinput);

    return rest.tag_list(params);
}

} `

hanggrian commented 2 years ago

Sorry for late reply. If the dropdownlist disappears without you interacting it, then it suggests a misbehavior of the adapter. From the code you share, I'd say maybe don't implement your own filtering logic as SocialArrayAdapter already has them. See HashtagArrayAdapter for example.

hanggrian commented 10 months ago

Closed for inactivity.