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

product binding

parent 251fb334
...@@ -33,13 +33,14 @@ import com.yono.messeripos.response.DataResponse; ...@@ -33,13 +33,14 @@ import com.yono.messeripos.response.DataResponse;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
MaterialToolbar toolbar; MaterialToolbar toolbar;
MainViewModels mainViewModels; MainViewModels mainViewModels;
ActivityMainBinding binding; ActivityMainBinding binding;
CategoryAdapter categoryAdapter; CategoryAdapter categoryAdapter;
ProductAdapter productAdapter;
@Override @Override
...@@ -52,10 +53,10 @@ public class MainActivity extends AppCompatActivity { ...@@ -52,10 +53,10 @@ public class MainActivity extends AppCompatActivity {
// setContentView(R.layout.activity_main); // setContentView(R.layout.activity_main);
categoryAdapter = new CategoryAdapter(); categoryAdapter = new CategoryAdapter();
productAdapter = new ProductAdapter();
binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow(); Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
...@@ -81,7 +82,7 @@ public class MainActivity extends AppCompatActivity { ...@@ -81,7 +82,7 @@ public class MainActivity extends AppCompatActivity {
mainViewModels.getCategory().observe(this, new Observer<DataResponse<List<CategoryModels>>>() { mainViewModels.getCategory().observe(this, new Observer<DataResponse<List<CategoryModels>>>() {
@Override @Override
public void onChanged(DataResponse<List<CategoryModels>> listDataResponse) { public void onChanged(DataResponse<List<CategoryModels>> listDataResponse) {
if (listDataResponse != null){ if (listDataResponse != null) {
List<CategoryModels> categoryModels = listDataResponse.getData(); List<CategoryModels> categoryModels = listDataResponse.getData();
ArrayList<CategoryModels> categoryModelsArrayList = new ArrayList<>(); ArrayList<CategoryModels> categoryModelsArrayList = new ArrayList<>();
...@@ -93,10 +94,31 @@ public class MainActivity extends AppCompatActivity { ...@@ -93,10 +94,31 @@ public class MainActivity extends AppCompatActivity {
} }
String js = new Gson().toJson(listDataResponse); 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; ...@@ -10,50 +10,41 @@ import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.yono.messeripos.R; import com.yono.messeripos.R;
import com.yono.messeripos.databinding.ItemCategoryBinding;
import com.yono.messeripos.databinding.ItemListBinding; import com.yono.messeripos.databinding.ItemListBinding;
import com.yono.messeripos.models.CategoryModels;
import com.yono.messeripos.models.ProductModels; import com.yono.messeripos.models.ProductModels;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder> { public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder> {
private List<ProductModels> productModels; private ArrayList<ProductModels> productModels;
Context context; 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(); notifyDataSetChanged();
} }
@NonNull @NonNull
@Override @Override
public ProductAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public ProductAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyViewHolder( ItemListBinding itemListBinding = DataBindingUtil.inflate(
DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()), LayoutInflater.from(parent.getContext()),
R.layout.item_list, R.layout.item_list,
parent, false parent,
) false
); );
return new MyViewHolder(itemListBinding);
} }
@Override @Override
public void onBindViewHolder(@NonNull ProductAdapter.MyViewHolder holder, int position) { public void onBindViewHolder(@NonNull ProductAdapter.MyViewHolder holder, int position) {
holder.bindData(productModels.get(position), listener); holder.bindData(productModels.get(position));
} }
@Override @Override
...@@ -64,15 +55,14 @@ public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHo ...@@ -64,15 +55,14 @@ public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHo
public class MyViewHolder extends RecyclerView.ViewHolder { public class MyViewHolder extends RecyclerView.ViewHolder {
private ItemListBinding binding; private ItemListBinding binding;
public MyViewHolder(@NonNull ItemListBinding binding) { public MyViewHolder(@NonNull ItemListBinding binding) {
super(binding.getRoot()); super(binding.getRoot());
this.binding = binding; this.binding = binding;
} }
public void bindData(ProductModels productModels, ProductListener listener) { public void bindData(ProductModels products) {
binding.setProduct(productModels); binding.setProducts(products);
} }
} }
} }
...@@ -3,9 +3,6 @@ ...@@ -3,9 +3,6 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<data> <data>
<variable <variable
name="mainView" name="mainView"
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<data> <data>
<variable <variable
name="product" name="products"
type="com.yono.messeripos.models.ProductModels" /> type="com.yono.messeripos.models.ProductModels" />
</data> </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