到哪表单
This commit is contained in:
parent
3a1fe37f2c
commit
9667ae4a3e
|
@ -7,31 +7,47 @@ import androidx.recyclerview.widget.RecyclerView;
|
|||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
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.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.RequestOptions;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class DaonaTable extends AppCompatActivity {
|
||||
|
||||
RecyclerView recyclerView;
|
||||
MyAdapter myAdapter;
|
||||
List<Position> positionList = new ArrayList<>();
|
||||
List<LineSearch_Item.DestinationDTO> destinationDTOS = new ArrayList<>();
|
||||
EditText searchEditText;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_daona_table);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
Position position = new Position();
|
||||
position.name = "军事博物馆采血点"+i;
|
||||
// position.position="聊天内容";
|
||||
positionList.add(position);
|
||||
}
|
||||
|
||||
// for (int i = 0; i < 6; i++) {
|
||||
// Position position = new Position();
|
||||
// position.name = "军事博物馆采血点"+i;
|
||||
//// position.position="聊天内容";
|
||||
// positionList.add(position);
|
||||
// }
|
||||
searchEditText = findViewById(R.id.editText3);
|
||||
recyclerView = findViewById(R.id.recycle);
|
||||
myAdapter = new MyAdapter();
|
||||
recyclerView.setAdapter(myAdapter);
|
||||
|
@ -46,17 +62,95 @@ public class DaonaTable extends AppCompatActivity {
|
|||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//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/linesearch")
|
||||
.get()
|
||||
.build();
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
Call call = okHttpClient.newCall(request);
|
||||
//创建了一个线程
|
||||
call.enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||
//请求失败
|
||||
Toast.makeText(DaonaTable.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);
|
||||
//所有对于UI控件的操作行为放在主线程中 解决方法:切换到主线程
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Gson gson = new Gson();
|
||||
LineSearch_Item lineSearchItem = gson.fromJson(result,LineSearch_Item.class);
|
||||
destinationDTOS.addAll(lineSearchItem.destination);
|
||||
myAdapter.notifyDataSetChanged();
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
private void filterStationList(String query) {
|
||||
// 如果查询字符串为空,则显示所有站点
|
||||
if (query.isEmpty()) {
|
||||
myAdapter.updateStationList(new ArrayList<>(destinationDTOS));
|
||||
} else
|
||||
{
|
||||
List<LineSearch_Item.DestinationDTO> filteredList = new ArrayList<>();
|
||||
for (LineSearch_Item.DestinationDTO destinationDTO : destinationDTOS) {
|
||||
if (destinationDTO.name.toLowerCase().contains(query.toLowerCase())) {
|
||||
filteredList.add(destinationDTO);
|
||||
|
||||
}
|
||||
}
|
||||
myAdapter.updateStationList(filteredList);
|
||||
}
|
||||
}
|
||||
//item布局文件中空间初始化
|
||||
public class MyViewHolder extends RecyclerView.ViewHolder {
|
||||
TextView nameTextView;
|
||||
// TextView detailTextView;
|
||||
|
||||
TextView detailTextView;
|
||||
ImageView image;
|
||||
public MyViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
nameTextView = itemView.findViewById(R.id.name);
|
||||
// detailTextView = itemView.findViewById(R.id.position);
|
||||
detailTextView = itemView.findViewById(R.id.position);
|
||||
image=itemView.findViewById(R.id.image);
|
||||
}
|
||||
}
|
||||
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
|
||||
|
@ -73,13 +167,16 @@ public class DaonaTable extends AppCompatActivity {
|
|||
//设置item中的控件设置值 点击事件
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
|
||||
Position position1 = positionList.get(position);
|
||||
holder.nameTextView.setText(position1.name);
|
||||
// holder.detailTextView.setText(position1.position);
|
||||
// Glide.with(ChufaTable.this)
|
||||
// .load(chat.imgurl)
|
||||
// .apply(RequestOptions.bitmapTransform(new CircleCrop()))
|
||||
// .into(holder.imageView);
|
||||
LineSearch_Item.DestinationDTO dto = destinationDTOS.get(position);
|
||||
holder.nameTextView.setText(dto.name);
|
||||
holder.detailTextView.setText(dto.didian);
|
||||
if(position==0){
|
||||
Glide.with(DaonaTable.this)
|
||||
.load(R.drawable.train)
|
||||
.apply(RequestOptions.centerCropTransform())
|
||||
.into(holder.image);
|
||||
}
|
||||
|
||||
//TODO 点击事件
|
||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
|
@ -99,5 +196,10 @@ public class DaonaTable extends AppCompatActivity {
|
|||
public int getItemCount() {
|
||||
return 6;
|
||||
}
|
||||
public void updateStationList(List<LineSearch_Item.DestinationDTO> filteredList) {
|
||||
destinationDTOS.clear();
|
||||
destinationDTOS.addAll(filteredList);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue