What needs to be altered in the code so that the data is output not in a ListView, but in a TextView?

I give the code SecondActivity

public class SecondActivity extends Activity { public static String JsonURL; private static ArrayList<HashMap<String, Object>> myBooks; private static final String NAME = "name"; private static final String REIT = "reit"; private static final String BALANCE = "balance"; public ListView listView; /** @param result */ public void JSONURL(String result) { try { //создали читателя json объектов и отдали ему строку - result JSONObject json = new JSONObject(result); //дальше находим вход в наш json им является ключевое слово data JSONArray urls = json.getJSONArray("data"); //проходим циклом по всем нашим параметрам for (int i = 0; i < urls.length(); i++) { HashMap<String, Object> hm; hm = new HashMap<String, Object>(); //читаем что в себе хранит параметр balance hm.put(NAME, urls.getJSONObject(i).getString("name").toString()); //читаем что в себе хранит параметр reit hm.put(REIT, urls.getJSONObject(i).getString("reit").toString()); //читаем что в себе хранит параметр balance hm.put(BALANCE, urls.getJSONObject(i).getString("balance").toString()); myBooks.add(hm); //дальше добавляем полученные параметры в наш адаптер SimpleAdapter adapter = new SimpleAdapter(SecondActivity.this, myBooks, R.layout.list, new String[] { NAME, REIT, BALANCE, }, new int[] { R.id.text1, R.id.text2, R.id.text3 }); //выводим в листвю listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.url); listView = (ListView) findViewById(R.id.list); myBooks = new ArrayList<HashMap<String, Object>>(); //принимаем параметр который мы послылали в mainActivity Bundle extras = getIntent().getExtras(); //превращаем в тип стринг для парсинга String json = extras.getString(JsonURL); //передаем в метод парсинга JSONURL(json); }} 

And PHP JSON code

 <?php // серверная часть вывода json $login = $_POST['login']; $pass = $_POST['pass']; if($login == "user" & $pass == "pass") { ?> { "data":[{ "name":"Jurij", "reit":"150", "balance":"50" }] }<?php } ?> 
  • Your problem is not clear. It is not clear what causes you difficulties and what you tried to do to solve the problem. \ - YuriiSPb

2 answers 2

  1. You do not need to recreate the adapter and assign it at each iteration of the loop. You must first completely form a list with the data and only then, once and create and assign an adapter.

  2. To display it in TextView, you need to create a markup with it, programmatically find it in the markup, form a line for display and assign it to TextView.

  • @Lobs, what exactly you can’t do is not clear. In general, this line looks true, but in it I can immediately see 5 possible errors of a different type. For example, NAME , apparently, is a key, but not a value. - JuriySPb
  • The program works correctly. But it displays the data in the list, but I need to display the data in TextView. - Lobs 1:58 pm
  • @Lobs, that's understandable. It is not clear that you have difficulty. Listing is many times more difficult than in the text field. How could you do the first, but fail the second? - JuriySPb
  • The fact is that I found this code on the Internet as an example. And to alter it to fit your needs fails. I would like to know, as a novice programmer, what exactly needs to be removed and what to add instead. Or does it need all recode? - Lobs
  • @Lobs, you need to change the markup, removing the ListView from it and replacing TexyView instead. Then put the data from myBooks into it. Read somewhere else thread K-l lessons about markup and other basic things. If I just write you an app - this will not help you in studying ... - YuriySPb

Replaced:

 SimpleAdapter adapter = new SimpleAdapter(SecondActivity.this, myBooks, R.layout.list, new String[] { NAME, REIT, BALANCE, }, new int[] { R.id.text1, R.id.text2, R.id.text3 }); //выводим в листвъю listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

On

 textView20.setText((CharSequence) hm.get("name")); textView34.setText((CharSequence) hm.get("reit")); textView60.setText((CharSequence) hm.get("balance"));