java - 如何从另一个类刷新列表适配器?

标签 java android

我想知道是否有人可以帮助我编写代码。我有一个 ListView,其中列出了数据库中的设备。每个设备都有一个用彩色图标表示的状态。每个设备还有一堆按钮来启动/停止/等设备并且可以工作(在注销和登录图标改变颜色后)。我想要做的是以某种方式刷新此列表,以便图标颜色是最新的。提前致谢!

ListAdapter.java:

public class ListAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] deviceName;
    private final String[] ip;
    private final Integer[] imgid;

    public ListAdapter(Activity context, String[] deviceName, String[] ip, Integer[] imgid) {
        super(context, R.layout.list_item, deviceName);

        this.context = context;
        this.deviceName = deviceName;
        this.ip = ip;
        this.imgid = imgid;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_item, null, true);

        TextView titleText = (TextView) rowView.findViewById(R.id.title);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        TextView subtitleText = (TextView) rowView.findViewById(R.id.subtitle);
        Button startBtn = (Button) rowView.findViewById(R.id.startBtn);
        Button stopBtn = (Button) rowView.findViewById(R.id.stopBtn);
        final String URL3 = "http://damiangozdzi.nazwa.pl/pact-dev/sendstatus.php";


        titleText.setText(deviceName[position]);
        imageView.setImageResource(imgid[position]);
        subtitleText.setText(ip[position]);
        startBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "Device has been started", Toast.LENGTH_SHORT).show();
                SenderStatus s = new SenderStatus(getContext(), URL3, Integer.toString(position +1), "3");
                s.execute();
                //I tried to refresh my list from here but nothing worked
            }
        });

        stopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "Device has been stopped", Toast.LENGTH_SHORT).show();
                SenderStatus s = new SenderStatus(getContext(), URL3, Integer.toString(position +1), "1");
                s.execute();

            }
        });


        return rowView;
    }


}

用户.java:

public class User extends Fragment {
    private ListView list;
    private Button startBtn;
    private Button stopBtn;

    private String[] deviceName ={};
    private String[] ip ={};


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user, container, false);

        Devices d = Devices.getInstance();
        String s = d.getString();
        String[][] all = getDevices(s);

        deviceName = all[0];
        ip = all[1];
        String[] pre_imgid = all[2];
        int x = pre_imgid.length;
        Integer[] imgid = new Integer[x];
        for (int i = 0; i < x; i++){
            switch(pre_imgid[i]){
                case "0":
                    imgid[i] = R.drawable.large_circle_szary; break;
                case "1":
                    imgid[i] = R.drawable.large_circle_czerwony; break;
                case "2":
                    imgid[i] = R.drawable.large_circle_pomarancz; break;
                case "3":
                    imgid[i] = R.drawable.large_circle_zielony; break;
                default:
                    imgid[i] = R.drawable.large_circle_niebieski; break;
            }
        }


        ListAdapter adapter=new ListAdapter(getActivity(), deviceName, ip, imgid);
        list = (ListView) view.findViewById(R.id.plcsList);
        list.setAdapter(adapter);

        return view;
    }

    public String[][] getDevices(String s){

        char c = '{';
        int count = 0;

        for(int i=0; i < s.length(); i++)
        {    if(s.charAt(i) == c)
            count++;
        }

        String[][] all =  new String[3][count];

        if (s == null) {
            Toast.makeText(getContext(), "s is null", Toast.LENGTH_SHORT).show();
        } else {
            try{
                JSONArray jsonArray = new JSONArray(s);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject obj = jsonArray.getJSONObject(i);
                    all[0][i] = obj.getString("address");
                    all[1][i] = obj.getString("name");
                    all[2][i] = Integer.toString(obj.getInt("status"));
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return all;
    }
}

最佳答案

欢迎来到 Stackoverflow。

在适配器内创建一个方法,接收您的 deviceName、ip、imgid 作为参数。然后你只需要在点击按钮时调用它即可。

在适配器上:

public void refreshData(String[] deviceName, String[] ip, Integer[] imgid){
    this.deviceName = deviceName;
    this.ip = ip;
    this.imgid = imgid;
    notifyDataSetChanged();
}

然后点击按钮:

adapter.refreshData(yourDeviceNameVariable, yourIpVariable, yourImdidVariable);

我建议您将此代码放入一个方法中,其中 deviceName、ip 和 imdid 变量是全局变量,并在刷新适配器之前单击按钮时调用该方法。

Devices d = Devices.getInstance();
String s = d.getString();
String[][] all = getDevices(s);

deviceName = all[0];
ip = all[1];
String[] pre_imgid = all[2];
int x = pre_imgid.length;
Integer[] imgid = new Integer[x];
for (int i = 0; i < x; i++){
    switch(pre_imgid[i]){
        case "0":
            imgid[i] = R.drawable.large_circle_szary; break;
        case "1":
            imgid[i] = R.drawable.large_circle_czerwony; break;
        case "2":
            imgid[i] = R.drawable.large_circle_pomarancz; break;
        case "3":
            imgid[i] = R.drawable.large_circle_zielony; break;
        default:
            imgid[i] = R.drawable.large_circle_niebieski; break;
    }
}

关于java - 如何从另一个类刷新列表适配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57853026/

相关文章:

android - 带有 CursorAdapter 的 ListView

android - SoundPool 或 MediaPlayer 队列

android notifyDataSetChanged 不起作用

java - Hibernate:获取实体映射中的一组对象

java - 如何将 java 方法调用与 Antlr4 正确匹配

java - 使用 apache httpcomponents 进行 Http 身份验证

android - 如何在opencv android中导入IplImage类?

java - 在Java Android应用程序中获取ruby环境变量DATABASE_URL

java - 从 imageview 触摸点,您可以调整 MAT 的大小以获得该点的颜色吗?

java - 显示关于 https 重定向的消息