As it is more convenient to implement in XML layout like this?

18a7bc06d6804bd18f646bb04ddcda42.jpg

Each terminal block (row) clickable. As easier to implement in the XML layout? Only interested in the block contacts. Just using LinearLayout to draw a Button with icon +2 textview on-one contact or is there a more humane widgets?
July 8th 19 at 16:23
1 answer
July 8th 19 at 16:25
Solution
As a variant - to create a custom list item and generate the adapter based on the input data. Well, and the listener learn to distinguish one from the other.

As an example (for the bad code soran :) ):

How it looks:

dd7030dc4f30465fba19dab8a8432a10.jpg

XML:

<?xml version="1.0" encoding="utf-8"?>

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap content" android:orientation="horizontal">

 <imageview android:id="@+id/imageOrderMenuItem" android:layout_width="wrap content" android:layout_height="wrap content" android:layout_gravity="center_vertical">

 <linearlayout android:layout_width="fill_parent" android:layout_height="wrap content" android:parent="8dp" android:orientation="vertical">

 <textview android:id="@+id/tvOrderListItemType" android:layout_width="wrap content" android:layout_height="wrap content" android:visibility="gone">

 <textview android:id="@+id/tvOrderListItemTitle" android:layout_width="fill_parent" android:layout_height="wrap content" android:textappearance="?android:attr/textAppearanceMediumInverse">

 <textview android:id="@+id/tvOrderListItemText" android:layout_width="fill_parent" android:layout_height="wrap content" android:textappearance="?android:attr/textAppearanceSmallInverse">
</textview></textview></textview></linearlayout>

</imageview></linearlayout>


Adapter:

// names of attributes to Map, which is used in drawing the screen order
 public final static String ATTRIBUTE_NAME_TYPE = "type";
 public final static String ATTRIBUTE_NAME_TITLE = "title";
 public final static String ATTRIBUTE_NAME_TEXT = "text";
 public final static String ATTRIBUTE_NAME_ICON = "icon";
 // array of names of attributes from which to read data
 public static final String[] orderListFrom = {
ATTRIBUTE_NAME_TYPE,
ATTRIBUTE_NAME_TITLE,
ATTRIBUTE_NAME_TEXT,
ATTRIBUTE_NAME_ICON
};
 // array of ID View-components that will insert the data
 public static final int[] orderListTo = {
R. id.tvOrderListItemType,
R. id.tvOrderListItemTitle,
R. id.tvOrderListItemText,
R. id.imageOrderMenuItem
};

 getSimpleAdapterForOrderMenu public SimpleAdapter(Context Context) {
 Map<string, object> m = new HashMap<>();

menuData.clear();

 // When
 m.put(ATTRIBUTE_NAME_TYPE, OrderMenuItemTypes.WHEN);
 m.put(ATTRIBUTE_NAME_TITLE, MAP_ORDER_MENU_ITEM_TYPE_TITLE.get(OrderMenuItemTypes.WHEN));
 m.put(ATTRIBUTE_NAME_TEXT, getFormattedOrderDateTime());
 m.put(ATTRIBUTE_NAME_ICON, R. drawable.ic_icon_clock);

menuData.add(m);
 m = new HashMap<>();

 // Where
 m.put(ATTRIBUTE_NAME_TYPE, OrderMenuItemTypes.FROM);
 m.put(ATTRIBUTE_NAME_TITLE, MAP_ORDER_MENU_ITEM_TYPE_TITLE.get(OrderMenuItemTypes.FROM));
 if (getFromPoint().getFormattedAddress(true) != null)
 m.put(ATTRIBUTE_NAME_TEXT, getFromPoint().getFormattedAddress(true));
else
 m.put(ATTRIBUTE_NAME_TEXT, MAP_ORDER_MENU_ITEM_DEF_TEXT.get(OrderMenuItemTypes.FROM));
 m.put(ATTRIBUTE_NAME_ICON, R. drawable.ic_icon_start);

menuData.add(m);
 m = new HashMap<>();

 // Using the optional
 if (getPointsCount() > 2) {
 for (int i = 1; i < getPointsCount() - 1; i++) {
 m.put(ATTRIBUTE_NAME_TYPE, OrderMenuItemTypes.THROUGH);
 m.put(ATTRIBUTE_NAME_TITLE, MAP_ORDER_MENU_ITEM_TYPE_TITLE.get(OrderMenuItemTypes.THROUGH));
 if (getOrderPoint(i).getFormattedAddress(false) != null)
 m.put(ATTRIBUTE_NAME_TEXT, this.getOrderPoint(i).getFormattedAddress(false));
else
 m.put(ATTRIBUTE_NAME_TEXT, MAP_ORDER_MENU_ITEM_DEF_TEXT.get(OrderMenuItemTypes.THROUGH));
 m.put(ATTRIBUTE_NAME_ICON, R. drawable.ic_icon_intermediate);
menuData.add(m);
 m = new HashMap<>();
}
}

 // Where
 m.put(ATTRIBUTE_NAME_TYPE, OrderMenuItemTypes.TO);
 m.put(ATTRIBUTE_NAME_TITLE, MAP_ORDER_MENU_ITEM_TYPE_TITLE.get(OrderMenuItemTypes.TO));
 if (getToPoint().getFormattedAddress(false) != null)
 m.put(ATTRIBUTE_NAME_TEXT, getToPoint().getFormattedAddress(false));
else
 m.put(ATTRIBUTE_NAME_TEXT, MAP_ORDER_MENU_ITEM_DEF_TEXT.get(OrderMenuItemTypes.TO));
 m.put(ATTRIBUTE_NAME_ICON, R. drawable.ic_icon_finish);

menuData.add(m);
 m = new HashMap<>();

 // Advanced
 m.put(ATTRIBUTE_NAME_TYPE, OrderMenuItemTypes.EXTRA);
 m.put(ATTRIBUTE_NAME_TITLE, MAP_ORDER_MENU_ITEM_TYPE_TITLE.get(OrderMenuItemTypes.EXTRA));
 m.put(ATTRIBUTE_NAME_TEXT, getOrderExtraInfo());
 m.put(ATTRIBUTE_NAME_ICON, R. drawable.ic_icon_options);

menuData.add(m);

 // create the adapter

 return new SimpleAdapter(
context,
menuData,
R. layout.order_list_item,
orderListFrom,
orderListTo);
 }</string>


