我正在尝试将数据传递给一个intent,我在onCreate()上添加了一个onclicklistener方法,但由于某种原因,当我按下任何列表视图项时,它没有为我打开意图。数组列表与主活动不在同一个类中。 这是我的 MainActivity(onCreate)代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new GetRestaurants(places,this,lv).execute();
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ArrayAdapter<Restaurant> adapter = new CustomAdapter(this, 0, places);
ListView listView = (ListView) findViewById(R.id.customListView);
listView.setAdapter(adapter);
AdapterView.OnItemClickListener adapterViewListener = new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Restaurant restaurant = places.get(position);
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("name", restaurant.getName());
startActivity(intent);
}
};
listView.setOnItemClickListener(adapterViewListener);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
我的意图活动:
public class DetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView nameTV = (TextView) findViewById(R.id.name);
Intent intent = getIntent();
String name1= intent.getStringExtra("name");
nameTV.setText(name1);
}}
这是我的自定义适配器,它可以获取餐厅的详细信息:
public class CustomAdapter extends ArrayAdapter<Restaurant> {
private Context context;
private List<Restaurant> places;
public CustomAdapter(Context context, int resource, ArrayList<Restaurant> objects) {
super(context, resource, objects);
this.context = context;
this.places = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
Restaurant restaurant = places.get(position);
//get the inflater and inflate the XML layout for each item
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.search_layout, null);
TextView name = (TextView) view.findViewById(R.id.name);
TextView address1 = (TextView) view.findViewById(R.id.address1);
TextView menu = (TextView) view.findViewById(R.id.menu);
TextView price = (TextView) view.findViewById(R.id.price);
TextView phone = (TextView) view.findViewById(R.id.phone);
TextView rate = (TextView) view.findViewById(R.id.rate);
name.setText("Name " + restaurant.getName());
address1.setText("Address " +restaurant.getAddress1());
menu.setText("Cuisine "+restaurant.getMenu_type());
price.setText("Price " + Integer.toString(restaurant.getCost()));
phone.setText("Phone " +Integer.toString(restaurant.getPhone()));
rate.setText( "Rate " +Float.toString(restaurant.getRate()));
return view;
}}
这是我填充后点击列表视图上的任何项目后出现的错误:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
com.example.abdulhakim.navbar.MainActivity$1.onItemClick(MainActivity.java:52)
03-05 22:02:53.212 25306-25306/com.example.abdulhakim.navbar
代码
GetRestaurant
public class GetRestaurants extends AsyncTask<Void, Void, Void> {
private String host ="";
private String port = "";
private String Search="/api/restaurants/search/";
private String getplace="/api/restaurants/search/";
private String register="api/users/add";
private String url = "http://#DELETED#/api/restaurants/search/?key=Burger&col=menu_type";
private String TAG = MainActivity.class.getSimpleName();
private ArrayList<Restaurant>places;
private Context context;
private Dialog dialog;
private ListView lv;
public GetRestaurants(ArrayList<Restaurant> places,Context context,ListView lv) {
this.places = places;
this.context=context;
this.lv=lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(context,ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(true);
dialog.show();
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ArrayAdapter<Restaurant> adapter = new CustomAdapter(context, 0, places);
ListView lv = (ListView) findViewById(R.id.customListView);
lv.setAdapter(adapter);
// Dismiss the progress dialog
if (dialog.isShowing())
dialog.dismiss();
}
protected Void doInBackground(Void... arg0) {
places = new ArrayList<>();
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray restaurants = jsonObj.getJSONArray("restaurants");
// looping through the JSON object
for (int i = 0; i < restaurants.length(); i++) {
JSONObject c = restaurants.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String address1 = c.getString("address1");
String address2 = c.getString("address2");
String lat = c.getString("lat");
String lng = c.getString("lng");
String cost = c.getString("cost");
String menu_type = c.getString("menu_type");
String rate = c.getString("rate");
String offer = c.getString("offer");
// Phone node is JSON Object
String mobile = c.getString("phone");
places.add(new Restaurant(Integer.parseInt(id),name,address1,address2,Integer.parseInt(mobile)
,Float.parseFloat(lat),Float.parseFloat(lng)
,Integer.parseInt(cost),menu_type,Float.parseFloat(rate),offer));
}
}
catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
} else {
Log.e(TAG, "Couldn't get json from server.");
}
return null;
}
}