I described the Custom ListView for the GET request so that the name and the name of the city were on one line, but the result is not the same. What exactly is wrong? Tell me please.
public class MainActivity extends ActionBarActivity { private EditText editText; private ArrayList<String> addresses = new ArrayList<String>(); private ListView listView; //ArrayList<Currency> products = new ArrayList<Currency>(); BoxAdapter boxAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.edit_text); listView = (ListView)findViewById(R.id.listview); //ActionBar actionBar = getSupportActionBar(); //actionBar.setDisplayHomeAsUpEnabled(true); // создаем адаптер boxAdapter = new BoxAdapter(this, addresses); // настраиваем список ListView lvMain = (ListView) findViewById(R.id.listview); lvMain.setAdapter(boxAdapter); } private void refreshAdapter(){ ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,addresses); listView.setAdapter(adapter); } public void loadGetBtnClicked(View v){ CallWebServiceGet callWebServiceGet = new CallWebServiceGet(this); //callWebServiceGet.execute("https://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.xchange+where+pair+=+%22USDRUB,EURRUB%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="); //callWebServiceGet.execute("https://maps.googleapis.com/maps/api/geocode/json?latlng=12.823539,80.219490"); callWebServiceGet.execute("http://androiddocs.ru/api/friends.json"); } @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_http_get_example, 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 == android.R.id.home){ finish(); } return super.onOptionsItemSelected(item); } private class CallWebServiceGet extends AsyncTask<String,Void,String>{ private ProgressDialog progressDialog; public CallWebServiceGet(Context context){ progressDialog = ProgressDialog.show(context,"Loading","About to connect to server"); } @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.setMessage("Almost ready to connect with server..Doing pre checks"); } @Override protected String doInBackground(String... params) { progressDialog.setMessage("Connected to server..Please wait while we fetch results"); String url = params[0]; StringBuilder stringBuilder = new StringBuilder(); HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = null; try { httpGet = new HttpGet(new URI(url)); HttpResponse httpResponse = httpClient.execute(httpGet); // time taking process InputStream inputStream = httpResponse.getEntity().getContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"),2*1024); String line = null; while ((line = bufferedReader.readLine()) != null){ stringBuilder.append(line).append("\n"); } } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } return stringBuilder.toString(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); progressDialog.dismiss(); editText.setText(s); try { JSONObject jsonObject = new JSONObject(s); //JSONObject resultsObject = jsonObject.getJSONObject("friends"); //JSONObject resultOb = resultsObject.getJSONObject("results"); JSONArray resultArray = jsonObject.getJSONArray("friends"); addresses = new ArrayList<String>(); for (int i = 0; i<resultArray.length(); i++){ addresses.add(resultArray.getJSONObject(i).getString("name")); Log.d("DTA",resultArray.getJSONObject(i).getString("name")); } for (int j = 0; j<resultArray.length(); j++){ addresses.add(resultArray.getJSONObject(j).getString("city")); Log.d("DTA",resultArray.getJSONObject(j).getString("city")); } refreshAdapter(); } catch (JSONException e) { e.printStackTrace(); } } } This is my adapter
public class BoxAdapter extends BaseAdapter { private static ArrayList addresses; Context ctx; LayoutInflater lInflater; BoxAdapter(Context context, ArrayList<String> products) { ctx = context; addresses = products; lInflater = (LayoutInflater) ctx .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return addresses.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { view = lInflater.inflate(R.layout.item, parent, false); } Currency p = getProduct(position); TextView title2 = (TextView) view.findViewById(R.id.tvName); // title String song = addresses.get(position).toString(); title2.setText(song); System.out.println(title2); TextView title22 = (TextView) view.findViewById(R.id.tvPrice); // notice String song2 = addresses.get(position).toString(); title22.setText(song2); System.out.println(title22); return view; } Currency getProduct(int position) { return ((Currency) getItem(position)); } } For every MainActivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> And item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="" android:textSize="20sp"> </TextView> <TextView android:id="@+id/tvPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginRight="10dp" android:text=""> </TextView> </LinearLayout>