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

我想知道是否有人可以帮助我处理我的代码。我有一个 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

            }

        });


    }

}

SMILET
浏览 113回答 2
2回答

慕姐8265434

在适配器内创建一个方法来接收您的deviceName, ip, imgid参数。然后你只需要在点击按钮时调用它即可。在适配器上:public void refreshData(String[] deviceName, String[] ip, Integer[] imgid){&nbsp; &nbsp; this.deviceName = deviceName;&nbsp; &nbsp; this.ip = ip;&nbsp; &nbsp; this.imgid = imgid;&nbsp; &nbsp; 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++){&nbsp; &nbsp; switch(pre_imgid[i]){&nbsp; &nbsp; &nbsp; &nbsp; case "0":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgid[i] = R.drawable.large_circle_szary; break;&nbsp; &nbsp; &nbsp; &nbsp; case "1":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgid[i] = R.drawable.large_circle_czerwony; break;&nbsp; &nbsp; &nbsp; &nbsp; case "2":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgid[i] = R.drawable.large_circle_pomarancz; break;&nbsp; &nbsp; &nbsp; &nbsp; case "3":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgid[i] = R.drawable.large_circle_zielony; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgid[i] = R.drawable.large_circle_niebieski; break;&nbsp; &nbsp; }}

侃侃无极

这是你需要解决的问题。我将为您提供更改要点,但您需要更新您的代码。在启动/停止的 onclicklistener 中,您需要手动获取所选对象并更新数据,其中视图将动态更新。在这里,您必须使用列表中的对象对父布局执行类似 settag() 的操作,我看到有多个列表,请使用要更新视图的列表的对象。例如:// your parentlayout.setTag(devicename[position])在onclicklistner中,需要获取这个对象例如:// get your object from settag:- DeviceName dobj = (DeviceName) view.getTag()更远:dobj.updateview,&nbsp;//&nbsp;something&nbsp;your&nbsp;logic&nbsp;of&nbsp;updating&nbsp;view.就这样,你可以开始了,但问题是你的班级设计。建议:创建另一个模型类,其中您的 deviname 和 ip 也是您想要在 onclicklistener 中编辑/更新的数据/字段尝试使用列表/映射,最好将列表传递给适配器。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java