我想使用从Firebase数据库接收到的搜索栏数据搜索数据到recyclerview ...
我正在尝试通过编辑文本搜索数据,并从数据库文本接收的内容中进行搜索。但是失败了,你能告诉我如何做吗?这是我的代码。我已经尝试了4个多小时,但:(
eco.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button"
android:text="search!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ScrollView
android:id="@+id/textViewWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView tools:context="Controller.MainActivity"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/recycler_view"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.v7.widget.RecyclerView>
</ScrollView>
</LinearLayout>
recyclerview_item.xml
android:id="@+id/list_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:textColor="#000000"
/>
Eco.java
public class Eco extends AppCompatActivity {
private RecyclerView recyclerView;
EditText editText;
ScrollView textViewWrapper;
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eco);
textView = (TextView) findViewById(R.id.list_item_text);
editText = (EditText) findViewById(R.id.editText);
textViewWrapper = (ScrollView) findViewById(R.id.textViewWrapper);
button = (Button) findViewById(R.id.button);
//textView.setText(R.string.longText);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
new Eco.GetDataFromFirebase().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
// Read from the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("F");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
/*for (DataSnapshot alert: dataSnapshot.getChildren()) {
System.out.println(alert.getValue());
}*/
// This method is called once with the initial value and again
// whenever data at this location is updated.
ArrayList<String> values = (ArrayList<String>) dataSnapshot.getValue();
recyclerView.setAdapter(new RecyclerViewAdapter(values));
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
System.out.println("Failed to read value." + error.toException());
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String criteria = editText.getText().toString();
String fullText = textView.getText().toString();
if (fullText.contains(criteria)) {
int indexOfCriteria = fullText.indexOf(criteria);
int lineNumber = textView.getLayout().getLineForOffset(indexOfCriteria);
String highlighted = "<font color='red'>"+criteria+"</font>";
fullText = fullText.replace(criteria, highlighted);
textView.setText(Html.fromHtml(fullText));
textViewWrapper.scrollTo(0, textView.getLayout().getLineTop(lineNumber));
}
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() == 0) {
String fullText = textView.getText().toString();
fullText = fullText.replace("<font color='red'>", "");
fullText = fullText.replace("</font>", "");
textView.setText(fullText);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
private class GetDataFromFirebase extends AsyncTask<Void,Void,Boolean>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... voids) {
return false;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
}
}
}