我想计算通过JSON数组获取的元素数量,并使用.settext()方法将其显示在TextView上。 我得到以下JSON ARRAY:
{ "result": [ { "id":"283", "fullname":"shyam", "contactno":"898888888", "collegename":"mjc", "emailaddress":"shyamzawar", "eventname":"gaming" }, { "id":"285", "fullname":"ffca", "contactno":"8888888888", "collegename":"Brihan maharashtra College of Commerce (BMCC)", "emailaddress":"shyamzawar@ymail.com", "eventname":"FootBall" } ] }
以下是我的活动:
private static final String TAG = "userList";
private List<FeedItem> feedsList;
private RecyclerView mRecyclerView;
private MyRecyclerAdapter adapter;
private ProgressBar progressBar;
private TextView ParticipantsCounts;
int count=0;
private SwipeRefreshLayout swipeRefreshLayout;
private final String url="http://bmcctroika.hol.es/get-data.php";;
private int offSet = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
// Initialize recycler view
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
progressBar.setVisibility(View.VISIBLE);
ParticipantsCounts= (TextView) findViewById(R.id.ParticipantsCount);
// Downloading data from below url
new AsyncHttpTask().execute(url);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshitems();
}
});
}
private void refreshitems() {
new AsyncHttpTask().execute(url);
}
@Override
public void onRefresh() {
new AsyncHttpTask().execute(url);
}
public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
@Override
protected void onPreExecute() {
setProgressBarIndeterminateVisibility(true);
}
@Override
protected Integer doInBackground(String... params) {
Integer result = 0;
HttpURLConnection urlConnection;
try {
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
int statusCode = urlConnection.getResponseCode();
// 200 represents HTTP OK
if (statusCode == 200)
{
BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
{
response.append(line);
}
parseResult(response.toString());
result = 1; // Successful
} else {
result = 0; //"Failed to fetch data!";
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return result; //"Failed to fetch data!";
}
@Override
protected void onPostExecute(Integer result)
{
// Download complete.
progressBar.setVisibility(View.GONE);
if (result == 1)
{
adapter = new MyRecyclerAdapter(AdminActivity.this, feedsList);
mRecyclerView.setAdapter(adapter);
} else
{
Toast.makeText(AdminActivity.this, "Failed to fetch data!", Toast.LENGTH_SHORT).show();
}
swipeRefreshLayout.setRefreshing(false);
}
}
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("result");
feedsList = new ArrayList<>();
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
FeedItem item = new FeedItem();
item.setTitle(post.optString("fullname"));
item.setContact(post.optString("contactno"));
item.setEmail(post.optString("emailaddress"));
item.setEventname(post.optString("eventname"));
item.setCollegename(post.optString("collegename"));
feedsList.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
您需要获得lenght
result
的{{1}}。因此,在您的代码中,您可以获得JSONArray
- posts
的长度。
这将为您提供结果数组中的项目数:
JSONArray posts = response.optJSONArray("result");