1

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





    }



}
}
4
  • what i understand,i think you want a button display in your listview? Commented Mar 20, 2014 at 7:16
  • @FarhanShah Thanks for your reply.I need add button in ArrayList and display it. Commented Mar 20, 2014 at 7:19
  • Try to look at it: stackoverflow.com/questions/1709166/… Commented Mar 20, 2014 at 7:20
  • try this: stackoverflow.com/questions/20283324/… Commented Mar 20, 2014 at 7:22

1 Answer 1

1

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

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.