线路查询的出发表单数据从json获得,以及查询功能
This commit is contained in:
parent
a61b891fe6
commit
3a1fe37f2c
|
@ -7,20 +7,36 @@ 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 ChufaTable extends AppCompatActivity {
|
||||
|
||||
RecyclerView recyclerView;
|
||||
MyAdapter myAdapter;
|
||||
List<Position> positionList = new ArrayList<>();
|
||||
List<LineSearch_Item.DepartureDTO> departureDTOS = new ArrayList<>();
|
||||
EditText searchEditText;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
@ -34,30 +50,102 @@ public class ChufaTable extends AppCompatActivity {
|
|||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
for(int i=0;i<6;i++ ){
|
||||
Position position = new Position();
|
||||
position.name = "大望"+i;
|
||||
// position.position="聊天内容";
|
||||
positionList.add(position);
|
||||
}
|
||||
|
||||
recyclerView=findViewById(R.id.recycle);
|
||||
searchEditText = findViewById(R.id.editText3);
|
||||
myAdapter = new MyAdapter();
|
||||
recyclerView.setAdapter(myAdapter);
|
||||
//TODO 一行展示一个
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
//TODO 一行展示2个
|
||||
// recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
|
||||
}
|
||||
|
||||
//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(ChufaTable.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);
|
||||
departureDTOS.addAll(lineSearchItem.departure);
|
||||
myAdapter.notifyDataSetChanged();
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
private void filterStationList(String query) {
|
||||
// 如果查询字符串为空,则显示所有站点
|
||||
if (query.isEmpty()) {
|
||||
myAdapter.updateStationList(new ArrayList<>(departureDTOS));
|
||||
} else
|
||||
{
|
||||
List<LineSearch_Item.DepartureDTO> filteredList = new ArrayList<>();
|
||||
for (LineSearch_Item.DepartureDTO departureDTO : departureDTOS) {
|
||||
if (departureDTO.spotName.toLowerCase().contains(query.toLowerCase())) {
|
||||
filteredList.add(departureDTO);
|
||||
|
||||
}
|
||||
}
|
||||
myAdapter.updateStationList(filteredList);
|
||||
}
|
||||
}
|
||||
//item布局文件中空间初始化
|
||||
public class MyViewHolder extends RecyclerView.ViewHolder {
|
||||
TextView nameTextView;
|
||||
TextView detailTextView;
|
||||
ImageView image;
|
||||
public MyViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
nameTextView=itemView.findViewById(R.id.name);
|
||||
detailTextView=itemView.findViewById(R.id.position);
|
||||
image=itemView.findViewById(R.id.image);
|
||||
}
|
||||
}
|
||||
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
|
||||
|
@ -74,13 +162,17 @@ public class ChufaTable 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.DepartureDTO dto = departureDTOS.get(position);
|
||||
holder.nameTextView.setText(dto.spotName);
|
||||
holder.detailTextView.setText(dto.weizhi);
|
||||
if(position==0){
|
||||
Glide.with(ChufaTable.this)
|
||||
.load(R.drawable.train)
|
||||
.apply(RequestOptions.centerCropTransform())
|
||||
.into(holder.image);
|
||||
}
|
||||
|
||||
|
||||
//TODO 点击事件
|
||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
|
@ -88,7 +180,7 @@ public class ChufaTable extends AppCompatActivity {
|
|||
// Log.i("test","onClick");
|
||||
//TODO 跳转到新的activity
|
||||
Intent intent = new Intent(ChufaTable.this, SearchLine.class);
|
||||
intent.putExtra("start",position1.name);
|
||||
intent.putExtra("start",dto.spotName);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
|
@ -98,7 +190,12 @@ public class ChufaTable extends AppCompatActivity {
|
|||
//item显示条数
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return 6;
|
||||
return departureDTOS.size();
|
||||
}
|
||||
public void updateStationList(List<LineSearch_Item.DepartureDTO> filteredList) {
|
||||
departureDTOS.clear();
|
||||
departureDTOS.addAll(filteredList);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.hnucm.c25;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LineSearch_Item {
|
||||
|
||||
public List<DepartureDTO> departure;
|
||||
public List<DestinationDTO> destination;
|
||||
|
||||
public static class DepartureDTO {
|
||||
public String spotName;
|
||||
public String weizhi;
|
||||
}
|
||||
|
||||
public static class DestinationDTO {
|
||||
public String name;
|
||||
public String didian;
|
||||
}
|
||||
}
|
|
@ -6,17 +6,17 @@
|
|||
android:layout_height="match_parent"
|
||||
tools:context=".ChufaTable">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView155"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:background="#ECECEC"
|
||||
android:text="14号线"
|
||||
android:textColor="#9B9B9B"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/textView154"
|
||||
app:layout_constraintStart_toEndOf="@+id/textView154"
|
||||
app:layout_constraintTop_toTopOf="@+id/textView154" />
|
||||
<!-- <TextView-->
|
||||
<!-- android:id="@+id/textView155"-->
|
||||
<!-- android:layout_width="wrap_content"-->
|
||||
<!-- android:layout_height="wrap_content"-->
|
||||
<!-- android:layout_marginStart="8dp"-->
|
||||
<!-- android:background="#ECECEC"-->
|
||||
<!-- android:text="14号线"-->
|
||||
<!-- android:textColor="#9B9B9B"-->
|
||||
<!-- app:layout_constraintBottom_toBottomOf="@+id/textView154"-->
|
||||
<!-- app:layout_constraintStart_toEndOf="@+id/textView154"-->
|
||||
<!-- app:layout_constraintTop_toTopOf="@+id/textView154" />-->
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/imageView18"
|
||||
|
@ -35,75 +35,78 @@
|
|||
android:layout_marginStart="60dp"
|
||||
android:layout_marginEnd="24dp"
|
||||
android:background="@drawable/edit_background"
|
||||
android:text="大望"
|
||||
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"
|
||||
app:layout_constraintTop_toTopOf="@+id/imageView18" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView149"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:background="#F3F3F3"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/editText3" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/imageView41"
|
||||
android:layout_width="40dp"
|
||||
android:layout_height="40dp"
|
||||
android:layout_marginStart="16dp"
|
||||
app:layout_constraintBottom_toTopOf="@+id/textView152"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/textView149"
|
||||
app:srcCompat="@drawable/train" />
|
||||
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" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView150"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="24dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:text="大望路(地铁站)"
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintBottom_toTopOf="@+id/textView154"
|
||||
app:layout_constraintStart_toEndOf="@+id/imageView41" />
|
||||
<!-- <ImageView-->
|
||||
<!-- android:id="@+id/imageView41"-->
|
||||
<!-- android:layout_width="40dp"-->
|
||||
<!-- android:layout_height="40dp"-->
|
||||
<!-- android:layout_marginStart="16dp"-->
|
||||
<!-- app:layout_constraintBottom_toTopOf="@+id/textView152"-->
|
||||
<!-- app:layout_constraintStart_toStartOf="parent"-->
|
||||
<!-- app:layout_constraintTop_toBottomOf="@+id/textView149"-->
|
||||
<!-- app:srcCompat="@drawable/train" />-->
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView152"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:layout_marginTop="100dp"
|
||||
android:background="#F3F3F3"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/textView149" />
|
||||
<!-- <TextView-->
|
||||
<!-- android:id="@+id/textView150"-->
|
||||
<!-- android:layout_width="wrap_content"-->
|
||||
<!-- android:layout_height="wrap_content"-->
|
||||
<!-- android:layout_marginStart="24dp"-->
|
||||
<!-- android:layout_marginBottom="8dp"-->
|
||||
<!-- android:text="大望路(地铁站)"-->
|
||||
<!-- android:textSize="16sp"-->
|
||||
<!-- android:textStyle="bold"-->
|
||||
<!-- app:layout_constraintBottom_toTopOf="@+id/textView154"-->
|
||||
<!-- app:layout_constraintStart_toEndOf="@+id/imageView41" />-->
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView154"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:background="#ECECEC"
|
||||
android:text="1号线"
|
||||
android:textColor="#9B9B9B"
|
||||
app:layout_constraintBottom_toTopOf="@+id/textView152"
|
||||
app:layout_constraintStart_toStartOf="@+id/textView150" />
|
||||
<!-- <TextView-->
|
||||
<!-- android:id="@+id/textView152"-->
|
||||
<!-- android:layout_width="match_parent"-->
|
||||
<!-- android:layout_height="1dp"-->
|
||||
<!-- android:layout_marginTop="100dp"-->
|
||||
<!-- android:background="#F3F3F3"-->
|
||||
<!-- app:layout_constraintEnd_toEndOf="parent"-->
|
||||
<!-- app:layout_constraintStart_toStartOf="parent"-->
|
||||
<!-- app:layout_constraintTop_toBottomOf="@+id/textView149" />-->
|
||||
|
||||
<!-- <TextView-->
|
||||
<!-- android:id="@+id/textView154"-->
|
||||
<!-- android:layout_width="wrap_content"-->
|
||||
<!-- android:layout_height="wrap_content"-->
|
||||
<!-- android:layout_marginBottom="24dp"-->
|
||||
<!-- android:background="#ECECEC"-->
|
||||
<!-- android:text="1号线"-->
|
||||
<!-- android:textColor="#9B9B9B"-->
|
||||
<!-- app:layout_constraintBottom_toTopOf="@+id/textView152"-->
|
||||
<!-- app:layout_constraintStart_toStartOf="@+id/textView150" />-->
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recycle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="173dp"
|
||||
android:layout_marginTop="65dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.0"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/textView152"
|
||||
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_bias="0.0" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -99,6 +99,7 @@
|
|||
android:layout_marginTop="16dp"
|
||||
android:layout_marginEnd="64dp"
|
||||
android:hint="在哪出发"
|
||||
android:textColor="@color/black"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="1.0"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
|
@ -114,6 +115,7 @@
|
|||
android:layout_marginStart="64dp"
|
||||
android:layout_marginEnd="64dp"
|
||||
android:hint="要去哪里"
|
||||
android:textColor="@color/black"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/editText6">
|
||||
|
|
Loading…
Reference in New Issue