亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從另一個類刷新列表適配器?

如何從另一個類刷新列表適配器?

SMILET 2023-07-19 16:06:53
我想知道是否有人可以幫助我處理我的代碼。我有一個 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            }        });    }}
查看完整描述

2 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

在適配器內創建一個方法來接收您的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;

    }

}


查看完整回答
反對 回復 2023-07-19
?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

這是你需要解決的問題。我將為您提供更改要點,但您需要更新您的代碼。

在啟動/停止的 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.

就這樣,你可以開始了,但問題是你的班級設計。

建議:

  1. 創建另一個模型類,其中您的 deviname 和 ip 也是您想要在 onclicklistener 中編輯/更新的數據/字段

  2. 嘗試使用列表/映射,最好將列表傳遞給適配器。


查看完整回答
反對 回復 2023-07-19
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號