问题
I know that this question is asked 100 times, but for some reason i cannot find what is wrong in my code. I am very new to android and java. Basically i have a wordpress website from which I want to get the articles. I have installed the wp-rest-api v2 to get the data in json array format. In my android app I have
MainActivity.java
package al.im.imp;
/*ALL THE IMPORTS*/
public class ImpHome extends AppCompatActivity implements View.OnClickListener {
ListView lstTest;
JSONAdapter mJSONAdapter;
Button search_Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imp_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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();
}
});
//Access Listview
lstTest = (ListView) findViewById(R.id.home_list);
search_Button = (Button) findViewById(R.id.button1);
search_Button.setOnClickListener(this);
mJSONAdapter = new JSONAdapter(this, getLayoutInflater());
lstTest.setAdapter(mJSONAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_impakt_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void queryImp() {
// Create a client to perform networking
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://example.com/wp-json/wp/v2/posts",
new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONArray jsonArray) {
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
mJSONAdapter.updateData(jsonArray);
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Display a "Toast" message
// to announce the failure
Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();
// Log error message
// to help solve any problems
Log.e("Imp android", statusCode + " " + throwable.getMessage());
}
});
}
@Override
public void onClick(View v) {
queryImp();
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
}
My article_list.xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="75dp">
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"/>
</RelativeLayout>
and my JSONAdapter.java class
package al.imp.imp;
/*IMPORTS*/
public class JSONAdapter extends BaseAdapter{
Context mContext;
LayoutInflater mInflater;
JSONArray mJsonArray;
public JSONAdapter(Context context, LayoutInflater inflater) {
mContext = context;
mInflater = inflater;
mJsonArray = new JSONArray();
}
@Override
public int getCount() {
return mJsonArray.length();
}
@Override
public Object getItem(int position) {
return mJsonArray.optJSONObject(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.article_list, null);
holder = new ViewHolder();
holder.titleTextView = (TextView) convertView.findViewById(R.id.text_title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
JSONObject jsonObject = (JSONObject) getItem(position);
String articleTitle = "";
articleTitle = jsonObject.optJSONObject("title").optString("rendered");
holder.titleTextView.setText(articleTitle);
return convertView;
}
private static class ViewHolder {
public TextView titleTextView;
}
public void updateData(JSONArray jsonArray) {
// update the adapter's dataset
mJsonArray = jsonArray;
notifyDataSetChanged();
}
}
I dont know why this does not work. When i try to log the data like
for (int i = 1; i < jsonArray.length(); i++) {
Log.d("Title is:", jsonArray.optJSONObject(i).getJSONObject("title").getString("rendered").toString());
Log.d("Image Url is:", jsonArray.optJSONObject(i).getString("featured_image_thumbnail_url").toString());
}
it works perfectly, so I guess it is not a problem in getting the json response, but populating the listview.
Help me please
回答1:
After updating the data, try something like refreshing the list view like lstTest.invalidateViews();
- so your queryImp()
code will change into:
private void queryImp() {
// Create a client to perform networking
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://example.com/wp-json/wp/v2/posts",
new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONArray jsonArray) {
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
mJSONAdapter.updateData(jsonArray);
mJSONAdapter.notifyDataSetChanged();
//try refreshing the list-view like this:
lstTest.invalidateViews()
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Display a "Toast" message
// to announce the failure
Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();
// Log error message
// to help solve any problems
Log.e("Imp android", statusCode + " " + throwable.getMessage());
}
});
}
Let me know if the changes help.
来源:https://stackoverflow.com/questions/37014600/populating-listview-from-json