为了账号安全,请及时绑定邮箱和手机立即绑定

如何从 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条经验 获得超6个赞

尝试 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 关注
  • 158 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号