Handler for clicking on a list:

// Handler for clicking on menu
lvOrder.setOnItemClickListener(
 new AdapterView.OnItemClickListener() {
@Override
 public void onItemClick(AdapterView<?> adapterView, View View, int i, long l) {
 OrderConst.OrderMenuItemTypes selectedMenuItemType = (OrderConst.OrderMenuItemTypes) ((HashMap) adapterView.getAdapter().getItem(i)).get(OrderConst.ATTRIBUTE_NAME_TYPE);
 Gson gson = new Gson();

 switch (selectedMenuItemType) {
 case WHEN:
 //Selected item "When"
 //Open the dialog with the choice - now, today, tomorrow
 SingleChoiceDialogFragment dialog = new SingleChoiceDialogFragment();

 Bundle bundle = new Bundle();
 bundle.putString(SingleChoiceDialogFragment.ID OrderConst.SINGLE_CHOICE_DIALOG_TYPE_TIME);
 bundle.putString(SingleChoiceDialogFragment.TITLE "When you need to take the car?");
 bundle.putStringArray(SingleChoiceDialogFragment.DATA OrderConst.orderTimeList);
 bundle.putInt(SingleChoiceDialogFragment.SELECTED, newOrder.getOrderDateMoment());
dialog.setArguments(bundle);

 dialog.show(getSupportFragmentManager(), "WhenDialog");
break;
 case EXTRA:
 //Select "Advanced"
 //Open the window select advanced options order
 OrderExtra Intent = new Intent(getApplicationContext(), OrderExtraActivity.class);
 orderExtra.putExtra(GlobalConst.INTENT_KEY_ORDER, gson.toJson(newOrder));
 startActivityForResult(orderExtra, OrderConst.REQUEST_CODE_ORDER);
break;
default:
 // Chose some of the items "From", "Through", "Where"
 int idx = (i - 1 < 0) ? 0 : i - 1;
 OrderAddressChoose Intent = new Intent(getApplicationContext(), OrderAddressActivityTabs.class);
 orderAddressChoose.putExtra(GlobalConst.INTENT_KEY_ADDRESS, gson.toJson(newOrder.getOrderPoint(idx)));
 startActivityForResult(orderAddressChoose, OrderConst.REQUEST_CODE_ADDRESS);
break;
}

}
 }

Find more questions by tags Android