I cannot see the old values of a property of custom component on Scene Builder.
I want to get the old value of "Speed Unit Type" to change the Min/Max values accordingly.
Maybe I can use static Min / Max values and bind these to "Speed Unit Type". But just wonderwhether it is possible to use old values at design tima on SB.
public static enum SpeedUnitType {KILO_METERS_PER_HOUR,METERS_PER_SECOND,DATA_MILES_PER_HOUR}
public static final int SPEED_MAX_DEFAULT_VALUE = 1000;
public static final int SPEED_MAX_LIMIT_VALUE = 999;
public static final double SPEED_MIN_LIMIT_VALUE = -100;
private final IntegerProperty minValue = new SimpleIntegerProperty(0);
private final IntegerProperty maxValue = new SimpleIntegerProperty(Integer.MAX_VALUE);
private final IntegerProperty value = new SimpleIntegerProperty(0);
private SpeedUnitType _speedUnit = SpeedUnitType.KILO_METERS_PER_HOUR;
private ObjectProperty<SpeedUnitType> speedUnitTypeProperty ;
public final SpeedUnitType getSpeedUnitType() {
return null == speedUnitTypeProperty ? _speedUnit : speedUnitTypeProperty.get();
}
public final void setSpeedUnitType(final SpeedUnitType UNIT) {
if (null == speedUnitTypeProperty) {
_speedUnit = UNIT;
} else {
speedUnitTypeProperty.set(UNIT);
}
}
public final ObjectProperty<SpeedUnitType> speedUnitTypeProperty() {
if (null == speedUnitTypeProperty) {
speedUnitTypeProperty = new SimpleObjectProperty<>(this, "speedUnitTypeProperty", _speedUnit);
}
return speedUnitTypeProperty;
}
public Speed() {
this.construct();
}
public void setMinValue(int min){ this.minValue.set(min); }
public int getMinValue() { return this.minValue.get(); }
public IntegerProperty minValueProperty() { return this.minValue;}
public void setMaxValue(int max) { this.maxValue.set(max);}
public int getMaxValue(){ return this.maxValue.get();}
public IntegerProperty maxValueProperty() { return this.maxValue; }
public final int getValue() { return value.get(); }
public final void setValue(final int VALUE) { value.set(VALUE);}
public final IntegerProperty valueProperty() {return value;}
private void construct()
{
this.maxValueProperty().addListener((observable, oldValue, newValue) ->
{
try
{
System.out.println("maxValueProperty --> The value was changed from " + oldValue.toString() + " to " + newValue.toString());
}
catch (Exception e)
{
System.err.println("Exception: " + e.getLocalizedMessage());
setText("ERROR");
}
});
speedUnitTypeProperty().addListener((observable, oldValue, newValue) -> {
this.convertSpeedUnit(oldValue, newValue);
});
// this.maxValueProperty().bind(Bindings.add(10, this.minValueProperty())); test to bind property min to property max
}
// ******************** Utility Methods ***********************************
public void convertSpeedUnit(SpeedUnitType oldValue, SpeedUnitType newValue)
{
if (oldValue == null)
return;
if (oldValue == newValue){}
else{
switch (oldValue){
case KILO_METERS_PER_HOUR:
System.out.println("convertSpeedUnit::formerUnit - KILO_METERS_PER_HOUR");
switch (newValue){
case METERS_PER_SECOND:
System.out.println("convertSpeedUnit::currentUnit - METERS_PER_SECOND");
setMinValue(10);
setMaxValue(20);
break;
case DATA_MILES_PER_HOUR:
System.out.println("convertSpeedUnit::currentUnit - DATA_MILES_PER_HOUR");
setMinValue(11);
setMaxValue(21);
break;
}
break;
case METERS_PER_SECOND:
System.out.println("convertSpeedUnit::formerUnit - METERS_PER_SECOND");
switch (newValue){
case KILO_METERS_PER_HOUR:
System.out.println("convertSpeedUnit::currentUnit - KILO_METERS_PER_HOUR");
setMinValue(12);
setMaxValue(22);
break;
case DATA_MILES_PER_HOUR:
System.out.println("convertSpeedUnit::currentUnit - DATA_MILES_PER_HOUR");
setMinValue(13);
setMaxValue(23);
break;
}
break;
case DATA_MILES_PER_HOUR:
System.out.println("convertSpeedUnit::formerUnit - DATA_MILES_PER_HOUR");
switch (newValue){
case KILO_METERS_PER_HOUR:
System.out.println("convertSpeedUnit::currentUnit - KILO_METERS_PER_HOUR");
setMinValue(34);
setMaxValue(45);
break;
case METERS_PER_SECOND:
System.out.println("convertSpeedUnit::currentUnit - METERS_PER_SECOND");
setMinValue(56);
setMaxValue(67);
break;
}
break;
}
}
}
Related
After first qr code scan i make API call and get data. its work fine. But later when i scan other QRcode the data is retrieved but its not shown in inner recyclerview, mean no expand and collapse function works.
after instantiating recyclerview, adapter & layout manager. i scan qrcode and with result i make api call. then observe it and send data for first binding.
Below is Observer.
//ViewModel Observer
private void observeVIewModel() {
qrTrac_viewModel.getQRTrackListLiveData().observe(this, qrTrackingList -> {
if (qrTrac_viewModel.getSerResponseCodeVModel() != null) {
if (qrTrac_viewModel.getSerResponseCodeVModel().equals(Constants.OK)){
if (qrTrackingList != null) {
Util.Logd(qrTrackingList.getLstQRL0().toString());
progressActivity.dismiss();
Util.Logd("qrtrack viewmodel call");
mLstQRL0Adapter.setLstQRL0List((ArrayList<LstQRL0>) qrTrackingList.getLstQRL0(),(ArrayList<LstQRL1Info>) qrTrackingList.getLstQRL1Info(),
(ArrayList<LstQRTrackingSummary>) qrTrackingList.getLstQRTrackingSummary());
}
} else if(qrTrac_viewModel.getSerResponseCodeVModel().equals(Constants.INTERNAL_SERVER_ERROR)){
progressActivity.dismiss();
Util.Loge("in not found");
Snackbar snackbar = Snackbar.make(view, "Internal Server Error", BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, view -> {
});
snackbar.show();
}
else {
progressActivity.dismiss();
Snackbar snackbar = Snackbar.make(view, qrTrac_viewModel.getSerVModelResponseMsg(), BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, v -> {});
snackbar.show();
}
}
else {
progressActivity.dismiss();
Snackbar snackbar = Snackbar.make(view, "Some Unknown Error occured", BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, v -> {});
snackbar.show();
}
});
}
Here is first Adapter.
public class LstQRL0Adapter extends RecyclerView.Adapter<LstQRL0Adapter.LstQRL0ViewHolder> {
private Context context;
private List<LstQRL0> listQRL0 = new ArrayList<>();
private List<LstQRL1Info> listQRl1Info = new ArrayList<>();
private List<LstQRTrackingSummary> listQRTracSumm = new ArrayList<>();
private View view;
private LayoutInflater inflater;
private ArrayList<LstQRL1Info> templistQRl1Info = new ArrayList<>();
private int oldL0ClickPosition = -1;
//lstQRLInfo
private RecyclerView mLstQRL1InfoRecView;
private LstQRL1InfoAdapter mLstQRL1InfoAdapter;
private LinearLayoutManager mLst1Manager;
public LstQRL0Adapter(Context context) {
inflater = LayoutInflater.from(context);
this.context = context;
}
public void setLstQRL0List(ArrayList<LstQRL0> list, ArrayList<LstQRL1Info> listQRl1Info, ArrayList<LstQRTrackingSummary> listQRTracSumm) {
this.listQRL0 = list;
this.listQRl1Info = listQRl1Info;
this.listQRTracSumm = listQRTracSumm;
oldL0ClickPosition = -1 ;
notifyDataSetChanged();
#NonNull
#Override
public LstQRL0Adapter.LstQRL0ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
view = inflater.inflate(R.layout.lstqrl0_baseview, parent, false);
return new LstQRL0ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull LstQRL0Adapter.LstQRL0ViewHolder holder, int position) {
LstQRL0 myL0List = listQRL0.get(position);
holder.mSeason.setText(myL0List.getNvGDSeason());
holder.mStyle.setText(myL0List.getStyleBriefDescr().replace("<br/>", "\n"));
holder.mVendor.setText(myL0List.getNvPartnerVendor());
mLstQRL1InfoAdapter = new LstQRL1InfoAdapter(context);
mLstQRL1InfoRecView.setAdapter(mLstQRL1InfoAdapter);
mLstQRL1InfoRecView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
templistQRl1Info.clear();
for (int i = 0; i < listQRl1Info.size(); i++) {
if (listQRl1Info.get(position).getId().equals(listQRl1Info.get(i).getId()) && !listQRl1Info.get(i).getId().isEmpty()) {
Util.Logd(listQRl1Info.get(i).getId() + " is l0 adapter");
templistQRl1Info.add(listQRl1Info.get(i));
}
}
Util.Logd(templistQRl1Info.size() +" is temp l0 size");
mLstQRL1InfoAdapter.setLstQRL1infoList(templistQRl1Info, (ArrayList<LstQRTrackingSummary>) listQRTracSumm);
if (listQRL0.get(position).isL0Expanded()) {
holder.mLstQrL0ImageView.setImageResource(R.drawable.ic_arrow_drop_down);
mLstQRL1InfoRecView.setVisibility(View.VISIBLE);
} else {
holder.mLstQrL0ImageView.setImageResource(R.drawable.ic_arrow_right);
mLstQRL1InfoRecView.setVisibility(View.GONE);
}
view.setOnClickListener(view -> {
if (oldL0ClickPosition >= 0){
if (listQRL0.get(oldL0ClickPosition).isL0Expanded()) {
listQRL0.get(oldL0ClickPosition).setL0Expanded(false);
mLstQRL1InfoRecView.setVisibility(View.GONE);
notifyItemChanged(oldL0ClickPosition);
}
}
if (oldL0ClickPosition == holder.getAdapterPosition()){
oldL0ClickPosition = -1 ;
}else {
oldL0ClickPosition = holder.getAdapterPosition() ;
}
Util.Logd("Old clickc is "+oldL0ClickPosition);
if (oldL0ClickPosition >= 0){
new Handler(Looper.myLooper()).postDelayed(() -> {
listQRL0.get(oldL0ClickPosition).setL0Expanded(!listQRL0.get(oldL0ClickPosition).isL0Expanded());
notifyItemChanged(oldL0ClickPosition);
}, 500);
}
});
}
#Override
public int getItemCount() {
return listQRL0.size();
}
public class LstQRL0ViewHolder extends RecyclerView.ViewHolder {
private TextView mSeason, mStyle, mVendor;
private ImageView mLstQrL0ImageView;
public LstQRL0ViewHolder(#NonNull View itemView) {
super(itemView);
Util.Logd("just check is called");
mSeason = itemView.findViewById(R.id.lstqrl0_season);
mStyle = itemView.findViewById(R.id.lstqrl0_style);
mVendor = itemView.findViewById(R.id.lstqrl0_vendor);
mLstQrL0ImageView = itemView.findViewById(R.id.lstqr10_arrow);
mLstQRL1InfoRecView = itemView.findViewById(R.id.lstqrl1info_rv);
}
}
}
Here is Second Adapter
public class LstQRL1InfoAdapter extends RecyclerView.Adapter<LstQRL1InfoAdapter.LstQRL1InfoViewHolder> {
private Context context;
private List<LstQRL1Info> lstQRL1Infos = new ArrayList<>();
private List<LstQRTrackingSummary> listQRTracSumm = new ArrayList<>();
private ArrayList<LstQRTrackingSummary> tempListQRTracSumm = new ArrayList<>();
private View view;
private LayoutInflater inflater;
private int oldL1ClickPosition = 0; // not used in this case
//LstQRTracingSummary
private RecyclerView mQRLTracSummRV;
private LstQRTracSummAdapter mQrTracSummAdapter;
private LinearLayoutManager mQrTrackSummManager;
public LstQRL1InfoAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public void setLstQRL1infoList(ArrayList<LstQRL1Info> list, ArrayList<LstQRTrackingSummary> listQRTracSumm) {
this.lstQRL1Infos = list;
this.listQRTracSumm = listQRTracSumm;
notifyDataSetChanged();
}
#NonNull
#Override
public LstQRL1InfoViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
view = inflater.inflate(R.layout.lstqrl1info_baseview, parent, false);
return new LstQRL1InfoViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull LstQRL1InfoViewHolder holder, int position) {
holder.bind(lstQRL1Infos.get(position),position);
}
#Override
public int getItemCount() {
return lstQRL1Infos.size();
}
public class LstQRL1InfoViewHolder extends RecyclerView.ViewHolder {
private TextView quantity, info, add_info;
private ImageView mlstqrl1info_arrow;
public LstQRL1InfoViewHolder(#NonNull View itemView) {
super(itemView);
quantity = itemView.findViewById(R.id.lstqr1info_quantity);
info = itemView.findViewById(R.id.lstqr1info_info);
add_info = itemView.findViewById(R.id.lstqrl1info_add_info);
mQRLTracSummRV = itemView.findViewById(R.id.lstqrtracsumm_rv);
mlstqrl1info_arrow = itemView.findViewById(R.id.lstqr1info_arrow);
itemView.setOnClickListener(view -> {
if (lstQRL1Infos.get(oldL1ClickPosition).isL1Expanded()) {
Util.Logd("in expanded check");
lstQRL1Infos.get(oldL1ClickPosition).setL1Expanded(false);
mQRLTracSummRV.setVisibility(View.GONE);
notifyItemChanged(oldL1ClickPosition);
}
oldL1ClickPosition = getAdapterPosition() ;
new Handler(Looper.myLooper()).postDelayed(() -> {
}, 500);
lstQRL1Infos.get(getAdapterPosition()).setL1Expanded(!lstQRL1Infos.get(getAdapterPosition()).isL1Expanded());
notifyItemChanged(getAdapterPosition());
});
}
public void bind(LstQRL1Info lstQRL1Info,int position) {
quantity.setText(Math.round(lstQRL1Info.getmQty())+"");
info.setText(lstQRL1Info.getNvGDActivity());
add_info.setText(lstQRL1Info.getNvAdditionalInfo1());
mQrTracSummAdapter = new LstQRTracSummAdapter(context);
mQRLTracSummRV.setAdapter(mQrTracSummAdapter);
mQRLTracSummRV.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
tempListQRTracSumm.clear() ;
for (int i = 0; i < listQRTracSumm.size(); i++) {
if (lstQRL1Info.getId().equals(listQRTracSumm.get(i).getIdQRTrackingList()) && !listQRTracSumm.get(i).getIdQRTrackingList().isEmpty()) {
tempListQRTracSumm.add(listQRTracSumm.get(i));
Util.Logd(listQRTracSumm.get(i).getIdQRTrackingList() + " is info adapter");
}
}
Util.Logd("in view binding and size is " + lstQRL1Infos.size() + " "+ tempListQRTracSumm.size());
mQrTracSummAdapter.setListQRTracSumm(tempListQRTracSumm);
if (lstQRL1Infos.get(position).isL1Expanded()) {
mlstqrl1info_arrow.setImageResource(R.drawable.ic_arrow_drop_down);
mQRLTracSummRV.setVisibility(View.VISIBLE);
} else {
mlstqrl1info_arrow.setImageResource(R.drawable.ic_arrow_right);
mQRLTracSummRV.setVisibility(View.GONE);
}
}
}
}
Third adapter is also same as like second one.
First Adapter is bit different from second and third as am working on it to achieve what is needed.
Probelem : When i scan second and subsequent times then data from api call is received and i think properly set as well but somewhow its not show recyclerview 2 data.(First recyclerview data is shown properly).
Help me whether its different approach or update in same code.
Thanks in Advance.
UPDATE - Above code is correct , the mistake I was doing is -> I was creating RecyclerView adapter instance in advance when Activity runs for 1st time (Which is advance initialization of adapter) and was updating data later when QrCode is scanned and Api call is made .
So now I was creating instance when data is received by observer . as Shown Below
private void observeVIewModel() {
qrTrac_viewModel.getQRTrackListLiveData().observe(this, qrTrackingList -> {
if (qrTrac_viewModel.getSerResponseCodeVModel() != null) {
if (qrTrac_viewModel.getSerResponseCodeVModel().equals(Constants.OK)){
if (qrTrackingList != null) {
Util.Logd(qrTrackingList.getLstQRL0().toString());
progressActivity.dismiss();
Util.Logd("qrtrack viewmodel call");
// result sent to adapters for binding
mLstQRL0Adapter = new LstQRL0Adapter(this,(ArrayList<LstQRL0>) qrTrackingList.getLstQRL0(),(ArrayList<LstQRL1Info>) qrTrackingList.getLstQRL1Info(),
(ArrayList<LstQRTrackingSummary>) qrTrackingList.getLstQRTrackingSummary());
mLst0Manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mLst0RecView.setLayoutManager(mLst0Manager);
mLst0RecView.setNestedScrollingEnabled(false);
mLst0RecView.setRecycledViewPool(sharedPool);
mLst0RecView.setAdapter(mLstQRL0Adapter);
mLstQRL0Adapter.notifyDataSetChanged();
}
} else if(qrTrac_viewModel.getSerResponseCodeVModel().equals(Constants.INTERNAL_SERVER_ERROR)){
progressActivity.dismiss();
Util.Loge("in not found");
Snackbar snackbar = Snackbar.make(view, "Internal Server Error", BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, view -> {
});
snackbar.show();
}
else {
progressActivity.dismiss();
Snackbar snackbar = Snackbar.make(view, qrTrac_viewModel.getSerVModelResponseMsg(), BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, v -> {});
snackbar.show();
}
}
else {
progressActivity.dismiss();
Snackbar snackbar = Snackbar.make(view, "Some Unknown Error occured", BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, v -> {});
snackbar.show();
}
});
}
Same way I was doing for further adapters for nested lists to work better .
1st dapter.
public class LstQRL0Adapter extends RecyclerView.Adapter<LstQRL0Adapter.LstQRL0ViewHolder> {
private Context context;
private List<LstQRL0> listQRL0 = new ArrayList<>() ;
private List<LstQRL1Info> listQRl1Info = new ArrayList<>(); ;
private List<LstQRTrackingSummary> listQRTracSumm = new ArrayList<>();
private View view;
private LayoutInflater inflater;
private ArrayList<LstQRL1Info> templistQRl1Info = new ArrayList<>();
private int oldL0ClickPosition = -1;
private Boolean isFromClose = false ;
//lstQRLInfo
private RecyclerView mLstQRL1InfoRecView;
public LstQRL1InfoAdapter mLstQRL1InfoAdapter;
public LstQRL0Adapter(Context context,ArrayList<LstQRL0> list, ArrayList<LstQRL1Info> listQRl1Info, ArrayList<LstQRTrackingSummary> listQRTracSumm) {
inflater = LayoutInflater.from(context);
this.context = context;
this.listQRL0 = list;
this.listQRl1Info = listQRl1Info;
this.listQRTracSumm = listQRTracSumm;
oldL0ClickPosition = -1;
}
#NonNull
#Override
public LstQRL0Adapter.LstQRL0ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
view = inflater.inflate(R.layout.lstqrl0_baseview, parent, false);
return new LstQRL0ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull LstQRL0Adapter.LstQRL0ViewHolder holder, int position) {
LstQRL0 myL0List = listQRL0.get(position);
holder.mSeason.setText(myL0List.getNvGDSeason());
holder.mStyle.setText(myL0List.getStyleBriefDescr().replace("<br/>", "\n"));
holder.mVendor.setText(myL0List.getNvPartnerVendor());
Util.Logd( "temp l0 size is "+templistQRl1Info.size());
if (!isFromClose){
templistQRl1Info.clear();
if (oldL0ClickPosition >=0){
for (int i = 0; i < listQRl1Info.size(); i++) {
if (listQRL0.get(oldL0ClickPosition).getId().equals(listQRl1Info.get(i).getId())) {
Util.Logd( "temp ids are "+listQRl1Info.get(i).getId());
templistQRl1Info.add(listQRl1Info.get(i));
}
}
}
mLstQRL1InfoAdapter = new LstQRL1InfoAdapter(context,templistQRl1Info, (ArrayList<LstQRTrackingSummary>) listQRTracSumm);
mLstQRL1InfoRecView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
mLstQRL1InfoRecView.setNestedScrollingEnabled(false);
mLstQRL1InfoRecView.setAdapter(mLstQRL1InfoAdapter);
mLstQRL1InfoAdapter.notifyDataSetChanged();
}
boolean isL0Expanded = myL0List.isL0Expanded();
Util.Logd("Expanded status is "+ position + " " + listQRL0.get(position).isL0Expanded());
if (isL0Expanded) {
holder.mLstQrL0ImageView.setImageResource(R.drawable.ic_arrow_drop_down);
holder.hideShowL0Layout.setVisibility(View.VISIBLE);
} else {
holder.mLstQrL0ImageView.setImageResource(R.drawable.ic_arrow_right);
holder.hideShowL0Layout.setVisibility(View.GONE);
}
holder.itemView.setOnClickListener(view -> {
Util.Logd("////////////////////////");
if (oldL0ClickPosition >= 0) {
if (listQRL0.get(oldL0ClickPosition).isL0Expanded()) {
listQRL0.get(oldL0ClickPosition).setL0Expanded(false);
holder.hideShowL0Layout.setVisibility(View.GONE);
Util.Logd("Id to close is "+listQRL0.get(oldL0ClickPosition).getId());
isFromClose = true ;
notifyItemChanged(oldL0ClickPosition);
}
}
if (oldL0ClickPosition == holder.getAdapterPosition()) {
oldL0ClickPosition = -1;
} else {
oldL0ClickPosition = holder.getAdapterPosition();
}
Util.Logd("New click is " + oldL0ClickPosition);
if (oldL0ClickPosition >= 0 && oldL0ClickPosition != -1) {
new Handler(Looper.myLooper()).postDelayed(() -> {
listQRL0.get(oldL0ClickPosition).setL0Expanded(!listQRL0.get(oldL0ClickPosition).isL0Expanded());
Util.Logd("new data modify is "+listQRL0.get(oldL0ClickPosition).getId());
isFromClose = false ;
notifyItemChanged(oldL0ClickPosition);
},500);
}
});
}
#Override
public int getItemCount() {
return listQRL0.size();
}
public class LstQRL0ViewHolder extends RecyclerView.ViewHolder {
private TextView mSeason, mStyle, mVendor;
private ImageView mLstQrL0ImageView;
private LinearLayout hideShowL0Layout;
public LstQRL0ViewHolder(#NonNull View itemView) {
super(itemView);
mSeason = itemView.findViewById(R.id.lstqrl0_season);
mStyle = itemView.findViewById(R.id.lstqrl0_style);
mVendor = itemView.findViewById(R.id.lstqrl0_vendor);
mLstQrL0ImageView = itemView.findViewById(R.id.lstqr10_arrow);
mLstQRL1InfoRecView = itemView.findViewById(R.id.lstqrl1info_rv);
hideShowL0Layout = itemView.findViewById(R.id.hideShowL0Layout);
}
}
}
There is one more nested adapter but thats also same , so am not posting that here.
If need any help then just post comment.
What im doing wrong here?
i tried to use function replace() on my setOnEditCommit, but not work.
clnVt.setCellValueFactory(new PropertyValueFactory<>("valorVt"));
clnVt.setEditable(true);
clnVt.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));
clnVt.setOnEditCommit(new EventHandler<CellEditEvent<GSTabela2, Double>>() {
#Override
public void handle(CellEditEvent<GSTabela2, Double> c) {
Double valor = c.getTableView().getItems().get(c.getTablePosition().getRow()).valorVtProperty().getValue();
String converte = valor.toString().replace(",", ".");
c.getTableView().getItems().get(c.getTablePosition().getRow()).setValorVt(Double.valueOf(converte));
}
});
Then i try to use a DoubleStringConverter implemention:
public class EstilizadoDoubleStringConverter extends DoubleStringConverter {
private final DoubleStringConverter conversor = new DoubleStringConverter();
#Override
public Double fromString(String value) {
try {
value.replace(",", ".");
return conversor.fromString(value);
} catch (NumberFormatException e) {
e.getStackTrace();
}
return -1.0;
}
#Override
public String toString(Double value) {
try {
return conversor.toString(value);
} catch (NumberFormatException e) {
e.getStackTrace();
}
return null;
}
}
If i use a the DoubleStringConverter a get -1.0;
So, my question is. How i can replace the comma to dot?
I fix with Slaw Hint.
In my implemention of DoubleStringConverter, i change the
private final DoubleStringConverter conversor = new DoubleStringConverter();
to
private final Locale local = Locale.getDefault(Category.FORMAT);
private final NumberStringConverter conversor = new NumberStringConverter(local);
so my whole implemention its now like this.
public class EstilizadoDoubleStringConverter extends DoubleStringConverter {
private final Locale local = Locale.getDefault(Category.FORMAT);
private final NumberStringConverter conversor = new NumberStringConverter(local);
#Override
public Double fromString(String value) {
try {
return conversor.fromString(value).doubleValue();
} catch (NumberFormatException e) {
e.getStackTrace();
}
return -1.0;
}
#Override
public String toString(Double value) {
try {
return conversor.toString(value);
} catch (NumberFormatException e) {
e.getStackTrace();
}
return null;
}
}
In a JavaFX TreeView I'm using 'custom' classes which extend TreeItem. This makes me able to edit the items in the TreeView (I can double click them and edit the contents when running the application) but I can't seem to be able to set the .setOnEditCommit() method properly. I was hoping it'd work similar as the function in a tableview but I didn't have any luck yet.
This is my code in my controller in which I try to set the setOnEditCommit() method. In my TreeView called 'trvDivisies' I display football team divisions / competitions and one level lower I display all the teams that are in a certain division.
private void setUpTreeView() {
trvDivisies.setEditable(true);
trvDivisies.setShowRoot(false);
TreeItem<String> root = new TreeItem<>();
for (Divisie d : divisies) {
TreeItem<String> divisieTreeItem = d;
divisieTreeItem.valueProperty().set(d.getNaam());
for (VoetbalTeam vt : d.getVoetbalTeams()) {
TreeItem<String> voetbalTeamTreeItem = vt;
voetbalTeamTreeItem.valueProperty().setValue(vt.getTeamNaam());
divisieTreeItem.getChildren().add(voetbalTeamTreeItem);
}
root.getChildren().add(divisieTreeItem);
}
trvDivisies.setRoot(root);
trvDivisies.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
#Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
System.out.println(newValue);
}
});
trvDivisies.setCellFactory(TextFieldTreeCell.forTreeView());
// I get an error at the following line when compiling
trvDivisies.setOnEditCommit((TreeView.EditEvent p) -> {
TreeItem<String> selectedItem = p.getTreeItem();
if (selectedItem instanceof Divisie) {
updateDivisie((Divisie)selectedItem);
} else if (selectedItem instanceof VoetbalTeam) {
updateTeam((VoetbalTeam)selectedItem);
}
});
}
This is what my 'custom' classes look like.
public class Divisie extends TreeItem<String> {
private static int idCount = 0;
private int id;
private String naam;
private List<VoetbalTeam> voetbalTeams;
public int getId() {
return id;
}
public String getNaam() {
return naam;
}
public List<VoetbalTeam> getVoetbalTeams() {
return voetbalTeams;
}
public Divisie(int id, String naam) {
super(naam);
this.id = id;
this.naam = naam;
}
public Divisie(String naam) {
this.id = ++idCount;
this.naam = naam;
}
public void addTeam(VoetbalTeam toBeAdded) {
if (voetbalTeams == null) {
voetbalTeams = new LinkedList<>();
}
voetbalTeams.add(toBeAdded);
}
#Override
public String toString() {
return this.naam;
}
}
Second 'lower level' class
public class VoetbalTeam extends TreeItem<String> {
private static int idCount = 0;
private int id;
private String teamNaam;
private List<Speler> spelers;
public int getId() {
return id;
}
public String getTeamNaam() {
return teamNaam;
}
public List<Speler> getSpelers() {
return this.spelers;
}
public VoetbalTeam(int id, String teamNaam) {
super(teamNaam);
this.id = id;
this.teamNaam = teamNaam;
}
public VoetbalTeam(String teamNaam) {
super(teamNaam);
this.id = ++idCount;
this.teamNaam = teamNaam;
}
public void addSpeler(Speler nieuweSpeler) {
if (spelers == null) {
spelers = new LinkedList<>();
}
this.spelers.add(nieuweSpeler);
}
#Override
public String toString() {
return this.teamNaam;
}
}
When trying to run the application WITH the .setOnEditCommit() method I get an error saying:
Error:(97, 37) java: incompatible types: incompatible parameter types in lambda expression
I was hoping you guys can tell me what I need to change my TreeView.EditEvent lambda to or help me find an easier solution.
For a TreeView<T>, the signature of setOnEditCommit is
void setOnEditCommit(EventHandler<TreeView.EditEvent<T>> value)
Since you have (apparently) a TreeView<String>, you need
trvDivisies.setOnEditCommit((TreeView.EditEvent<String> p) -> {
// ...
});
Or, of course, you can just let the compiler do the work for you:
trvDivisies.setOnEditCommit(p -> {
// ...
});
I have a listview with a getCount() of 7. I want all 7 items to be shown regardless if any data from my database is available to populate them. If no data is available then an item should just be blank with predetermined text.
When I have not hardcoded 7 database entries beforehand to go into the 7 views then I get an indexoutofbound exception when running the app due to the 7 items not being able to be populated accordingly. This happens in ListMealsAdapter.java when method Meal currentItem = getItem(position); is called and triggers public Meal getItem(int position).
I am looking for a condition statement that I can use for my listview/adapter that can handle an empty database so that the index does not go out of bounds. Also, is the BaseAdapter suited for what I want to do?
MainActivity.java
public class MainActivity extends BaseActivity {
public static final String TAG = "MainActivity";
private ListView mListviewMeals;
private MealDAO mMealDao;
private List<Meal> mListMeals;
private ListMealsAdapter mAdapter;
private SQLiteDatabase mDatabase;
DatabaseHelper mDbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activateToolbar(1);
// initialize views
initViews();
// fill the dailyListView
mMealDao = new MealDAO(this);
mListMeals = mMealDao.getAllMeals();
mAdapter = new ListMealsAdapter(this, mListMeals, MainActivity.this);
mListviewMeals.setAdapter(mAdapter);
}
private void initViews() {
this.mListviewMeals = (ListView) findViewById(R.id.view_daily_list);
}
ListMealsAdapter.java
public class ListMealsAdapter extends BaseAdapter {
public static final String TAG = "ListMealsAdapter";
Activity mActivity;
private List<Meal> mItems;
private LayoutInflater mInflater;
public ListMealsAdapter(Context context, List<Meal> listMeals, Activity activity) {
super();
mActivity = activity;
this.setItems(listMeals);
this.mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return 7;
}
#Override
public Meal getItem(int position) {
return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position) : null;
}
#Override
public long getItemId(int position) {
return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position).getId() : position;
}
#Override
public View getView(int position, final View convertView, final ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
if (v == null) {
v = mInflater.inflate(R.layout.list_item_daily, parent, false);
holder = new ViewHolder();
holder.txtDescription = (TextView) v.findViewById(R.id.txtBreakfast);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
// fill row data
Meal currentItem = getItem(position);
if (currentItem != null) {
holder.txtDescription.setText(currentItem.getDescription());
}
return v;
}
public List<Meal> getItems() {
return mItems;
}
public void setItems(List<Meal> mItems) {
this.mItems = mItems;
}
class ViewHolder {
TextView txtDescription;
}
}
Meal.java
public class Meal implements Serializable {
public static final String TAG = "Meal";
private static final long serialVersionUID = -7406082437623008161L;
private long mId;
private int mType;
private String mDescription;
public Meal() {
}
public Meal(int type, String description) {
this.mType = type;
this.mDescription = description;
}
public long getId() {
return mId;
}
public void setId(long mId) {
this.mId = mId;
}
public int getType() {
return mType;
}
public void setType(int mType) {
this.mType = mType;
}
public String getDescription() {
return mDescription;
}
public void setDescription(String mDescription) {
this.mDescription = mDescription;
}
}
MealDAO.java
public class MealDAO {
public static final String TAG = "MealDAO";
private SQLiteDatabase mDatabase;
private DatabaseHelper mDbHelper;
private Context mContext;
private String[] mAllColumns = { DatabaseHelper.COLUMN_MEAL_ID,
DatabaseHelper.COLUMN_MEAL_TYPE, DatabaseHelper.COLUMN_MEAL_DESCRIPTION};
public MealDAO(Context context) {
this.mContext = context;
mDbHelper = new DatabaseHelper(context);
// open the database
try {
open();
} catch (SQLException e) {
Log.e(TAG, "SQLException on opening database " + e.getMessage());
e.printStackTrace();
}
}
public void open() throws SQLException {
mDatabase = mDbHelper.getWritableDatabase();
}
public void close() {
mDbHelper.close();
}
public List<Meal> getAllMeals() {
List<Meal> listMeals = new ArrayList<Meal>();
Cursor query = mDatabase.rawQuery("SELECT * from meal", null);
if(query.moveToFirst()) {
do {
// Cycle through all records
Meal meal = cursorToMeal(query);
listMeals.add(meal);
} while(query.moveToNext());
}
return listMeals;
}
public Meal getMealById(long id) {
Cursor cursor = mDatabase.query(DatabaseHelper.TABLE_MEALS, mAllColumns,
DatabaseHelper.COLUMN_MEAL_ID + " = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
Meal meal = cursorToMeal(cursor);
return meal;
}
protected Meal cursorToMeal(Cursor cursor) {
Meal meal = new Meal();
meal.setId(cursor.getLong(0));
meal.setType(cursor.getInt(1));
meal.setDescription(cursor.getString(2));
return meal;
}
}
After a LOT of trial and error I finally found an acceptable solution to my problem. What I did was to add a default row to my database for the view items that I wanted to have a predetermined database entry when no data had been entered beforehand.
I then made sure to start at index 2, making sure that index 1 would be reserved for my default value. If the index comes out of bounds then the exception is caught and the default database entry will be added to the array.
public Meal getItem(int position) {
Meal result;
try {
result = (getItems() != null && !getItems().isEmpty()) ? getItems().get(position) : null;
} catch (Exception e) {
Meal default = getItem(0);
return default;
}
return result;
}
Meal currentItem = getItem(position + 1);
if (currentItem != null) {
holder.txtDescription.setText(currentItem.getDescription());
}
With that change things have been running smooth ever since. I hope this can help someone else as well.
my project is todo
i have 4 tab , 4 fragment with 4 list(Actionbar navigation contain 4 tab and ViewPager).
4 list(tab) use same db table but each of them retrieve different data with categoryID.
i use a Asynctask for all of them ,to read data and set adapter to list.
public class AsyncTaskDB extends AsyncTask<Void, Void, listAdapter> {
Context act;
int Categoryid;
ArrayList<memo> arraymemo;
listAdapter myadapter;
ListView list;
listAdapter listAdp;
public AsyncTaskDB(Context acti, int categoryID) {
this.act = acti;
this.Categoryid = categoryID;
}
#Override
protected listAdapter doInBackground(Void... params) {
MemoDBHelper helper = new MemoDBHelper(act);
// getAllDataByCategoryID
if (Categoryid != CategoryID.Done_ID)
arraymemo = helper.getAllTaskByCategory(Categoryid);
else
arraymemo = (ArrayList<memo>) helper.gatDoneMemo();
myadapter = new listAdapter(act, arraymemo);
if (myadapter == null) {
Toast.makeText(act, "no data", Toast.LENGTH_SHORT).show();
cancel(true);
}
return myadapter;
}
#Override
protected void onPostExecute(listAdapter result) {
switch (Categoryid) {
case CategoryID.Urgent_Imprtant_ID:
list = (ListView) ((Activity) act)
.findViewById(R.id.Urgent_Important_list);
break;
case CategoryID.Urgent_Less_Imprtant_ID:
list = (ListView) ((Activity) act)
.findViewById(R.id.Urgent_Less_Important_list);
break;
case CategoryID.Less_Urgent_Imprtant_ID:
list = (ListView) ((Activity) act)
.findViewById(R.id.Less_Urgent_Imprtant_list);
break;
case CategoryID.Neither_Urgent_Or_Imprtant_ID:
list = (ListView) ((Activity) act)
.findViewById(R.id.Neither_Urgent_Imprtant_list);
break;
case CategoryID.Done_ID:
list = (ListView) ((Activity) act).findViewById(R.id.ArchiveList);
break;
}
list.setAdapter(result);
this.listAdp = result;
}
public listAdapter getlistAdapter() {
return this.listAdp;
}
}
each memo in list have Done CheckBox.when user check and uncheck it,automatically memo update in db.(in custom adapter)
----------------------------
| -- |
| | | memotitle |
| -- |
----------------------------
public class listAdapter extends BaseAdapter implements OnCheckedChangeListener { Context act;
ArrayList<memo> MemoArray;
SparseBooleanArray mcheck;
int pos;
MemoDBHelper helper;
public listAdapter(Context activity, ArrayList<memo> memoarray) {
this.act = activity;
this.MemoArray = memoarray;
mcheck = new SparseBooleanArray(memoarray.size());
helper = new MemoDBHelper(act);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return MemoArray.size();
}
#Override
public memo getItem(int position) {
// TODO Auto-generated method stub
return MemoArray.get(position);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public class viewHolder {
TextView title;
// TextView description;
CheckBox chkstatus;
}
viewHolder it;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
pos = position;
LayoutInflater in = ((Activity) act).getLayoutInflater();
if (convertView == null) {
convertView = in.inflate(R.layout.list_item, null);
it = new viewHolder();
it.title = (TextView) convertView.findViewById(R.id.txt_list_title);
it.chkstatus = (CheckBox) convertView
.findViewById(R.id.chkStatusid);
convertView.setTag(it);
} else {
it = (viewHolder) convertView.getTag();
}
it.title.setText(MemoArray.get(position).GetTitle());
it.chkstatus.setChecked(MemoArray.get(position).GetSattus());
it.chkstatus.setOnCheckedChangeListener(this);
it.chkstatus.setTag(String.valueOf(MemoArray.get(position).GetID()));
return convertView;
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mcheck.put(Integer.valueOf((String) buttonView.getTag()), isChecked);
helper.updateStatusByID(Integer.valueOf((String) buttonView.getTag()),
(isChecked));
helper.close();
//after db updatedt ,call method in fragment to notifydatsetchanged!
UrgentImportant_frg.notifyAdapter();
}
}
adapter must notify data changed ,and list don't show done memo.i don't how to do it !
my first fragment :
public class UrgentImportant_frg extends Fragment {
static listAdapter myadp;
ListView list;
// memo selectedmemo;
long[] checkid;
AsyncTaskDB asyn;
ArrayList<memo> selectedMemoArray;
final static int RQS_MoveTo = 10;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.urgentimportant_layout,
container, false);
return rootview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
selectedMemoArray = new ArrayList<memo>();
list.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.list_select_menu, menu);
/*
* MenuInflater inflater = getActivity().getMenuInflater();
* inflater.inflate(R.menu.list_select_menu, menu);
*/
mode.setTitle("Select Items");
return true;
}
#Override
public boolean onActionItemClicked(final ActionMode mode,
MenuItem item) {
switch (item.getItemId()) {
case R.id.deletemenu:
final int[] myitemsid = getSelectedID();
final MemoDBHelper helper = new MemoDBHelper(getActivity());
AlertDialog.Builder myAlert = new AlertDialog.Builder(
getActivity());
myAlert.setMessage(
"Are you sure to delete " + myitemsid.length
+ " memo ?")
.setPositiveButton("yes", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
for (int j = 0; j < myitemsid.length; j++) {
helper.deleteRow(myitemsid[j]);
/*
* if (j == myitemsid.length - 1) {
* strid[j] = String
* .valueOf(myitemsid[j]); } else {
* strid[j] = String
* .valueOf(myitemsid[j]) + ","; }
*/
}
mode.finish();
onResume();
}
}).setNegativeButton("no", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
mode.finish();
}
});
AlertDialog alert = myAlert.create();
alert.show();
// mode.finish();
break;
case R.id.MoveTomenu:
// myadp.getItem(position);
Intent i = new Intent(getActivity(),
MoveToCategory_act.class);
i.putExtra("categoryid", CategoryID.Urgent_Imprtant_ID);
startActivityForResult(i, RQS_MoveTo);
mode.finish();
break;
}
return true;
}
// get selected id to delete and move category
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
if (myadp == null) {
myadp = asyn.getlistAdapter();
}
int p = ifMemoSelectedBefore(myadp.getItem(position));
if (p != -1) {
selectedMemoArray.remove(p);
} else if (checked) {
selectedMemoArray.add(myadp.getItem(position));
}
final int checkedCount = list.getCheckedItemCount();
switch (checkedCount) {
case 0:
mode.setSubtitle(null);
break;
case 1:
mode.setSubtitle("One Item Selected");
break;
default:
mode.setSubtitle(checkedCount + " Item Selected");
break;
}
}
});
getActivity().getActionBar().setSubtitle("subtitle");
}
public int ifMemoSelectedBefore(memo m) {
return selectedMemoArray.indexOf(m);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
list = (ListView) view.findViewById(R.id.Urgent_Important_list);
// -------------click item
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
if (myadp == null) {
myadp = asyn.getlistAdapter();
}
// Log.d("tag", myadp.getItem(position).GetTitle() + "");
Intent nextintent = new Intent(getActivity(),
EditMemo_act.class);
memo g = myadp.getItem(position);
/*
* MemoDBHelper helper = new MemoDBHelper(getActivity());
* helper.updateStatusByID(g.GetID(), true);
*/
nextintent.putExtra("editmemo", g);
startActivity(nextintent);
}
});
}
#Override
public void onResume() {
asyn = new AsyncTaskDB(getActivity(), CategoryID.Urgent_Imprtant_ID);
asyn.execute();
super.onResume();
}
public int[] getSelectedID() {
int[] SelectedArray_ID = new int[selectedMemoArray.size()];
for (int j = 0; j < selectedMemoArray.size(); j++) {
SelectedArray_ID[j] = selectedMemoArray.get(j).GetID();
// Log.d("id", selectedMemoArray.get(j).GetID() + "");
}
return SelectedArray_ID;
}
//-------------a method to notifymyadpter
public static void notifyAdapter() {
if (myadp != null) {
myadp.notifyDataSetChanged();
Log.d("notify", "here");
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RQS_MoveTo) {
if (resultCode == Result.RESULT_OK) {
int id = data.getExtras().getInt("NEWCategoryID");
MemoDBHelper helper = new MemoDBHelper(getActivity());
final int[] myitemsid = getSelectedID();
for (int j = 0; j < myitemsid.length; j++) {
helper.updateCategory(myitemsid[j], id);
}
onResume();
}
}
}
}
is there any method in fragment to run after adapter changed?or in myadapter ,after db updated i call a method in fragment to notify data changed ? i think the second solution isn't right >_<
p.s:notifyAdapter() doesn't work,is it because my adapter fill in asyntask ?
When the adapter changed, try to call below method.
Once you use FragmentPagerAdapter or ListView, when you change the data, you should call this.
notifyDataSetChanged();
read about it : notifyDataSetChanged