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

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

如何從 Json 字符串動態創建按鈕

如何從 Json 字符串動態創建按鈕

ibeautiful 2023-02-16 16:21:13
我將 URL 中的 JSON 數據解析為一個列表,現在我想為列表中的每個項目創建按鈕,但我不知道該怎么做。我不確定該列表是否是最好的主意,但這是我在網上找到的解決方案。public class SecondActivity extends AppCompatActivity {    private String TAG = SecondActivity.class.getSimpleName();    private ProgressDialog pDialog;    private ListView lv;    private static String url = "https://ggysqqcz.p51.rt3.io/available-remotes/TV";    ArrayList<HashMap<String, String>> contactList;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);        contactList = new ArrayList<>();        lv = (ListView) findViewById(R.id.list);        new GetContacts().execute();    }    private class GetContacts extends AsyncTask<Void, Void, Void> {        @Override        protected void onPreExecute() {            super.onPreExecute();            // Showing progress dialog            pDialog = new ProgressDialog(SecondActivity.this);            pDialog.setMessage("Please wait...");            pDialog.setCancelable(false);            pDialog.show();        }        @Override        protected Void doInBackground(Void... arg0) {            HttpHandler sh = new HttpHandler();            // Making a request to url and getting response            String jsonStr = sh.makeServiceCall(url);            Log.e(TAG, "Response from url: " + jsonStr);            if (jsonStr != null) {                try {                    JSONObject jsonObj = new JSONObject(jsonStr);                    // Getting JSON Array node                    JSONArray remotes = jsonObj.getJSONArray("remotes");這顯示了所有按鈕,但顯然它們在單擊時都執行相同的操作,因為我只有 button1。我怎樣才能讓所有的按鈕做不同的活動?
查看完整描述

3 回答

?
九州編程

TA貢獻1785條經驗 獲得超4個贊

我想建議為您創建一個自定義適配器,ListView它將為您的按鈕提供一個onClick功能,并且根據該項目在您的 中的位置,您可以在您的功能ListView中實現不同的操作。onClick因此,我想推薦一個如下所示的適配器。


public class ListAdapter extends ArrayAdapter<Item> {


    private int resourceLayout;

    private Context mContext;

    private ArrayList<Contact> contacts;


    public ListAdapter(Context context, int resource, ArrayList<Contact> contacts) {

        super(context, resource, items);

        this.resourceLayout = resource;

        this.mContext = context;

        this.contacts = contacts;

    }


    @Override

    public View getView(int position, View convertView, ViewGroup parent) {


        View v = convertView;


        if (v == null) {

            LayoutInflater vi;

            vi = LayoutInflater.from(mContext);

            v = vi.inflate(resourceLayout, null);

        }


        Item p = getItem(position);


        if (p != null) {

            Button btn = (TextView) v.findViewById(R.id.button1);


            if (btn != null) {

                btn.setOnClickListener(new View.OnClickListener() {

                    @Override

                    public void onClick(View view) {

                        if(position == 1) implementSomethingFor1();

                        else if (position == 2) implementSomethingFor2();

                        // ... Define the other implementations

                    }

                });

            }

        }


        return v;

    }

}

然后像下面這樣使用適配器。


ListView lv = (ListView) findViewById(R.id.list);

ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item, contactList);

lv.setAdapter(customAdapter);

請注意,這不是一個確切的實現。您應該修改您的自定義適配器,使其符合您的目的。


查看完整回答
反對 回復 2023-02-16
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

嘗試 lv.setonitemclicklistenter,這將創建一個允許您單擊每個項目的方法,您可以在該方法中編寫例如 Toast 消息,這樣當您單擊一個項目時,將彈出一個 Toast 消息。



查看完整回答
反對 回復 2023-02-16
?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

您有幾種選擇:

  1. 檢查view參數以確定要做什么。您可以在每個按鈕上使用getTag()setTag()提供自定義數據。

  2. 通過擴展創建自定義適配器SimpleAdapter。覆蓋 createView() 和 bindView() 以便為每個按鈕提供自定義行為,例如向每個按鈕添加不同的 OnClickListener 對象

  3. OnItemClickListener為設置ListView。這提供了一個參數,用于單擊列表視圖中的位置。您可以使用它來確定要做什么或將哪些數據傳遞給新活動。您可能希望使用getItem()適配器來獲取當前行的數據。


查看完整回答
反對 回復 2023-02-16
  • 3 回答
  • 0 關注
  • 209 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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