Commit 20daad65 authored by Alfansyah Fadlian's avatar Alfansyah Fadlian

product binding

parent 251fb334
......@@ -33,13 +33,14 @@ import com.yono.messeripos.response.DataResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
MaterialToolbar toolbar;
MainViewModels mainViewModels;
ActivityMainBinding binding;
CategoryAdapter categoryAdapter;
ProductAdapter productAdapter;
@Override
......@@ -52,10 +53,10 @@ public class MainActivity extends AppCompatActivity {
// setContentView(R.layout.activity_main);
categoryAdapter = new CategoryAdapter();
productAdapter = new ProductAdapter();
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
......@@ -81,7 +82,7 @@ public class MainActivity extends AppCompatActivity {
mainViewModels.getCategory().observe(this, new Observer<DataResponse<List<CategoryModels>>>() {
@Override
public void onChanged(DataResponse<List<CategoryModels>> listDataResponse) {
if (listDataResponse != null){
if (listDataResponse != null) {
List<CategoryModels> categoryModels = listDataResponse.getData();
ArrayList<CategoryModels> categoryModelsArrayList = new ArrayList<>();
......@@ -93,10 +94,31 @@ public class MainActivity extends AppCompatActivity {
}
String js = new Gson().toJson(listDataResponse);
Log.d("Get data from category", "Response "+js);
Log.d("Get data from category", "Response " + js);
}
});
mainViewModels = ViewModelProviders.of(this).get(MainViewModels.class);
mainViewModels.getProduct().observe(this, new Observer<DataResponse<List<ProductModels<CategoryModels>>>>() {
@Override
public void onChanged(DataResponse<List<ProductModels<CategoryModels>>> listDataResponse) {
if (listDataResponse != null) {
List<ProductModels<CategoryModels>> productModels = listDataResponse.getData();
ArrayList<ProductModels> productModelsArrayList = new ArrayList<>();
productModelsArrayList.addAll(productModels);
productAdapter.setProduct(MainActivity.this,productModelsArrayList );
binding.rvCategory.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
binding.rvCategory.setAdapter(categoryAdapter);
}
String js = new Gson().toJson(listDataResponse);
Log.d("Get data from products", "Response " + js);
}
});
}
......
......@@ -10,50 +10,41 @@ import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import com.yono.messeripos.R;
import com.yono.messeripos.databinding.ItemCategoryBinding;
import com.yono.messeripos.databinding.ItemListBinding;
import com.yono.messeripos.models.CategoryModels;
import com.yono.messeripos.models.ProductModels;
import java.util.ArrayList;
import java.util.List;
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder> {
private List<ProductModels> productModels;
private ArrayList<ProductModels> productModels;
Context context;
public interface ProductListener {
void onUpdate(ProductModels product);
void onDelete(ProductModels product);
}
private ProductListener listener;
public void setListener(ProductListener listener) {
this.listener = listener;
}
public void setProducts(List<ProductModels> product) {
this.productModels = product;
public void setProduct(Context context, ArrayList<ProductModels> productModels){
this.productModels = productModels;
this.context = context;
notifyDataSetChanged();
}
@NonNull
@Override
public ProductAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(
DataBindingUtil.inflate(
ItemListBinding itemListBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()),
R.layout.item_list,
parent, false
)
parent,
false
);
return new MyViewHolder(itemListBinding);
}
@Override
public void onBindViewHolder(@NonNull ProductAdapter.MyViewHolder holder, int position) {
holder.bindData(productModels.get(position), listener);
holder.bindData(productModels.get(position));
}
@Override
......@@ -64,15 +55,14 @@ public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHo
public class MyViewHolder extends RecyclerView.ViewHolder {
private ItemListBinding binding;
public MyViewHolder(@NonNull ItemListBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bindData(ProductModels productModels, ProductListener listener) {
binding.setProduct(productModels);
public void bindData(ProductModels products) {
binding.setProducts(products);
}
}
}
......@@ -3,9 +3,6 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="mainView"
......
......@@ -5,7 +5,7 @@
<data>
<variable
name="product"
name="products"
type="com.yono.messeripos.models.ProductModels" />
</data>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment