gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:51:36 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com