3bu1
3bu1

Reputation: 985

How to add button to the array list

I need to add button to ArrayList and listener to the button dynamically in for loop. In for loop i am adding listing dynamically to ArrayList and displaying it on the screen.Any suggestions please.

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));





    }



}
}

Upvotes: 1

Views: 1275

Answers (1)

Hasan G&#252;ner
Hasan G&#252;ner

Reputation: 53

You don't have to put a Button on your list items. You can add a onItemClickListener and you can catch the selected item.
I have used something like below, maybe it can help you.

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();
         }
      });

    }
}

Upvotes: 1

Related Questions