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

时间:2014-03-20 07:08:56

标签: 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 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));





    }



}
}

1 个答案:

答案 0 :(得分:1)

您不必在列表项上添加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();
         }
      });

    }
}
相关问题