AutoCompleteTextView 上的 getText() 返回 SpannableStringBuilder?
我有一个带有 MatrixCursor 作为适配器的 AutoCompleteTextView。 AutoCompleteTextView 有一个 OnItemClickListener,如下所示:
public class AutoCompleteListClickListner implements OnItemClickListener
{
private AutoCompleteTextView acView;
public AutoCompleteListClickListner(AutoCompleteTextView view)
{
this.acView = view;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
/* tv is the selected view in the dropdown */
TextView tv = (TextView) ((LinearLayout) view).getChildAt(0);
Log.d(AndroidLogTag, "Before:>>>>" + this.acView.getText().toString());
this.acView.setText(tv.getText().toString());
Log.d(AndroidLogTag, "After:>>>>" + this.acView.getText().toString());
}
}
当选择一个项目时,我在日志中看到以下内容
10-14 00:34:37.893: DEBUG/MyApp(19151): Before:>>>>android.database.MatrixCursor@4643e388
10-14 00:34:37.912: DEBUG/MyApp(19151): After:>>>>Ankit V Jain
如果您看到日志,Before
日志条目,getText()
返回一个SpannableStringBuilder
对象及其 toString()
返回一些对象标识符。
如何获取单击下拉项之前存在的自动完成视图的实际文本?
I have an AutoCompleteTextView with a MatrixCursor as an adapter. The AutoCompleteTextView has a OnItemClickListener as follows:
public class AutoCompleteListClickListner implements OnItemClickListener
{
private AutoCompleteTextView acView;
public AutoCompleteListClickListner(AutoCompleteTextView view)
{
this.acView = view;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
/* tv is the selected view in the dropdown */
TextView tv = (TextView) ((LinearLayout) view).getChildAt(0);
Log.d(AndroidLogTag, "Before:>>>>" + this.acView.getText().toString());
this.acView.setText(tv.getText().toString());
Log.d(AndroidLogTag, "After:>>>>" + this.acView.getText().toString());
}
}
When an item is selected I see following in the log
10-14 00:34:37.893: DEBUG/MyApp(19151): Before:>>>>android.database.MatrixCursor@4643e388
10-14 00:34:37.912: DEBUG/MyApp(19151): After:>>>>Ankit V Jain
If you see the log, the Before
log entry, the getText()
returns a SpannableStringBuilder
object and toString()
of it returns some object identifier.
How do I get actual text of the auto-complete view which was there just before clicking drop-down item?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
SpannableStringBuilder
转换为CharSequence
try casting
SpannableStringBuilder
toCharSequence
好吧,代码中总是有另一种方式(肮脏的或最好的)。
我向 AutoCompleteView 添加了一个
TextWatcher
,并使用beforeTextChanged()
事件来记录文本内容并在onItemClick()
中重用它!Well, there is always another way (dirty or best) in code.
I added a
TextWatcher
to the AutoCompleteView and usedbeforeTextChanged()
event to record text content and reuse that inonItemClick()
!