我刚创建了用于获取网站数据的应用程序,以便在列表视图中显示。同时当用户使用在editText
中键入文本时搜索数据
我的xml文件显示在这里
<EditText
android:layout_width="180px"
android:layout_height="75px"
android:drawableLeft="@android:drawable/ic_menu_search"
android:background="@color/thick"
android:id="@+id/filterText"
android:hint="Filter Items.."
android:inputType="textAutoComplete"
android:textColor="#fffff0"
android:layout_below="@+id/radioTym"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="fill_vertical"
/>
<Button
android:id="@+id/btnData"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="KNOW YOUR AREA"
android:textColor="#ffffff"
android:background="#444549"
android:layout_below="@+id/filterText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listShow"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/btnData" />
我的java代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_primary);
filterItems=(EditText)findViewById(R.id.filterText);
filterItems.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
PrimaryActivity.this.arrayAdapter.getFilter().filter(charSequence);
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});
我的数组适配器
ArrayAdapter<String> arrayAdapter;
@Override
protected void onPostExecute(String[] result) {
// Set title into TextView
//TextView txttitle = (TextView) findViewById(R.id.txtData);
//txttitle.setMovementMethod(new ScrollingMovementMethod());
//txttitle.setText(listValue);
ListView list=(ListView)findViewById(R.id.listShow);
arrayAdapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,result);
list.setAdapter(arrayAdapter);
mProgressDialog.dismiss();
}
}
我无法在beforeTextChanged
方法中使用arrayadapter。我如何访问它?
答案 0 :(得分:1)
您只需在活动开始时声明数组适配器。
public class PrimaryActivity extends Activity {
ArrayAdapter<String> arrayAdapter;
}
filterItems=(EditText)findViewById(R.id.filterText);
filterItems.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
PrimaryActivity.this.arrayAdapter.getFilter().filter(charSequence);
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});