ListView with 2 TextViews

时间:2015-09-29 11:33:57

标签: java android android-listview ion

我正在尝试使用2个Textview发布每个项目的某个消息,但ListView on接受1个适配器并显示该适配器给出的数据

the current view

屏幕截图显示已检索到数据库中的数据并将其放入de ListView中,只缺少小文本数据。

代码:

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();
    }

如何制作它以便填充所有项目?

2 个答案:

答案 0 :(得分:0)

以下可能对您有所帮助:

  • 您需要自定义适配器扩展ArrayAdapterBaseAdapter才能实现您的观点。
  • 准备与您的要求相同的布局,即使用2 TextViewsImageView
  • 并且,getView方法根据您的要求填充数据。

This 可能会帮助您完成此过程。

希望有所帮助!!!

答案 1 :(得分:0)

您需要创建一个带有自定义视图/布局的自定义ArrayAdapter,在您的情况下包含两个textView,您可以看到我的old post带有用于创建自定义适配器的代码。

告诉我,如果我帮助你并做好编程!