Как отобразить данные из команды, которую я запускаю на ListView Android? - PullRequest
0 голосов
/ 07 июня 2019

У меня проблема с моим кодом, я хочу отобразить результаты команды, которую я запускаю в ListView

Теперь это мой код

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_log, container, false);

        // Untuk menjalankan API Mikrotik
        ApiConnection con = MainActivity.getCon();
        if (con !=null) {
            try {
                List<Map<String, String>> rs = con.execute("/log/print");
                for (Map<String, String> r : rs) {
                    ListView listView = (ListView) view.findViewById(R.id.viewLog);
                    ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
                            getActivity(),
                            android.R.layout.simple_list_item_1);
                    listView.setAdapter(listViewAdapter);
                }

            } catch (MikrotikApiException e) {
                e.printStackTrace();
            }
        }


        return view;
    }

Я пытался использовать System.Out, и данные могут отображаться

List<Map<String, String>> rs = con.execute("/log/print");
    for (Map<String, String> r : rs) {
        System.out.println(r);
    }

Показать данные с System.Out

06-08 02: 27: 32.433 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107 через api, themes = system, info, account, time = jun / 08 00:36:45, .id = * 1036} 06-08 02: 27: 32.433 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = 40: B0: 76: D5: DE: 00 @ wlan1: отключен, получен по умолчанию: отправка отправляющей станции (3), themes = wireless, info, time = jun / 08 00:50:14, .id = * 1037} 06-08 02: 27: 32.433 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: тег = 4, данные = {сообщение = 40: B0: 76: D5: DE: 00 @ wlan1: подключено, сигнал сила -51, темы = беспроводная, информация, время = июнь / 08 00:50:26, .id = * 1038} 06-08 02: 27: 32.433 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = wifi_dhcp отменены 10.10.2.103 с 40: B0: 76: D5: DE: 00, темы = dhcp, информация, время = июнь / 08 00:50:26, .id = * 1039} 06-08 02: 27: 32.433 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = wifi_dhcp, назначенный 10.10.2.103 для 40: B0: 76: D5: DE: 00, тематика = dhcp, информация, время = июнь / 08 00:50:26, .id = * 103A} 06-08 02: 27: 32.433 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь kkp вышел из 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:11:35, .id = * 103B} 06-08 02: 27: 32.433 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:12:04, .id = * 103C} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.100 через api, themes = system, info, account, time = jun / 08 01:13:29, .id = * 103D} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь kkp вышел из 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:25:07, .id = * 103E} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:25:37, .id = * 103F} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь kkp вышел из 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:26:38, .id = * 1040} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:27:33, .id = * 1041} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь kkp вышел из 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:27:40, .id = * 1042} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:28:18, .id = * 1043} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь kkp вышел из 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:29:00, .id = * 1044} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:29:15, .id = * 1045} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь kkp вышел из 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:30:10, .id = * 1046} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107через api, themes = system, info, account, time = jun / 08 01:30:25, .id = * 1047} 06-08 02: 27: 32.434 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь kkp вышел из 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:32:21, .id = * 1048} 06-08 02: 27: 32.444 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:32:53, .id = * 1049} 06-08 02: 27: 32.444 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь kkp вышел из 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:38:08, .id = * 104A} 06-08 02: 27: 32.444 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:39:02, .id = * 104B} 06-08 02: 27: 32.445 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь kkp вышел из 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:44:38, .id = * 104C} 06-08 02: 27: 32.445 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:44:53, .id = * 104D} 06-08 02: 27: 32.445 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь kkp вышел из 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:52:58, .id = * 104E} 06-08 02: 27: 32.445 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = user kkp, вошедший в систему с 10.10.2.107 через api, themes = system, info, account, time = jun / 08 01:53:13, .id = * 104F} 06-08 02: 27: 32.445 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = пользователь yogahilmi, вошедший с CC: 2D: 83: A4: 94: B8 через winbox, разделы = система, информация, учетная запись, время = июнь / 08 02:03:26, .id = * 1050} 06-08 02: 27: 32.445 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, данные = {сообщение = пользователь yogahilmi вышел из CC: 2D: 83: A4: 94: B8 через winbox, themes = system, info, account, time = jun / 08 02:04:46, .id = * 1051} 06-08 02: 27: 32.445 11252-11252 / com.tasanahetech.mikroboxv2 I / System.out: tag = 4, data = {message = wifi_dhcp отменены 10.10.2.108 из CC: 2D: 83: 82: 17: 57, темы = dhcp, информация, время = июнь / 08 02:19:56, .id = * 1052}

1 Ответ

0 голосов
/ 07 июня 2019

Объект карты r нельзя напрямую преобразовать в строку. Используйте

List<Map<String, String>> rs = con.execute("/log/print");
    for (Map<String, String> r : rs) {
        System.out.println(r.get("put the key here").toString());
    }

или если у вас нет ключей, сделайте это, чтобы напечатать все значения

 List<Map<String, String>> rs = con.execute("/log/print");
        for (Map<String, String> r : rs) {
            for(String valueStr:r.values()){
                 System.out.println(valueStr);
               }      
        }

Обновить .. использовать этот специальный адаптер

public class text extends ArrayAdapter<Map<String, String>> {

    List<Map<String, String>> rs;

    public text(@NonNull Context context, int resource, @NonNull List<Map<String, String>> objects) {
        super(context, resource, objects);

        this.rs = objects;// assigns your map object to rs
    }


    @Override
    public int getCount() {
        return rs.size(); //basically number of Items to be in the listview
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return super.getView(position, convertView, parent); // this is where you use your data

        // first inflate a custom view from xml

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView =inflater.inflate("custom view here", null); // the custom view

        LinearLayout l=convertView.findViewById("some layoutid inside the custom view");


        // Now below you can use your List
        Map<String, String> r = rs.get(position);
        for (String valueStr : r.values()) {

            TextView t=new TextView(getContext());
            t.setText(valueStr);

            l.addView(t);
        }


        return convertView;
    }
}
...