我正在尝试将项目添加到列表视图并设置适配器。但是我不知道怎么了,因为我无法将项目设置为列表->适配器。
主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Json
new GetDataTask().execute();
//Datas
categoryModelArrayList = new ArrayList<>();
categoryAdapter = new CategoryAdapter(MainActivity.this, categoryModelArrayList);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(categoryAdapter);
//Drawer
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@SuppressLint("StaticFieldLeak")
private class GetDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
String url = Constant.baseUrl+"?json=get_category_index";
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("categories");
for (int i = 0; i < jsonArray.length(); i++) {
CategoryModel categoryModel = new CategoryModel();
JSONObject jo = jsonArray.getJSONObject(i);
String id = jo.getString("id");
String title = jo.getString("title");
Log.d("Veri", id);
categoryModel.setCatID(id);
categoryModel.setCatTitle(title);
categoryModelArrayList.add(categoryModel);
}
} catch (JSONException e) {
e.printStackTrace();
Log.i("Error", e.getLocalizedMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Anything you want
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(categoryModelArrayList.size() > 0) {
categoryAdapter.notifyDataSetChanged();
} else {
System.out.println("No data");
}
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
protected void onResume(){
super.onResume();
}
@Override
protected void onPause(){
super.onPause();
}
@Override
protected void onDestroy(){
super.onDestroy();
}
我正在使用您看到的代码提取数据。来自服务器的数据成功发送。我正在尝试将此数据发送到model
,然后发送到adapter
。但是以某种方式不会发生。我确定我忘记了一些东西,但是我不知道什么:)有什么想法吗?