Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add onclick dynamically to arraylist

I am getting list of titles from DB and adding those titles to layout using ArrayList. This layout is created by using adapters. I want open another layout when i click on titles displayed in this layout.How can i solve this problem?Any suggestions please....

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FullDetails extends ListActivity  {

    ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
    private ListView lv;
    int clickCounter=0;
    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_full_details);
       adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
         setListAdapter(adapter);
        fullItems(null);


    }
JSONArray all=new JSONArray();
    String size;
    public void fullItems(View v) {
        String result = "";
         InputStream is=null;
        try{

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://MOTal.com/and/events.jsp");
                HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                     is = entity.getContent();
        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }
        try{
                    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();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }
        try{
                JSONArray jArray = new JSONArray(result);
                            for(int i=0;i<jArray.length();i++){

                        JSONObject json_data = jArray.getJSONObject(i);

                    size=json_data.getString("size");

                   for(int j=0;j<Integer.parseInt(size);j++){

                     all=json_data.getJSONArray("res"+j);
listItems.add(all.get(0)+" \n venue:"+all.get(1)+" Price:"+all.get(3));
                   }

                }

        }
        catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

        adapter.notifyDataSetChanged();
    }
}

activity_full_details.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
    />
</LinearLayout>
like image 841
PRK449 Avatar asked Mar 21 '23 05:03

PRK449


1 Answers

What you are looking for is List Item click listener. You can try lv<your listview's obj>.setOnItemCLickListener below is the example for you.

listView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    Toast.makeText(getApplicationContext(),
      "Click ListItem Number " + position, Toast.LENGTH_LONG)
      .show();
  }
}); 

Follow this basic example

And there is nothing like onClick to ArrayList, clicks are set for views.

like image 72
Hardik Trivedi Avatar answered Apr 02 '23 18:04

Hardik Trivedi