using Android studio, I cannot find anything of my customized gridview in my design view.
Android Studio Arctic Fox | 2020.3.1 Patch 3
Build #AI-203.7717.56.2031.7784292, built on October 1, 2021
Runtime version: 11.0.10+0-b96-7249189 amd64
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 8
Registry: external.system.auto.import.disabled=true, debugger.watches.in.variables=false, compiler.automake.allow.when.app.running=true, ide.balloon.shadow.size=0
First of all, one of my customized gridview which can be seen is showing below:
public class PickWaveListGridVIew extends GridView {
public PickWaveListGridVIew(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
private void initialize() {
List<WaveListItemDTO> dataList = requestForWaveList();
setAdapter(new PickWaveListAdapter(dataList, getContext()));
}
private List<WaveListItemDTO> requestForWaveList() {
//todo demo data
return Lists.newArrayList(new WaveListItemDTO("S202205250001", "S202205250001", "2022-05-25 10:42:01"),
new WaveListItemDTO("S202205250002", "S202205250002", "2022-05-25 10:42:02"),
new WaveListItemDTO("S202205250003", "S202205250003", "2022-05-25 10:42:03")
);
}
/**
* dto class of demo
*/
private static class WaveListItemDTO {
private String waveId;
private String waveCode;
private String createTime;
public WaveListItemDTO() {
}
public WaveListItemDTO(String waveId, String waveCode, String createTime) {
this.waveId = waveId;
this.waveCode = waveCode;
this.createTime = createTime;
}
}
private class PickWaveListAdapter extends BaseAdapter {
private final List<WaveListItemDTO> dataList;
private LayoutInflater menuInflater;
public PickWaveListAdapter(List<WaveListItemDTO> dataList, Context context) {
this.dataList = dataList;
this.menuInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return this.dataList.size();
}
#Override
public Object getItem(int position) {
return this.dataList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//load layout
View layout = this.menuInflater.inflate(R.layout.item_clickable_pick_wave_list_button, parent, false);
TextView waveId = layout.findViewById(R.id.item_clickable_pick_wave_list_button_wave_id);
TextView datetime = layout.findViewById(R.id.item_clickable_pick_wave_list_button_datetime);
WaveListItemDTO dto = this.dataList.get(position);
//set data to views
waveId.setText(dto.waveCode == null ? dto.waveId : dto.waveCode);
datetime.setText(dto.createTime);
return layout;
}
private <T extends BaseFunctionPageActivity> void openDetailOperationActivity(
BaseActivity currentActivity,
Class<T> clz,
String title,
String waveId) {
ActivityUtils.openNewActivity(currentActivity,
clz,
true,
null,
new JSONObjectBuilder()
.put(Constants.INTENT_PASSING_DATA_JSON_TITLE_KEY, title)
.put(Constants.INTENT_PASSING_DATA_JSON_WAVE_ID_KEY, waveId)
.build()
);
}
}
}
customized gridview R.layout.item_clickable_pick_wave_list_button
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="90dp"
android:background="#drawable/item_text_view_underline">
<!--拣货波次列表Item-->
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_wave_list_button_splitter_mid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_percent="0.45"
android:orientation="vertical" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_wave_list_button_splitter_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_percent="0.03"
android:orientation="vertical" />
<TextView
android:id="#+id/item_clickable_pick_wave_list_button_wave_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="S202205250001"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/item_clickable_pick_wave_list_button_splitter_left"
app:layout_constraintRight_toLeftOf="#+id/item_clickable_pick_wave_list_button_splitter_mid" />
<ImageButton
android:id="#+id/item_clickable_pick_wave_list_button_img_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="10dp"
android:src="#drawable/right"
android:background="#color/transparent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="#+id/item_clickable_pick_wave_list_button_datetime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2022-05-25 10:42:01"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/item_clickable_pick_wave_list_button_splitter_mid" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_mws_pick_wave_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.originlinks.wmspda.activities.wms.pick.PickWaveListGridVIew
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="#+id/activity_mws_pick_wave_list_layout"
app:layout_constraintBottom_toBottomOf="#+id/activity_mws_pick_wave_list_layout"
android:numColumns="1"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
it is normal to see my demo data in design view like this.
So I decided to develop another customized gridview in exactly the same way, I found it failed to show in design view.
Here below is the code which has problems:
my customize gridview
public class ProductListGridView extends GridView {
private List<ProductListDTO> productList;
private List<String> itemIdList;
public ProductListGridView(Context context) {
super(context);
initialize(context);
}
public ProductListGridView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public ProductListGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
public ProductListGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context);
}
private void initialize(Context context) {
this.productList = getProductListOfSpecifiedWaveId();
this.itemIdList = this.productList.stream().map(e -> e.shelveCode + e.sku).collect(Collectors.toList());
setAdapter(new ProductItemAdapter(productList, getContext()));
}
private List<ProductListDTO> getProductListOfSpecifiedWaveId() {
//todo demo data
return Lists.newArrayList(
new ProductListDTO("F2-A01-001", "020111", "demo product name", 20, null),
new ProductListDTO("F2-A01-002", "020112", "demo product name", 20, null),
new ProductListDTO("F2-A01-003", "020113", "demo product name", 20, null),
new ProductListDTO("F2-A01-004", "020114", "demo product name", 20, null),
new ProductListDTO("F2-A01-005", "020115", "demo product nameK", 20, null)
);
}
private static class ProductListDTO {
private String shelveCode;
private String sku;
private String name;
private Integer number2Pick;
private Integer numberPicked;
private String imgUrl;
public ProductListDTO() {
}
public ProductListDTO(String shelveCode, String sku, String name, Integer number2Pick, String imgUrl) {
this.shelveCode = shelveCode;
this.sku = sku;
this.name = name;
this.number2Pick = number2Pick;
this.imgUrl = imgUrl;
this.numberPicked = 0;
}
}
private static class ProductItemAdapter extends BaseAdapter {
private final List<ProductListDTO> productList;
private final LayoutInflater menuInflater;
private final Context context;
public ProductItemAdapter(List<ProductListDTO> productList, Context context) {
this.productList = productList;
this.menuInflater = LayoutInflater.from(context);
this.context = context;
}
#Override
public int getCount() {
return this.productList.size();
}
#Override
public Object getItem(int position) {
return this.productList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//inflate the gridview and get the needed controllers
View layout = this.menuInflater.inflate(R.layout.item_clickable_pick_product_list_button, parent, false);
TextView shelveCode = layout.findViewById(R.id.item_clickable_pick_product_list_button_shelve_code);
TextView sku = layout.findViewById(R.id.item_clickable_pick_product_list_button_sku);
TextView name = layout.findViewById(R.id.item_clickable_pick_product_list_button_name);
TextView processNum = layout.findViewById(R.id.item_clickable_pick_product_list_button_process);
ImageView img = layout.findViewById(R.id.item_clickable_pick_product_list_button_pic);
//set the controllers' value by specified dto data
ProductListDTO dto = this.productList.get(position);
shelveCode.setText(dto.shelveCode);
sku.setText(dto.sku);
name.setText(dto.name);
String initialPickNum = dto.numberPicked.toString() + "/" + dto.number2Pick.toString();
processNum.setText(initialPickNum);
//async download images from url
ImageLoader imageLoader = new ImageLoader(this.context);
imageLoader.disPlayImage(dto.imgUrl, img);
return layout;
}
}
}
customized gridview R.layout.item_clickable_pick_product_list_button
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/item_text_view_underline">
<!--拣货商品明细列表的图文按钮组件-->
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_product_list_button_splitter_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_begin="10dp"
android:orientation="vertical" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_product_list_button_splitter_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_end="10dp"
android:orientation="vertical" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_product_list_button_splitter_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_begin="10dp"
android:orientation="horizontal" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_product_list_button_splitter_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_end="10dp"
android:orientation="horizontal" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_product_list_button_splitter_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_begin="150dp"
android:orientation="vertical" />
<ImageView
android:id="#+id/item_clickable_pick_product_list_button_pic"
android:layout_width="130dp"
android:layout_height="130dp"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_splitter_top"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_splitter_left"
app:layout_constraintBottom_toTopOf="#id/item_clickable_pick_product_list_button_splitter_bottom"
android:scaleType="centerInside"
android:src="#drawable/empty_pic"
android:background="#drawable/item_text_view_surround_border" />
<!--文本框标题-->
<TextView
android:id="#+id/item_clickable_pick_product_list_button_shelve_code_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_splitter_top"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_splitter_pic"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_shelve_code"
android:textColor="#color/black"
android:textSize="16sp"
android:text="库位号:" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_sku_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_shelve_code_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_splitter_pic"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_sku"
android:layout_marginTop="8dp"
android:textColor="#color/black"
android:textSize="16sp"
android:text="ERP-SKU:" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_name_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_sku_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_splitter_pic"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_name"
android:layout_marginTop="8dp"
android:textColor="#color/black"
android:textSize="16sp"
android:lines="2"
android:ellipsize="end"
android:text="名称:" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_process_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_name_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_splitter_pic"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_process"
android:layout_marginTop="8dp"
android:textColor="#color/red"
android:textSize="16sp"
android:text="应拣/已拣:" />
<!--具体内容-->
<TextView
android:id="#+id/item_clickable_pick_product_list_button_shelve_code"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_splitter_top"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_shelve_code_title"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_splitter_right"
android:textColor="#color/black"
android:textSize="16sp"
android:text="F2-A01-001" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_sku"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_shelve_code_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_sku_title"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_splitter_right"
android:layout_marginTop="8dp"
android:textColor="#color/black"
android:textSize="16sp"
android:lines="1"
android:ellipsize="marquee"
android:text="05-06-001-020111" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_sku_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_name_title"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_splitter_right"
android:layout_marginTop="8dp"
android:textColor="#color/black"
android:textSize="16sp"
android:lines="2"
android:ellipsize="end"
android:text="这是一个两行的名称挺长的1231231231234511111111" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_process"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_name_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_process_title"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_splitter_right"
android:layout_marginTop="8dp"
android:textColor="#color/red"
android:textSize="16sp"
android:text="0/20" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_wms_multi_pick_dashboard_layout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.originlinks.wmspda.activities.wms.pick.ProductListGridView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="#+id/activity_wms_multi_pick_dashboard_layout"
app:layout_constraintBottom_toBottomOf="#+id/activity_wms_multi_pick_dashboard_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity
public class DemoActivity extends ComponentActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityWmsMultiPickDashboardBinding binding = ActivityWmsMultiPickDashboardBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
}
}
I can see nothing showing in the design view but when I open my virtual device, the grid view is correcty showed.
How can I make the gridview visible in my design view? Is there any thing wrong with my code?
Related
I ran into a problem and still don't know how to get out. I made a listview with a custom adapter for it. I also implemented a searchview which will update the listview everytime I type in a character. But the problem is that the listview is not showing the correct results. I put some checkpoints in getView method of the custom adapter to check if the values which will be displayed are right or not and they're all right.
I have two items, let's call them HD03 and HD02 as you can see their name on the top left corner of each item.
when I type in the search bar 02, the result must be item HD02 but I got item HD03 instead. Although, the value I checked in the getView method using debugger was HD02
I think I made a silly mistake somewhere but I can't find it. Thank you for your time !
Here're my fragment and my adapter code.
Fragment
public class DashboardFragment extends Fragment {
private DashboardViewModel dashboardViewModel;
private ListView listViewContract;
private SearchView contractSearchView;
private ContractAdapter productListAdapter;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
dashboardViewModel =
new ViewModelProvider(this).get(DashboardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
initView(root);
return root;
}
private void initView(View view) {
listViewContract = view.findViewById(R.id.contract_list);
listViewContract.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
listViewProduct_onItemClick(adapterView, view, i, l);
}
});
loadData();
contractSearchView = view.findViewById(R.id.search_bar);
contractSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
DashboardFragment.this.productListAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
DashboardFragment.this.productListAdapter.getFilter().filter(newText);
return false;
}
});
}
private void loadData() {
List<Contract> products = new ArrayList<Contract>();
products.add(new Contract("p03", "Name 1", "4", "Mới", "HD03" ));
products.add(new Contract("p02", "Name 2", "5", "Mới", "HD02"));
productListAdapter = new ContractAdapter(getContext(), products);
listViewContract.setAdapter(productListAdapter);
}
private void listViewProduct_onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Product product = (Product) adapterView.getItemAtPosition(i);
// Toast.makeText(getApplicationContext(), product.getName(), Toast.LENGTH_LONG).show();
}
}
Adapter
public class ContractAdapter extends BaseAdapter implements Filterable {
private Context context;
private List<Contract> products;
private List<Contract> filteredData;
private ItemFilter itemFilter = new ItemFilter();
public ContractAdapter(Context context, List<Contract> products){
this.context = context;
this.products = products;
this.filteredData = products;
}
#NonNull
#Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder;
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.phan_anh_cardview, parent, false);
viewHolder.textViewContractName = view.findViewById(R.id.ten_hd);
viewHolder.textViewCustomerName = view.findViewById(R.id.ten_dt);
viewHolder.textViewEmployeeName = view.findViewById(R.id.ten_nv);
viewHolder.textViewId = view.findViewById(R.id.ma_hd);
viewHolder.buttonStatus = view.findViewById(R.id.report_status);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
Contract product = filteredData.get(position);
viewHolder.textViewContractName.setText(product.getName());
viewHolder.textViewEmployeeName.setText(product.getNhanvien());
viewHolder.textViewCustomerName.setText(product.getCustomer());
viewHolder.textViewId.setText(product.get_id());
viewHolder.buttonStatus.setText(product.getStatus());
Log.i("view", viewHolder.textViewId.getText().toString());
return view;
}
public int getCount(){
return filteredData.size();
}
public Contract getItem(int position) {
return filteredData.get(position);
}
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public Filter getFilter() {
return itemFilter;
// return super.getFilter();
}
private static class ViewHolder {
public static TextView textViewContractName;
public static TextView textViewCustomerName;
public static TextView textViewEmployeeName;
public static TextView textViewId;
public static Button buttonStatus;
}
private class ItemFilter extends Filter{
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String query = constraint.toString();
FilterResults results = new FilterResults();
List<Contract> originalData = products;
int count = originalData.size();
List<Contract> nList = new ArrayList<>();
for (Contract contract : originalData) {
if(contract.get_id().contains(query) || contract.getName().contains(query) || contract.getCustomer().contains(query)){
nList.add(contract);
}
}
results.values = nList;
results.count = nList.size();
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (List<Contract>) results.values;
notifyDataSetChanged();
}
}
}
fragment_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.dashboard.DashboardFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/tag_spinner_constraint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<SearchView
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:iconifiedByDefault="false"
app:layout_constraintEnd_toEndOf="#id/tag_spinner_constraint"
app:layout_constraintStart_toStartOf="#id/tag_spinner_constraint"
app:layout_constraintTop_toTopOf="#id/tag_spinner_constraint" />
<HorizontalScrollView
android:id="#+id/filter_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_bar">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/empty_rooms_btn"
android:layout_width="99dp"
android:layout_height="34dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:background="#drawable/rounded_button"
android:text="Phòng trống"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/floor_spinner" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/not_full_rooms_btn"
android:layout_width="wrap_content"
android:layout_height="34dp"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="4dp"
android:background="#drawable/rounded_button"
android:text="Phòng ghép"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="#+id/empty_rooms_btn"
app:layout_constraintTop_toBottomOf="#+id/floor_spinner" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/full_rooms_btn"
android:layout_width="105dp"
android:layout_height="34dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:background="#drawable/rounded_button"
android:text="Phòng đã đầy"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/not_full_rooms_btn"
app:layout_constraintTop_toBottomOf="#+id/floor_spinner" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/all_rooms_btn"
android:layout_width="wrap_content"
android:layout_height="34dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:background="#drawable/rounded_button"
android:text="Tất cả"
android:textSize="12sp" />
</LinearLayout>
</HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
<ListView
android:id="#+id/contract_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tag_spinner_constraint">
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
phan_anh_cardview.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="8dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="162dp"
android:layout_marginTop="5dp"
android:padding="6dp">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/report_status"
android:layout_width="68dp"
android:layout_height="15dp"
android:background="#drawable/rounded_button"
android:text="Mới"
android:textSize="10sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ma_hd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ten_hd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ma_hd" />
<TextView
android:id="#+id/ten_dt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ten_hd" />
<TextView
android:id="#+id/ten_nv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ten_dt" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
rounded_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/cardview_dark_background" />
<corners android:bottomRightRadius="30dp"
android:bottomLeftRadius="30dp"
android:topRightRadius="30dp"
android:topLeftRadius="30dp"/>
</shape>
I found error in your code.
You shouldn't use static members in ViewHolder.
So please replace ViewHolder code as following.
ContractAdapter.java
..............
private static class ViewHolder {
public TextView textViewContractName;
public TextView textViewCustomerName;
public TextView textViewEmployeeName;
public TextView textViewId;
public Button buttonStatus;
}
...............
If so, it will be working.
I am having problem with "GridView".If I use 6 items then it is not creating problem while scrolling but as I increase no. of items its behavior is unpredictable.
Please check the following video link for better understanding my problem:
GridView Issues video link
I am attaching my codes here:
activity_achievements.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:background="#color/green_color">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/back_iv"
android:text="Acheivements"
android:textColor="#color/white_color"
android:textSize="24sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/back_iv"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/back" />
<TextView
android:id="#+id/total_achievements_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="0/15"
android:textColor="#color/white_color"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
<GridView
android:id="#+id/acheivements_gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" />
</RelativeLayout>
achievements_row.xml
<?xml version="1.0" encoding="utf-8"?>
<com.xyz.quitsmoking.view.AchievementsView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<android.support.v7.widget.CardView
android:id="#+id/card_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/card_view1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
card_view:cardCornerRadius="2dp"
android:background="#color/white_color"
card_view:contentPadding="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_iv"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:src="#drawable/achievement" />
<TextView
android:id="#+id/title_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:padding="10dp"
android:text="Himalyi"
android:textColor="#color/green_color" />
<TextView
android:id="#+id/desc_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="16sp"
android:padding="10dp"
android:text="5 cigarettes non-smoked"
android:textColor="#color/teal_color" />
</LinearLayout>
</android.support.v7.widget.CardView>
</pairroxz.com.quitsmoking.view.AchievementsView>
AchievementsAdapter.java
public class AchievementsAdapter extends BaseAdapter {
private Context context;
private ArrayList<Achievements> list;
private LayoutInflater inflater;
public AchievementsAdapter(Context context,ArrayList<Achievements> list){
this.context = context;
this.list = list;
initialize();
}
private void initialize() {
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
AchievementsView view = null;
if(view==null) {
view = (AchievementsView) inflater.inflate(R.layout.achievements_row,null);
}else{
view = (AchievementsView) convertView;
}
view.setContents(list.get(position));
notifyDataSetChanged();
return view;
}
}
AchievementsView.java
public class AchievementsView extends RelativeLayout {
private TextView title_tv,desc_tv;
private ImageView image_iv;
public AchievementsView(Context context) {
super(context);
}
public AchievementsView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AchievementsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public AchievementsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
initialize();
}
private void initialize() {
image_iv = (ImageView)findViewById(R.id.image_iv);
title_tv = (TextView)findViewById(R.id.title_tv);
desc_tv = (TextView) findViewById(R.id.desc_tv);
}
public void setContents(Achievements achievements){
image_iv.setImageDrawable(getContext().getResources().getDrawable(achievements.getImg()));
title_tv.setText(achievements.getTitle());
desc_tv.setText(achievements.getDesc());
}
}
AchievementsActivity.java
public class AchievementsActivity extends AppCompatActivity implements View.OnClickListener {
private GridView achievements_gv;
private AchievementsAdapter adapter;
private ArrayList<Achievements> list;
DatabaseHelper helper;
private ImageView back_iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_achievements);
initialize();
}
private void initialize() {
helper = DatabaseHelper.getInstance(this);
back_iv = (ImageView) findViewById(R.id.back_iv);
achievements_gv = (GridView) findViewById(R.id.acheivements_gv);
list = new ArrayList<Achievements>();
getData();
setListener();
}
private void setListener() {
back_iv.setOnClickListener(this);
}
private void getData() {
list = helper.getAchievementsList();
//list =
adapter = new AchievementsAdapter(this, list);
achievements_gv.setAdapter(adapter);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back_iv:
onBackPressed();
break;
}
}
}
if(view == null)
view.setTag(viewHolder);
viewHolder = (ViewHolder) view.getTag();
don't use this. or send your code
I have a problem. I started a few weeks ago learning Android. I am trying to create an adapter for a list. But the lists doesn't show up AT ALL. Any ideas why? The items are objects type Verb. Here is my code:
///my main activity///
public class Verbs extends Activity {
private ListView listViewVerbs;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verbs);
//instantiate Verb
Verb[] VerbData=new Verb[3];
VerbData[0] = new Verb("machen","mache","machen","mache","machen","mache","machen");
VerbData[1] = new Verb("machen","mache","machen","mache","machen","mache","machen");
VerbData[2] = new Verb("machen","mache","machen","mache","machen","mache","machen");
//pass data to adapter
VerbAdapter adapter = new VerbAdapter(this,R.layout.row_verbs,VerbData);
listViewVerbs = (ListView)findViewById(R.id.list);
listViewVerbs.setAdapter(adapter);
listViewVerbs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String wasClicked = view.findViewById(R.id.nameVerb).toString();
Toast.makeText(Verbs.this,
"You clicked: " + wasClicked, Toast.LENGTH_LONG)
.show();
}
});
}
}
//class for an item of the list
public class Verb {
public String verbname;
public String fpersg;
public String fperspl;
public String sperssg;
public String sperspl;
public String tperssg;
public String tperspl;
//constructor
public Verb(String s, String s1, String s2, String s3, String s4, String s5,String s6){
this.verbname = s;
this.fpersg = s1;
this.fperspl = s2;
this.sperssg = s3;
this.sperspl = s4;
this.tperssg = s5;
this.tperspl = s6;
}
//setters
public void setVbName(String vbName){
this.verbname = vbName;
}
public void setFpersg(String fpersg){
this.fpersg = fpersg;
}
public void setFperpl(String fperpl){
this.fperspl = fperpl;
}
public void setSpersg(String spersg){
this.sperssg = spersg;
}
public void setSperspl(String sperpl){
this.sperspl = sperpl;
}
public void setTpersg(String tpersg){
this.tperssg = tpersg;
}
public void setTperpl(String tperpl){
this.tperspl = tperpl;
}
//getters
public String getVerbname(){
return this.verbname;
}
public String getFpersg(){
return this.fpersg;
}
public String getFperpl(){
return this.fperspl;
}
public String getSpersg(){
return this.sperssg;
}
public String getSperpl(){
return this.sperspl;
}
public String getTpersg(){
return this.tperssg;
}
public String getTperspl(){
return this.tperspl;
}
}
//the adapter
public class VerbAdapter extends ArrayAdapter<Verb>{
Context mContext;
int layoutResourceId;
Verb data[] = null;
public VerbAdapter(Context mContext, int layoutResourceId, Verb[] data){
super(mContext, layoutResourceId, data);
this.mContext = mContext;
this.layoutResourceId =layoutResourceId;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId,parent, false);
}
TextView textView0 = (TextView) convertView.findViewById(R.id.nameVerb);
TextView textView1 = (TextView) convertView.findViewById(R.id.firstpersonsg);
TextView textView2 = (TextView) convertView.findViewById(R.id.firstpersonpl);
TextView textView3 = (TextView) convertView.findViewById(R.id.secondpersonsg);
TextView textView4 = (TextView) convertView.findViewById(R.id.secondpersonpl);
TextView textView5 = (TextView) convertView.findViewById(R.id.thirdpersonsg);
TextView textView6 = (TextView) convertView.findViewById(R.id.thirdpersonpl);
Verb verb = data[position];
textView0.setText(verb.verbname);
textView1.setText(verb.fpersg);
textView2.setText(verb.fperspl);
textView3.setText(verb.sperssg);
textView4.setText(verb.sperspl);
textView5.setText(verb.tperssg);
textView6.setText(verb.tperspl);
return convertView;
}
}
------and two XML files named activity_verbs where the list is declared and row_verbs where the layout of each item is declared-----
row_verbs.xml ------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/nameVerb"
android:text="#string/nameVerb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#7663d5"
android:textColor="#f8f8f8"
android:padding="10dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstpersonsg"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:text="#string/firstsg"
android:layout_below="#+id/nameVerb"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:padding="5dp"
android:background="#cacaca"
android:textColor="#7663d5" />
<TextView
android:id="#+id/firstpersonpl"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/firstpl"
android:padding="5dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="2dp"
android:background="#FFCACACA"
android:textColor="#7663d5"
android:layout_below="#+id/nameVerb"
android:layout_toRightOf="#+id/firstpersonsg"/>
<TextView
android:id="#+id/secondpersonsg"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:text="#string/secondsg"
android:layout_below="#+id/firstpersonsg"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:padding="5dp"
android:background="#FFCACACA"
android:textColor="#7663d5" />
<TextView
android:id="#+id/secondpersonpl"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/secondpl"
android:padding="5dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="2dp"
android:background="#FFCACACA"
android:textColor="#7663d5"
android:layout_below="#+id/firstpersonpl"
android:layout_toRightOf="#+id/secondpersonsg"/>
<TextView
android:id="#+id/thirdpersonsg"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:text="#string/thirdsg"
android:layout_below="#+id/secondpersonsg"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:padding="5dp"
android:background="#FFCACACA"
android:textColor="#7663d5" />
<TextView
android:id="#+id/thirdpersonpl"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/thirdpl"
android:padding="5dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="2dp"
android:background="#FFCACACA"
android:textColor="#7663d5"
android:layout_below="#+id/secondpersonpl"
android:layout_toRightOf="#+id/thirdpersonsg"/>
</RelativeLayout>
and the other one activity_verbs.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="${relativePackage}.${activityClass}"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textSize="20sp"
android:padding="10dp"
android:background="#7663d5"
android:textColor="#f8f8f8"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lessonId"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#292929"
android:textColor="#f0f0f0"
android:padding="10dp"/>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/lessonId"/>
</RelativeLayout>
I copied your code with only one change:
I removed
tools:context="${relativePackage}.${activityClass}"
from activity_verbs and it works like a charm for me:
I uploaded my test-project with your code to DropBox, feel free to download it:
https://www.dropbox.com/s/8fcdab3rmx8zb57/ElaPinkSO.zip?dl=0
P.S. Very well structured question, good job!
I have this layout in two apps, one inside a RecyclerView and the other in a root activity layout.
Here the layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:background="#CFCFCF"
android:minHeight="250dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_dark"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright"
android:layout_above="#+id/commerceTextView"
>
<ImageView
android:src="#drawable/imagen"
android:id="#+id/commerceImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/commerceTextView"
android:gravity="center"
android:textStyle="bold"
android:textSize="24sp"
android:textColor="#F1F1F1"
android:background="#color/colorPrimary"
android:layout_alignParentBottom="true"
android:paddingTop="10dp"
android:text="Best food ever"
android:paddingBottom="10dp"/>
</RelativeLayout>
</FrameLayout>
Here the adapter
public class CommercesAdapter extends RecyclerView.Adapter<CommercesAdapter.CommercesViewHolder> {
private final Context context;
private final ImageLoader loader;
private List<CommerceEntity> commercesList;
#Inject
public CommercesAdapter(Context context, ImageLoader loader) {
this.context = context;
this.loader = loader;
}
public void setData(List<CommerceEntity> commercesList) {
this.commercesList = commercesList;
}
#Override
public CommercesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context)
.inflate(R.layout.list_comerces_item, parent, false);
return new CommercesViewHolder(view);
}
#Override
public void onBindViewHolder(CommercesViewHolder holder, int position) {
CommerceEntity commerce = commercesList.get(position);
String imageUri = commerce.getImageUri();
String name = commerce.getName();
// holder.commerceTypeName.setText(name);
//loader.bind(holder.commerceImage, imageUri);
}
#Override
public int getItemCount() {
return commercesList.size();
}
public static class CommercesViewHolder extends RecyclerView.ViewHolder {
public ImageView commerceImage;
public TextView commerceTypeName;
public CommercesViewHolder(View itemView) {
super(itemView);
commerceImage = (ImageView) itemView.findViewById(R.id.commerceImageView);
commerceTypeName = (TextView) itemView.findViewById(R.id.commerceTextView);
}
}
Here the RecyclerView
And here in a root activity layout
Someone knows why this happen? if I add android:centerInParent"true" to the nested FrameLayout the image appears but i don't understand why.
In your adapter, uncomment the two lines that you have commented out;
`
// holder.commerceTypeName.setText(name);
//loader.bind(holder.commerceImage, imageUri);
`
I create a ListFragment for show Text & Image my code hase not any error but when i run it , it get me crash & when I see Log it don't get me any Message ?
My StartActivity.class :
public class StartActivity extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,new Setting_Fragment()).commit();
}
}
My activity_start.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back"
tools:context="in.project.StartActivity" >
<ImageView
android:id="#+id/imgHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="#e42769"
android:src="#drawable/head" />
<LinearLayout
android:id="#+id/LFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="6" >
<ImageView
android:id="#+id/img_Setting_note"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_setting_note" />
<ImageView
android:id="#+id/img_Calendar"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_calendar" />
<ImageView
android:id="#+id/img_Mail"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_mail" />
<ImageView
android:id="#+id/img_Review_note"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_review_note" />
<ImageView
android:id="#+id/img_Help"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_help" />
<ImageView
android:id="#+id/img_Setting"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_setting" />
</LinearLayout>
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/LFooter"
android:layout_below="#+id/imgHeader" />
</RelativeLayout>
My SettingArrayAdapter.class :
public class SettingArrayAdapter extends ArrayAdapter<Instructure_setting>{
private Context context;
private List<Instructure_setting> objects;
public SettingArrayAdapter(Context context, int resource, List<Instructure_setting> objects) {
super(context, resource);
this.context = context;
this.objects = objects;
}
#SuppressLint({ "ViewHolder", "InflateParams" })
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Instructure_setting _Data = objects.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listview_setting_fragment, null);
ImageView image = (ImageView) view.findViewById(R.id.img_message);
image.setImageResource(_Data.getImageResource());
TextView tv = (TextView) view.findViewById(R.id.txt_message);
tv.setText(_Data.gettxtMessage());
return view;
}
}
My Setting_Fragment.class :
public class Setting_Fragment extends ListFragment{
List<Instructure_setting> DATA = new Setting_Fragment_Data().getMessages();
public Setting_Fragment()
{
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SettingArrayAdapter adapter = new SettingArrayAdapter(getActivity(),
R.layout.listview_setting_fragment,
DATA);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.setting_fragment, container, false);
return rootView;
}
}
My setting_fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity" >
<ListView
android:id="#+id/lstview_Setting_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
My Setting_Fragment_Data.class :
public class Setting_Fragment_Data {
private List<Instructure_setting> _Messages = new ArrayList<Instructure_setting>();
public List<Instructure_setting> getMessages() {
return _Messages;
}
public Setting_Fragment_Data() {
_Messages.add(new Instructure_setting("AAA", R.drawable.period_color));
_Messages.add(new Instructure_setting("BBB", R.drawable.password_color));
_Messages.add(new Instructure_setting("CCC", R.drawable.cleare_data_color));
_Messages.add(new Instructure_setting("DDD", R.drawable.chanel_connect_color));
_Messages.add(new Instructure_setting("EEE", R.drawable.backup_color));
_Messages.add(new Instructure_setting("FFF", R.drawable.review_note_color));
_Messages.add(new Instructure_setting("GGG", R.drawable.notification_color));
_Messages.add(new Instructure_setting("HHH", R.drawable.sync_server_color));
}
}
My Instructure_setting.class :
public class Instructure_setting {
// constants for field references
public static final String TXT_Message = "TextMessage";
public static final String IMAGE_RESOURCE = "imageResource";
// private fields
private String txtMessage;
private int imageResource;
// getters and setters
public String gettxtMessage() {
return txtMessage;
}
public void settxtMessage(String txtMessage) {
this.txtMessage = txtMessage;
}
public int getImageResource() {
return imageResource;
}
public void setImageResource(int imageResource) {
this.imageResource = imageResource;
}
// Used when creating the data object
public Instructure_setting(String msg, int imageResource) {
this.txtMessage = msg;
this.imageResource = imageResource;
}
// Create from a bundle
public Instructure_setting(Bundle b) {
if (b != null) {
this.txtMessage = b.getString(TXT_Message);
this.imageResource = b.getInt(IMAGE_RESOURCE);
}
}
// Package data for transfer between activities
public Bundle toBundle() {
Bundle b = new Bundle();
b.putString(TXT_Message, this.txtMessage);
b.putInt(IMAGE_RESOURCE, this.imageResource);
return b;
}
// Output txtMessage data
#Override
public String toString() {
return txtMessage;
}
}
This is my listview_setting_fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp">
<ImageView
android:id="#+id/img_message"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:contentDescription="placeholder" />
<TextView
android:id="#+id/txt_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/img_message"
android:text="placeholder"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
NOTICE : I am using from appcompat_v7 .
When you use ListFragment or ListActivity then ListView id in xml must be :
android:id="#id/android:list
Change your ListView id in xml :
<ListView
android:id="#id/android:list"
Instead of
<ListView
android:id="#+id/lstview_Setting_fragment"
Note : Try to set List Adapter in onCreateView() instead of onCreate() in Fragment.