我想在 Array Adapter 中打开新的 activity 告诉我它是如何实现的
i want to open new activity in Array Adapter tell me how its possible
这是我正在运行的代码,但 startActivity 函数在这里不起作用,点击监听器正在运行。请帮我看看这怎么可能。
这是我的主线程代码:
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();
}
});
这是 AsyncTask 代码:
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();
}
}
}
这是阵列适配器,我想在其中打开新的 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;
}
}
像这样...
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);
}
});
将上下文保存在 home_listadapter 构造函数中
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;
}
然后在你的 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);
}
});
试试这个:
Intent intent = new Intent(context, restaurent.class);
((Activity) context).startActivity(intent);
试试这个,
在您的适配器构造函数中执行此操作:
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;
}
点击开始 activity:
mContext.startActivity(new Intent(mContext, restaurent.class));
这样做
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);
}
});
这是我正在运行的代码,但 startActivity 函数在这里不起作用,点击监听器正在运行。请帮我看看这怎么可能。
这是我的主线程代码:
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();
}
});
这是 AsyncTask 代码:
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();
}
}
}
这是阵列适配器,我想在其中打开新的 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;
}
}
像这样...
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);
}
});
将上下文保存在 home_listadapter 构造函数中
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;
}
然后在你的 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);
}
});
试试这个:
Intent intent = new Intent(context, restaurent.class);
((Activity) context).startActivity(intent);
试试这个,
在您的适配器构造函数中执行此操作:
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;
}
点击开始 activity:
mContext.startActivity(new Intent(mContext, restaurent.class));
这样做
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);
}
});