简体   繁体   中英

ListView with 2 TextViews

i am trying to post a certain message per item with 2 Textviews, however the ListView on accepts 1 adapter and shows the data given by that adapter

当前视图

the Screenshot shows that the data from the database has been retrieved and put in de ListView only the Small Text Data is missing.

Code:

public class ReservationsActivity extends AppCompatActivity {    

List<String> subTextList = new ArrayList<>();
List<String> titleList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reservations);
    listReservations = (ListView)
    findViewById(R.id.listReservationsView);}

public void getReservations() {
        subTextList = new ArrayList<>();
        titleList = new ArrayList<>();
        titleAdapter = new ArrayAdapter<>(this, R.layout.row, R.id.title, titleList);
        subTextAdapter = new ArrayAdapter<>(this,R.layout.row,R.id.subText, subTextList);
        listReservations.setAdapter(subTextAdapter);
        listReservations.setAdapter(titleAdapter);

        AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ReservationsActivity.this);
                String accessToken = prefs.getString("accessToken", null);
                String web_adres = getString(R.string.web_adres);
                String ip_adres = getString(R.string.ip_adres);
                Ion.with(ReservationsActivity.this)
                        .load(web_adres + "/reservations?accessToken=" + accessToken)
                        .proxy(ip_adres, 8080)
                        .asJsonArray()
                        .setCallback(new FutureCallback<JsonArray>() {
                            @Override
                            public void onCompleted(Exception e, JsonArray result) {

                                jsonArray = result;
                                for (int i = 0; i < result.size(); i++) {
                                    JsonObject jsonObject = result.get(i).getAsJsonObject();
                                    JsonObject date = jsonObject.get("date").getAsJsonObject();
                                    JsonObject time = jsonObject.get("time").getAsJsonObject();
                                    String title = date.get("dayOfMonth").toString() + "/"
                                            + date.get("monthValue").toString() + "/"
                                            + date.get("year").toString();
                                    String subtext;
                                    if (time.get("id").getAsInt() == 1) {
                                        subtext = getString(R.string.morning);
                                        //imageInt = R.drawable.parking_morning;
                                    } else if (time.get("id").getAsInt() == 2) {
                                        subtext = getString(R.string.afternoon);
                                        //imageInt = R.drawable.parking_afternoon;
                                    } else if (time.get("id").getAsInt() == 3) {
                                        subtext = getString(R.string.day);
                                        //imageInt = R.drawable.parking_fullday;
                                    } else
                                        subtext = getString(R.string.day);


                                    titleAdapter.add(title);
                                    subTextAdapter.add(subtext);
                                    //TODO set Image per item
                                    //image.setImageResource(imageInt);
                                    titleAdapter.notifyDataSetChanged();
                                    subTextAdapter.notifyDataSetChanged();
                                }

                            }
                        });
                return null;
            }
        };
        task.execute();
    }

how would one make it so that it populates all the items?

Following might help you:

  • You need to make Custom Adapter extending ArrayAdapter or BaseAdapter in order to achieve your perspective.
  • Prepare a layout same as your requirement, ie with 2 TextViews and an ImageView .
  • And, in the getView method populate the data as per your requirement.

This might help you in the process.

Hope that helps!!!

You need to create a custom ArrayAdapter with a custom view/layout in your case contains two textViews, you can see my old post with code for create a custom adapter.

Tell me if I helped you and good programming!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM