gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:41:44 26 4
gpt4 key购买 nike

我将 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");

// looping through All Contacts
for (int i = 0; i < remotes.length(); i++) {
JSONObject c = remotes.getJSONObject(i);

String id = c.getString("id");


HashMap<String, String> contact = new HashMap<>();


contact.put("id", id);

contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});

}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});

}

return null;

}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();


ListAdapter adapter = new SimpleAdapter(

SecondActivity.this, contactList,
R.layout.list_item, new String[]{"id"}, new int[]{button1});

lv.setAdapter(adapter);


}
}

public void onClickButton1(View view) {
startActivity(new Intent(getApplicationContext(), ThirdActivity.class));
}
}

这显示了所有按钮,但显然它们在单击时都执行相同的操作,因为我只有 button1。如何让所有按钮执行不同的 Activity ?

最佳答案

我想建议为您的 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);

请注意,这不是一个精确的实现。您应该修改您的自定义适配器,使其符合您的目的。

关于java - 如何从 Json 字符串动态创建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56727116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com