Android 如何将按钮添加到数组列表中
我需要将按钮添加到Android 如何将按钮添加到数组列表中,android,android-layout,android-listview,android-arrayadapter,Android,Android Layout,Android Listview,Android Arrayadapter,我需要将按钮添加到ArrayList中,并在for loop中动态地将侦听器添加到按钮中。在for循环中,我将动态地将列表添加到ArrayList并将其显示在屏幕上。请提供任何建议 public class Details extends ListActivity { ArrayList<String> listItems=new ArrayList<String>(); ArrayAdapter<String> adapter; JSONArray
ArrayList
中,并在for loop
中动态地将侦听器添加到按钮中。在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));
}
}
}
公共类详细信息扩展了ListActivity{
ArrayList listItems=新的ArrayList();
阵列适配器;
JSONArray all=新的JSONArray();
字符串大小;
字符串结果=”;
InputStream=null;
公共静态最终字符串MyPREFERENCES=“MyPrefs”;
SharedReferences SharedReferences;
@凌驾
创建时受保护的空隙(捆绑冰柱){
超级冰柱;
setContentView(R.layout.activity_详细信息);
adapter=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1,listItems);
setListAdapter(适配器);
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://rtotv.com/Details.jsp");
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
JSONArray jArray=新JSONArray(结果);
对于(int i=0;i您不必在列表项上放置按钮
。您可以添加侦听器
,然后可以捕获所选项。
我用过下面这样的东西,也许它能帮助你
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();
}
});
}
}
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_列表);
最终列表mylist=OtherActivity.listDBoperation.getAllList();
最终ListView customListView=(ListView)findViewById(R.id.ListView);
MyListAdapter MyListAdapter=新的MyListAdapter(listActivity.this,mylist);
setAdapter(myListAdapter);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView arg0,视图arg1,内部位置,长arg3){
Toast.makeText(getApplicationContext(),mylist.get(position).getListId(),Toast.LENGTH_LONG).show();
}
});
}
}
据我所知,我想您希望在listview中显示一个按钮?@FarhanShah感谢您的回复。我需要在ArrayList中添加按钮并显示它。尝试查看它:尝试以下操作: