-1

This is my code which is i'm working but the startActivity function is not working here and the click Listener is working. Please help me how is this possible.

This is my main Thread code:

restaurant_Array_list = new ArrayList<home_listview_model>();
        new get_restaurant_data().execute("http://unstoppable.io/food/androidApi");

        ListView listview = (ListView)findViewById(R.id.home_list);
        home_listadapter = new home_listadapter(getApplicationContext(), R.layout.home_list_view, restaurant_Array_list);

        listview.setAdapter(home_listadapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {
                Toast.makeText(getApplicationContext(), restaurant_Array_list.get(position).getRestaurant_name(), Toast.LENGTH_LONG).show();
            }
        });

This is AsyncTask code:

public class get_restaurant_data extends AsyncTask<String, Void, Boolean> {


    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(home.this);
        dialog.setMessage("Loading... please wait");
        dialog.show();
        dialog.setCancelable(false);
    }


    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            //------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("restaurant_list_data");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject jsonrealobject = jarray.getJSONObject(i);
                    home_listview_model lisviewarray = new home_listview_model();

                    lisviewarray.setRestaurant_id(jsonrealobject.getString("restaurant_id"));
                    lisviewarray.setRestaurant_name(jsonrealobject.getString("restaurant_name"));
                    lisviewarray.setRestaurant_address(jsonrealobject.getString("address"));
                    lisviewarray.setRestaurant_opping_time(jsonrealobject.getString("restaurant_opping_time"));

                    restaurant_Array_list.add(lisviewarray);
                }
                return true;
            }

            //------------------>>

        } catch (ParseException | IOException | JSONException e1) {
            e1.printStackTrace();
        }
        return false;
    }
    protected void onPostExecute(Boolean result) {
        dialog.cancel();
        home_listadapter.notifyDataSetChanged();
        if(result == false){
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
        }
    }
}

This is Array Adapter where i want to open new Activity:

    package com.example.nhp04.gqfood;

    import android.content.Context;
    import android.content.Intent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;

    import java.util.ArrayList;


    public class home_listadapter extends ArrayAdapter<home_listview_model> {

    ArrayList<home_listview_model> restaurant_Array_list;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;

    public home_listadapter(Context context, int resource, ArrayList<home_listview_model> objects) {
        super(context, resource, objects);
        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        restaurant_Array_list = objects;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // convert view = design
        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);
            holder.restaurant_id = (TextView)v.findViewById(R.id.restaurant_id);
            holder.restaurant_name = (TextView)v.findViewById(R.id.restaurant_name);
            holder.restaurant_address = (TextView)v.findViewById(R.id.address);
            holder.restaurant_opping_time = (TextView)v.findViewById(R.id.opningtime);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.restaurant_id.setText(restaurant_Array_list.get(position).getRestaurant_id());
        holder.restaurant_name.setText(restaurant_Array_list.get(position).getRestaurant_name());
        holder.restaurant_address.setText(restaurant_Array_list.get(position).getRestaurant_address());
        holder.restaurant_opping_time.setText("Opens at "+restaurant_Array_list.get(position).getRestaurant_opping_time());

        holder.restaurant_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(home_listadapter.this, restaurent.class));
            }
        });
        return v;

    }

    static class ViewHolder {
        public TextView  restaurant_id;
        public TextView  restaurant_name;
        public TextView  restaurant_address;
        public TextView  restaurant_opping_time;

    }
}
Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43

5 Answers5

1

Do like this...

holder.restaurant_name.setOnClickListener(new OnClickListener()
    {
        @Override 
        public void onClick(AdapterView<?> arg0, View arg1,int position, long arg3)
        { 
        Intent i = new Intent(context, restaurent.class);
        context.startActivity(i);
        }
    });
Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
1

Save your context on your home_listadapter constructor

Context mContext;

public home_listadapter(Context context, int resource, ArrayList<home_listview_model> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
restaurant_Array_list = objects;
mContext = context;
}

Then on your holder.restaurant_name.setOnClickListener

holder.restaurant_name.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(mContext, restaurent.class)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }
});
Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94
  • it's giving error Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag how i can handle this – Yaseen Ahmad Apr 13 '16 at 11:42
1

try this,

Do this in Your Adapter Constructor:

Context mContext;
public home_listadapter(Context context, int resource, ArrayList<home_listview_model> objects) {
    super(context, resource, objects);
   mContext=context;
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    restaurant_Array_list = objects;
}

And on Click start the activity:

mContext.startActivity(new Intent(mContext, restaurent.class));
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
0

Try this:

 Intent intent = new Intent(context, restaurent.class);
 ((Activity) context).startActivity(intent);
Jas
  • 3,207
  • 2
  • 15
  • 45
0

Do like this

 holder.restaurant_name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              Intent in=new Intent(v.getContext(),SingleItemView.class);
                    v.getContext().startActivity(in);

        }
    });
Rgv
  • 504
  • 5
  • 23