#
whycq
2025-01-21 685812c2040b48d06422caa61c03666c9ffa3952
#
5个文件已修改
193 ■■■■■ 已修改文件
app/src/main/java/com/example/agvcontroller/EditeActivity.java 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/EditeAdapter.java 109 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/StartActivity.java 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/res/layout/activity_edite.xml 36 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/res/layout/edite_view.xml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/EditeActivity.java
@@ -1,10 +1,14 @@
package com.example.agvcontroller;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@@ -21,23 +25,44 @@
    private List<Item> items;
    private EditeAdapter adapter;
    private SharedPreferences sharedPreferences;
    private AppCompatImageButton addItem;
    private Button confirm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edite);
        addItem = findViewById(R.id.add_button);
        confirm = findViewById(R.id.confirm_button);
        sharedPreferences = getSharedPreferences("AGVControllerPrefs", MODE_PRIVATE);
        // Load items from SharedPreferences
        items = loadItemsFromSharedPreferences();
        recyclerView = findViewById(R.id.edite_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        Log.d("EditeActivity", "onCreate: " + items.size());
        adapter = new EditeAdapter(items);
        recyclerView.setAdapter(adapter);
        addItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                items.add(new Item("", "", 0, "", 0));
                adapter.notifyDataSetChanged();
                Log.d("items", items.toString());
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveItemsToSharedPreferences();
            }
        });
    }
    @Override
@@ -52,6 +77,7 @@
        Gson gson = new Gson();
        String json = gson.toJson(items);
        editor.putString("items", json);
        Log.d("save",json);
        editor.apply();
    }
app/src/main/java/com/example/agvcontroller/EditeAdapter.java
@@ -1,21 +1,25 @@
package com.example.agvcontroller;
import android.graphics.Color;
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.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class EditeAdapter  extends RecyclerView.Adapter<EditeAdapter.ViewHolder> {
    private List<Item> itemList;
    private static List<Item> itemList;
    public EditeAdapter(List<Item> itemList) {
        this.itemList = itemList;
@@ -24,11 +28,60 @@
    public static class ViewHolder extends RecyclerView.ViewHolder {
        EditText agvNo;
        EditText ip;
        AppCompatImageButton btn;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            agvNo = itemView.findViewById(R.id.agvNo);
            ip = itemView.findViewById(R.id.ip);
            btn = itemView.findViewById(R.id.delete_button);
            // 设置 agvNo 的文本变化监听
            agvNo.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) {
                    // 文本变化时
                }
                @Override
                public void afterTextChanged(Editable s) {
                    // 文本变化之后
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        Item item = itemList.get(position);
                        item.setAgvNo(s.toString()); // 更新数据源
                    }
                }
            });
            // 设置 ip 的文本变化监听
            ip.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) {
                    // 文本变化时
                }
                @Override
                public void afterTextChanged(Editable s) {
                    // 文本变化之后
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        Item item = itemList.get(position);
                        item.setIp(s.toString()); // 更新数据源
                    }
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
@@ -36,6 +89,7 @@
                    Log.i("List","123");
                }
            });
        }
    }
@@ -66,6 +120,12 @@
        void onItemClick(View view,int position);
    }
    public void removeItem(int position) {
        itemList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, itemList.size()); // 更新剩余项的位置
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (itemList == null || itemList.isEmpty()) {
@@ -82,18 +142,57 @@
                    holder.itemView.setBackgroundColor(Color.parseColor("#90EE90"));
                    break;
            }
            holder.agvNo.setText("AGV_NO: " + item.getAgvNo());
            holder.ip.setText("AGV_IP: " + item.getIp());
            // 清除旧的 TextWatcher
            holder.agvNo.removeTextChangedListener((TextWatcher) holder.agvNo.getTag());
            holder.ip.removeTextChangedListener((TextWatcher) holder.ip.getTag());
            holder.itemView.setTag(position);
            // 设置当前文本
            holder.agvNo.setText(item.getAgvNo());
            holder.ip.setText(item.getIp());
            // 添加新的 TextWatcher
            TextWatcher agvNoWatcher = 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) {}
                @Override
                public void afterTextChanged(Editable s) {
                    item.setAgvNo(s.toString());
                }
            };
            holder.agvNo.addTextChangedListener(agvNoWatcher);
            holder.agvNo.setTag(agvNoWatcher);
            TextWatcher ipWatcher = 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) {}
                @Override
                public void afterTextChanged(Editable s) {
                    item.setIp(s.toString());
                }
            };
            holder.ip.addTextChangedListener(ipWatcher);
            holder.ip.setTag(ipWatcher);
        }
        // 设置删除按钮的点击事件
        holder.btn.setOnClickListener(v -> {
            Log.i("List",itemList.toString());
            removeItem(position); // 调用删除方法
        });
    }
    @Override
    public int getItemCount() {
        if (itemList == null || itemList.isEmpty()) {
            return 1;
            return 0;
        } else {
            return itemList.size();
        }
app/src/main/java/com/example/agvcontroller/StartActivity.java
@@ -2,6 +2,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
@@ -12,11 +13,14 @@
import androidx.recyclerview.widget.RecyclerView;
import com.example.agvcontroller.socket.SocketManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
@@ -26,6 +30,7 @@
    private List<Item> items;
    private ItemAdapter adapter;
    private Button addItem;
    private SharedPreferences sharedPreferences;
    SocketManager socketManager;
    @Override
@@ -45,7 +50,10 @@
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        sharedPreferences = getSharedPreferences("AGVControllerPrefs", MODE_PRIVATE);
        // Load items from SharedPreferences
        items = loadItemsFromSharedPreferences();
        items = new ArrayList<>();
@@ -76,6 +84,16 @@
        socketManager.startServer(8022);
    }
    private List<Item> loadItemsFromSharedPreferences() {
        Gson gson = new Gson();
        String json = sharedPreferences.getString("items", null);
        if (json != null) {
            Type type = new TypeToken<List<Item>>(){}.getType();
            return gson.fromJson(json, type);
        }
        return new ArrayList<>();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceConnected(Item deviceAddress) {
        Log.i("EventBus", "Received device connected: " + deviceAddress);
app/src/main/res/layout/activity_edite.xml
@@ -45,6 +45,19 @@
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
        <ImageButton
            android:id="@+id/add_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:src="@android:drawable/ic_menu_add"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteY="0dp" />
    </androidx.constraintlayout.widget.ConstraintLayout>
@@ -52,21 +65,24 @@
        android:id="@+id/edite_recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/addButton"
        app:layout_constraintBottom_toTopOf="@id/confirm_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header"
        app:layout_constraintVertical_bias="1.0" />
        app:layout_constraintVertical_bias="0.0" />
    <ImageButton
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
    <Button
        android:id="@+id/confirm_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_input_add"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:padding="10dp"
        tools:ignore="MissingConstraints" />
        android:layout_margin="20dp"
        android:text="完成"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />
app/src/main/res/layout/edite_view.xml
@@ -55,7 +55,7 @@
    </LinearLayout>
    <ImageButton
        android:id="@+id/deleteButton"
        android:id="@+id/delete_button"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"