gpt4 book ai didi

android - 如何将按钮添加到数组列表

转载 作者:行者123 更新时间:2023-11-30 03:01:03 26 4
gpt4 key购买 nike

我需要将按钮添加到 ArrayList 并在 for 循环 中动态地监听按钮。在 for 循环中,我将列表动态添加到 ArrayList 并将其显示在屏幕上。请提出任何建议。

public class Details extends ListActivity {
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
JSONArray all=new JSONArray();
String size;

String result = "";
InputStream is=null;
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle icicle) {

super.onCreate(icicle);
setContentView(R.layout.activity_details);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
setListAdapter(adapter);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://rtotv.com/Details.jsp");
HttpResponse response = httpclient.execute(httppost);

HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);

StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();

result=sb.toString();


JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){

JSONObject json_data = jArray.getJSONObject(i);


all=json_data.getJSONArray("res");
listItems.add(" \n Event Name :"+all.get(1)+" \n Location :"+all.get(2)+"\n "+all.get(3)+"\n Contact person :"+all.get(4)+"\n Start Date :"+all.get(7)+"\n End Date :"+all.get(8)+"\n Description :"+all.get(6));





}



}
}

最佳答案

您不必在列表项上放置 Button。您可以添加一个 onItemClickListener 并且您可以捕获所选项目。
我已经使用了类似下面的东西,也许它可以帮助你。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);

final List<Something> mylist = OtherActivity.listDBoperation.getAllList();
final ListView customListView = (ListView) findViewById(R.id.listview);
MyListAdapter myListAdapter = new MyListAdapter(ListeActivity.this, mylist);
customListView.setAdapter(myListAdapter);


customListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(getApplicationContext(),mylist.get(position).getListId(), Toast.LENGTH_LONG).show();
}
});

}
}

关于android - 如何将按钮添加到数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22525849/

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