我想知道是否有人可以帮助我编写代码。我有一个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;
}
}
User.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;
}
}
答案 0 :(得分:0)
欢迎使用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;
}
}
答案 1 :(得分:0)
这是您需要解决的方法。我将为您提供变更点,但是您需要更新代码。
在启动/停止的onclicklistener中,您需要手动获取所选对象并在那里更新数据,然后视图中的数据将被实时更新。
在这里,您必须使用列表中的对象对父布局进行settag()之类的操作,我看到有多个列表,请使用要更新视图的列表中的对象。
例如:// your parentlayout.setTag(devicename[position])
在onclicklistner中,您需要获取此对象
例如:// get your object from settag:- DeviceName dobj = (DeviceName) view.getTag()
进一步:
dobj.updateview, // something your logic of updating view.
就是这样,您很高兴,但是问题是您的课堂设计。
建议:
再创建一个模型类,您的设备名称和IP以及您要在onclicklistener中编辑/更新的数据/字段
尝试使用列表/映射,最好使用列表传递给适配器。