3

我正在尝试在我的 ListFragment 中添加搜索功能。我已经在标准活动中完成了它,但我试图将它移植到片段中。我目前正在做的是这样的:

  1. 我有一个承载片段的 Main.java。

  2. 我有一个 ListFrag.java,它使用 R.id.frag_list 托管列表数组 List.TERM

我的 Main.java 像这样实现 addTextChangedListener:

et.addTextChangedListener(new TextWatcher()
    {
    public void afterTextChanged(Editable s)
    {
                                                                    // Abstract Method of TextWatcher Interface.
    }
    public void beforeTextChanged(CharSequence s,
    int start, int count, int after)
    {
    // Abstract Method of TextWatcher Interface.
    }
    public void onTextChanged(CharSequence s,
    int start, int before, int count)
    {
    textlength = et.getText().length();
    array_sort.clear();
    for (int i = 0; i < List.TERM.length; i++)
    {
    if (textlength <= List.TERM[i].length())
    {
    if(et.getText().toString().equalsIgnoreCase(
    (String)
    List.TERM[i].subSequence(0,
    textlength)))
    {array_sort.add(List.TERM[i]);
    }
    }
    }
    lv.setAdapter(new ArrayAdapter<String>
    (Main.this,
            R.layout.main,R.id.frag_list, array_sort));
    }
    });

我的 main.xml 是这样的:

<LinearLayout
    android:layout_width="fill_parent"
    android:background="@drawable/searchbar"
    android:layout_height="60dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    >
    <EditText android:id="@+id/EditText01"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:hint="@string/prompt">
    </EditText>
    </LinearLayout>

<fragment
    android:id="@+id/frag_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:layout_marginTop="90dp"
android:layout_marginBottom="40dp"
    class="com.bernard.ListFrag" />

当我运行应用程序时,我单击editText。在我尝试输入字母的那一刻,应用程序崩溃并显示错误日志:

08-11 12:59:57.090: E/AndroidRuntime(20220): FATAL EXCEPTION: main
08-11 12:59:57.090: E/AndroidRuntime(20220): java.lang.NullPointerException
08-11 12:59:57.090: E/AndroidRuntime(20220):    at com.bernard.Main$1.onTextChanged(Main.java:54)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.widget.TextView.sendOnTextChanged(TextView.java:8105)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.widget.TextView.handleTextChanged(TextView.java:8152)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8532)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:892)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:669)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:431)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:369)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.os.Looper.loop(Looper.java:156)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at android.app.ActivityThread.main(ActivityThread.java:5025)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at java.lang.reflect.Method.invokeNative(Native Method)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at java.lang.reflect.Method.invoke(Method.java:511)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-11 12:59:57.090: E/AndroidRuntime(20220):    at dalvik.system.NativeStart.main(Native Method)

我对这种错误的猜测是:

  1. 我应该在片段而不是 Main.java 下实现 editText 和文本侦听器。
  2. 我不是从 Main.java 调用新列表,因此是 NullPointerException。

有人有解决此问题的建议吗?谢谢 :)

4

2 回答 2

0

尝试更换:

for (int i = 0; i < List.TERM.length; i++)
    {
    if (textlength <= List.TERM[i].length())
    {
    if(et.getText().toString().equalsIgnoreCase(
    (String)
    List.TERM[i].subSequence(0,
    textlength)))
    {array_sort.add(List.TERM[i]);
    }
    }
    }
    lv.setAdapter(new ArrayAdapter<String>
    (Main.this,
            R.layout.main,R.id.frag_list, array_sort));
    }
    });

和:

           // reuse the existing adapter instead of creating a new one:
           ArrayAdapter<String> ad = (ArrayAdapter<String>) getListView().getAdapter();
           ad.clear();
           for (int i = 0; i < List.TERM.length; i++) {
              if (textlength <= List.TERM[i].length()) {
                 if (et.getText().toString().equalsIgnoreCase(
                       (String)
                             List.TERM[i].subSequence(0,
                                   textlength))) {
                    ad.add(List.TERM[i]);
                 }
              }
           }
           ad.notifyDataSetChanged(); // tell the ListView to update
于 2012-08-11T05:46:40.057 回答
0

发现如何自己做!也感谢尼克的帮助:D

这有点粗糙,但它有效。如果您认为您有更好的解决方案,请分享!

所以我所做的是根据http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity中的示例创建一个片段视图列表片段视图

诀窍是,在 ListFrag.java 中,我只是在 onCreateView 下的原始活动中实现了所有内容。重要部分如下:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View V = inflater.inflate(R.layout.list_fragment, container, false);

    lv = (ListView) V.findViewById(R.id.ListView01);
    final ArrayList <String>ar = new ArrayList<String>();
    et = (EditText) V.findViewById(R.id.EditText01);
    final String[] words = list.TERM;
    // Populate list with our static array of titles.
    lv.setAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_activated_1, list.TERM));
    lv.setTextFilterEnabled(true); 

    et.addTextChangedListener(new TextWatcher()
    {
    public void afterTextChanged(Editable s)
    {
                                                                    // Abstract Method of TextWatcher Interface.
    }
    public void beforeTextChanged(CharSequence s,
    int start, int count, int after)
    {
    // Abstract Method of TextWatcher Interface.
    }
    public void onTextChanged(CharSequence s,
    int start, int before, int count)
    {
    textlength = et.getText().length();
    array_sort.clear();
    for (int i = 0; i < words.length; i++)
    {
    if (textlength <= words[i].length())
    {
    if(et.getText().toString().equalsIgnoreCase(
    (String)
    words[i].subSequence(0,
    textlength)))
    {array_sort.add(words[i]);
    }
    }
    }
    lv.setAdapter(new ArrayAdapter<String>
    (getActivity(),
            android.R.layout.simple_list_item_activated_1, array_sort));
    }
    });

    //Intent after selection is made
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String name = lv.getItemAtPosition(position).toString();
            for (int index = 0; index < words.length; index++)
            {
                if (name.equals(words[index]))
                {
                    position = index;
                    break;
                }
            }
            String d1  = words[position];
            ar.add(d1.toString()); 

            showDetails(position);

        }
    });



    return V;
}

有了它,在片段内的列表视图上进行完整搜索。

于 2012-08-12T12:13:02.283 回答