我需要SpannableStrings还是应该选择其他解决方案?

时间:2017-03-13 13:44:00

标签: android listview textview spannablestring

我根据从DataBase获得的数据制作了ListView。

@Override
                    public void mostCommonResults(String array) {
                        results=array;
                        pieces=results.split(";");
                        sorted=new String[pieces.length/2];
                        int firstNumber=0;
                        int secondNumber=1;
                        for(int i=0;i<sorted.length;i++){
                            sorted[i]=pieces[firstNumber]+"                             "+pieces[secondNumber]+" occurence";
                            firstNumber+=2;
                            secondNumber+=2;
                        }
                        list= (ListView) findViewById(R.id.list);
                        adapter=new ArrayAdapter<String>(ThirdActivity.this,R.layout.list_items,R.id.list_elements,sorted);
                        list.setAdapter(adapter);
                    }

结果:enter image description here

然后我为ListView设置OnItemClickListener,所以当用户点击指定的项目时,我想将它提供给第二张图片上的TextView。

enter image description here

我将数据作为SpannableString

提供给电视
if(selectedNumbers.getText().toString()=="Selected Numbers"){
                            pieces=0;
                            //position=which item has clicked
                            String temp=sorted[position].substring(0,2);
                            //if the number has only 1 digit
                            if(temp.substring(1,2).equals(" ")){
                              selectedNumbers.setText(temp.substring(0,1));
                                pieces++;
                            }
                            //if the number has 2 digits
                            else{
                                SpannableString ss = new SpannableString(temp);
                                //SpannableString array...Is it necessary?
                                selected[pieces]=ss;
                                ClickableSpan clickableSpan = new ClickableSpan() {
                                    @Override
                                    public void onClick(View textView) {
                                    }
                                    @Override
                                    public void updateDrawState(TextPaint ds) {
                                        super.updateDrawState(ds);
                                        ds.setUnderlineText(false);
                                    }
                                };
                                selected[pieces].setSpan(clickableSpan, 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                selectedNumbers.setText(selected[pieces]);
                                selectedNumbers.setMovementMethod(LinkMovementMethod.getInstance());
                                selectedNumbers.setHighlightColor(Color.TRANSPARENT);
                                pieces++;
                            }

                            selectedTextView.setEnabled(false);
                            selectedTextView.setOnClickListener(null);

                        }

我无法弄清楚当用户点击TextView指定的部分时,我该怎么做,只删除那部分(SpannableString)并保留其他部分,然后为用户删除了其编号的ListView元素设置OnClickListener 。 这将允许用户改变他/她的想法,他/她最终想要在电视上找到6个数字。

list_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView style="@style/TextDesign"
    android:id="@+id/list_elements"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

大!我会为您的利益发布答案! :) 对于ListView布局,您可以使用类似

的内容
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginTop="@dimen/activity_vertical_margin_narrow">

<TextView
    android:id="@+id/left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"/>

</RelativeLayout>

在列表视图的OnClicklistener中,您可以像这样获取TextView

TextView leftTextView = (TextView) view.findViewById(R.id.left);

然后执行此操作将其添加到标题

中的TextView
String text = headingTextView.getText();
headingTextView.setText(text+leftTextView.toString());

我没有测试这些功能,但希望你能得到这个想法!快乐编码:)