根据用户输入进行站点查询,前面留下的bug已解决
This commit is contained in:
parent
e6bff1a513
commit
a61b891fe6
|
@ -7,13 +7,18 @@ import androidx.recyclerview.widget.LinearLayoutManager;
|
|||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
|
@ -30,11 +35,13 @@ import okhttp3.Response;
|
|||
public class Station extends AppCompatActivity {
|
||||
RecyclerView recyclerView ;
|
||||
MyAdapter myAdapter ;
|
||||
List<Station_item> stationItemList = new ArrayList<>();
|
||||
List<String> stationItemList = new ArrayList<>();
|
||||
EditText searchEditText;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_station);
|
||||
searchEditText = findViewById(R.id.editText3);
|
||||
recyclerView = findViewById(R.id.recycleView2);
|
||||
myAdapter = new MyAdapter();
|
||||
recyclerView.setAdapter(myAdapter);
|
||||
|
@ -49,8 +56,34 @@ public class Station extends AppCompatActivity {
|
|||
}
|
||||
});
|
||||
|
||||
|
||||
//todo 清除输入框中的内容
|
||||
ImageView delete = findViewById(R.id.imageView138);
|
||||
delete.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
searchEditText.setText("");
|
||||
filterStationList("");
|
||||
}
|
||||
});
|
||||
//todo 根据用户的输入更新下面的recycleview显示内容
|
||||
searchEditText.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
filterStationList(s.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
}
|
||||
});
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url("https://test04.usemock.com/takeHistory")
|
||||
.url("https://test04.usemock.com/station")
|
||||
.get()
|
||||
.build();
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
|
@ -60,19 +93,13 @@ public class Station extends AppCompatActivity {
|
|||
@Override
|
||||
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||
//请求失败
|
||||
Toast.makeText(Station.this, "网络请求失败,请稍后重试", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
@Override
|
||||
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
||||
//请求成功
|
||||
String result = response.body().string();
|
||||
// Log.i("test",result);
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
//所有对于UI控件的操作行为放在主线程中 解决方法:切换到主线程
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
|
@ -80,7 +107,7 @@ public class Station extends AppCompatActivity {
|
|||
try {
|
||||
Gson gson = new Gson();
|
||||
Station_item station_item = gson.fromJson(result,Station_item.class);
|
||||
// stationItemList.addAll(station_item);
|
||||
stationItemList.addAll(station_item.stations);
|
||||
myAdapter.notifyDataSetChanged();
|
||||
|
||||
} catch (Exception e) {
|
||||
|
@ -94,6 +121,20 @@ public class Station extends AppCompatActivity {
|
|||
});
|
||||
}
|
||||
|
||||
private void filterStationList(String query) {
|
||||
// 如果查询字符串为空,则显示所有站点
|
||||
if (query.isEmpty()) {
|
||||
myAdapter.updateStationList(new ArrayList<>(stationItemList));
|
||||
} else {
|
||||
List<String> filteredList = new ArrayList<>();
|
||||
for (String stationName : stationItemList) {
|
||||
if (stationName.toLowerCase().contains(query.toLowerCase())) {
|
||||
filteredList.add(stationName);
|
||||
}
|
||||
}
|
||||
myAdapter.updateStationList(filteredList);
|
||||
}
|
||||
}
|
||||
|
||||
public class MyViewHolder extends RecyclerView.ViewHolder {
|
||||
TextView staionName;
|
||||
|
@ -118,15 +159,20 @@ public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
|
|||
//设置item中的控件设置值 点击事件
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
|
||||
Station_item station_item = stationItemList.get(position);
|
||||
holder.staionName.setText(station_item.stationName);
|
||||
String stationName = stationItemList.get(position);
|
||||
holder.staionName.setText(stationName);
|
||||
if(stationName.equals("A")||stationName.equals("B")||stationName.equals("C")||stationName.equals("D")){
|
||||
holder.itemView.setBackgroundColor(Color.parseColor("#F3F3F3"));
|
||||
}else{
|
||||
holder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
|
||||
}
|
||||
//TODO 点击事件
|
||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
//todo 站点信息
|
||||
Intent intent = new Intent(Station.this,StationMessage.class);
|
||||
intent.putExtra("name",station_item.stationName);
|
||||
intent.putExtra("name",stationName);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
|
@ -138,6 +184,12 @@ public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
|
|||
public int getItemCount() {
|
||||
return stationItemList.size();
|
||||
}
|
||||
|
||||
public void updateStationList(List<String> filteredList) {
|
||||
stationItemList.clear();
|
||||
stationItemList.addAll(filteredList);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package com.hnucm.c25;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Station_item {
|
||||
String stationName ;
|
||||
|
||||
public List<String> stations;
|
||||
}
|
||||
|
|
|
@ -7,18 +7,7 @@
|
|||
tools:context=".Station">
|
||||
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/constraintLayout33"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="40dp"
|
||||
android:background="@color/white"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent">
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
<ImageView
|
||||
<ImageView
|
||||
android:id="@+id/imageView18"
|
||||
android:layout_width="25dp"
|
||||
android:layout_height="25dp"
|
||||
|
@ -36,6 +25,8 @@
|
|||
android:layout_marginEnd="24dp"
|
||||
android:background="@drawable/edit_background"
|
||||
android:hint="站点名称"
|
||||
android:textColor="@color/black"
|
||||
android:paddingLeft="8dp"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/imageView18"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/imageView18"
|
||||
|
@ -46,12 +37,21 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="60dp"
|
||||
android:layout_marginBottom="40dp"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout33"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/imageView138"
|
||||
android:layout_width="20dp"
|
||||
android:layout_height="20dp"
|
||||
android:layout_marginRight="32dp"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/editText3"
|
||||
app:layout_constraintRight_toRightOf="@id/editText3"
|
||||
app:layout_constraintTop_toTopOf="@+id/editText3"
|
||||
app:srcCompat="@drawable/delete" />
|
||||
|
||||
<!-- <androidx.core.widget.NestedScrollView-->
|
||||
<!-- android:layout_width="match_parent"-->
|
||||
<!-- android:layout_height="match_parent"-->
|
||||
|
|
|
@ -11,7 +11,17 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="24dp"
|
||||
android:text="TextView"
|
||||
android:textSize="17sp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView151"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="#C3BEBE"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue