如何从 Json 字符串动态创建按钮

我将 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。我怎样才能让所有的按钮做不同的活动?


ibeautiful
浏览 159回答 3
3回答

九州编程

我想建议为您创建一个自定义适配器,ListView它将为您的按钮提供一个onClick功能,并且根据该项目在您的 中的位置,您可以在您的功能ListView中实现不同的操作。onClick因此,我想推荐一个如下所示的适配器。public class ListAdapter extends ArrayAdapter<Item> {&nbsp; &nbsp; private int resourceLayout;&nbsp; &nbsp; private Context mContext;&nbsp; &nbsp; private ArrayList<Contact> contacts;&nbsp; &nbsp; public ListAdapter(Context context, int resource, ArrayList<Contact> contacts) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, resource, items);&nbsp; &nbsp; &nbsp; &nbsp; this.resourceLayout = resource;&nbsp; &nbsp; &nbsp; &nbsp; this.mContext = context;&nbsp; &nbsp; &nbsp; &nbsp; this.contacts = contacts;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public View getView(int position, View convertView, ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; View v = convertView;&nbsp; &nbsp; &nbsp; &nbsp; if (v == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater vi;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vi = LayoutInflater.from(mContext);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v = vi.inflate(resourceLayout, null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Item p = getItem(position);&nbsp; &nbsp; &nbsp; &nbsp; if (p != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Button btn = (TextView) v.findViewById(R.id.button1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (btn != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btn.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(position == 1) implementSomethingFor1();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (position == 2) implementSomethingFor2();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ... Define the other implementations&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return v;&nbsp; &nbsp; }}然后像下面这样使用适配器。ListView lv = (ListView) findViewById(R.id.list);ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item, contactList);lv.setAdapter(customAdapter);请注意,这不是一个确切的实现。您应该修改您的自定义适配器,使其符合您的目的。

LEATH

尝试 lv.setonitemclicklistenter,这将创建一个允许您单击每个项目的方法,您可以在该方法中编写例如 Toast 消息,这样当您单击一个项目时,将弹出一个 Toast 消息。

幕布斯6054654

您有几种选择:检查view参数以确定要做什么。您可以在每个按钮上使用getTag()和setTag()提供自定义数据。通过扩展创建自定义适配器SimpleAdapter。覆盖 createView() 和 bindView() 以便为每个按钮提供自定义行为,例如向每个按钮添加不同的 OnClickListener 对象OnItemClickListener为设置ListView。这提供了一个参数,用于单击列表视图中的位置。您可以使用它来确定要做什么或将哪些数据传递给新活动。您可能希望使用getItem()适配器来获取当前行的数据。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java