I've been trying to get a custom Recycler view adapter to work. I can't quite see why it wouldn't work. What I find strange is that the code for the custom adapter does not execute (as in it doesn't break when I put a breakpoint there) and I'm certain I bind the custom adapter to the recycler view. I hope someone can see why it doesn't work.
Activity:
public class payment_history extends AppCompatActivity {
RecyclerView list;
ArrayList<pay_item> list_data;
pay_adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment_history);
list_data = new ArrayList<>();
list_data.add(new pay_item(10, 100, "jow", "10"));
list_data.add(new pay_item(10, 100, "joe", "10"));
list_data.add(new pay_item(10, 100, "joe", "10"));
list = findViewById(R.id.payment_history_list);
list.setLayoutManager(new LinearLayoutManager(this));
adapter = new pay_adapter(list_data);
list.setHasFixedSize(true);
list.setAdapter(adapter);
}
}
class pay_item {
int time;
double amount;
String name, table;
pay_item(int time, double amount, String name, String table) {
this.time = time;
this.amount = amount;
this.name = name;
this.table = table;
}
}
Adapter:
class pay_adapter extends RecyclerView.Adapter<pay_adapter.viewholder> {
private ArrayList<pay_item> data;
pay_adapter(ArrayList<pay_item> in) {
data = in;
}
#NonNull
#Override
public viewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.payment_history_item, parent, false);
return new viewholder(v);
}
#Override
public void onBindViewHolder(#NonNull viewholder holder, int position) {
holder.name.setText(data.get(position).name);
holder.table.setText(data.get(position).table);
holder.amm.setText(String.valueOf(data.get(position).amount));
holder.time.setText(data.get(position).time);
}
#Override
public int getItemCount() {
return 0;
}
static class viewholder extends RecyclerView.ViewHolder {
TextView time, amm, name, table;
viewholder(View view) {
super(view);
time = view.findViewById(R.id.pay_time);
amm = view.findViewById(R.id.pay_amount);
name = view.findViewById(R.id.pay_name);
table = view.findViewById(R.id.pay_table);
}
}
}
Row view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp">
<TextView
android:id="#+id/pay_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Time"
android:textColor="#000000"
android:textSize="36sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/pay_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="#+id/pay_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:text="table" />
</LinearLayout>
<TextView
android:id="#+id/pay_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0"
android:text="Amount"
android:textColor="#color/colorPrimary"
android:textSize="36sp" />
</LinearLayout>
Layout with recycler view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".payment_history">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/payment_history_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Any help is appreciated.
Two things
pay_adapter(ArrayList<pay_item> in) {
data = in;
}
#Override
public int getItemCount() {
return data.size() ;
}
Related
Please, help to figure out why my implementation of RecyclerView doesn't show anything.
Retrieving data asynchronously and result is successfull, but don't know how to display it correctly.
Activity
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
checkAndRequestPermissions(Manifest.permission.INTERNET);
RecyclerView recyclerView = binding.recyclerView;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
NewsAdapter adapter = new NewsAdapter();
recyclerView.setAdapter(adapter);
RssService.runRssFeed(newsList -> {
System.out.println("SIZE ----- " + newsList.size());
adapter.setItems(newsList);
});
}
}
Adapter
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private final List<NewsModel> news = new ArrayList<>();
#Override
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_item, parent, false);
return new ViewHolder(view);
}
public void setItems(List<NewsModel> news) {
this.news.addAll(news);
notifyDataSetChanged();
}
public void clearItems() {
this.news.clear();
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(#NonNull NewsAdapter.ViewHolder holder, int position) {
holder.title.setText(news.get(holder.getAdapterPosition()).getTitle());
holder.date.setText(news.get(holder.getAdapterPosition()).getDate());
holder.link.setText(news.get(holder.getAdapterPosition()).getLink());
}
#Override
public int getItemCount() {
return news.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
final TextView title, date, link;
public ViewHolder(View view) {
super(view);
title = view.findViewById(R.id.titleTxt);
date = view.findViewById(R.id.dateTxt);
link = view.findViewById(R.id.linkTxt);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
news_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical">
<TextView
android:id="#+id/titleTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="16sp"
android:textStyle="normal"
android:typeface="sans" />
<TextView
android:id="#+id/linkTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="normal"
android:typeface="sans" />
<TextView
android:id="#+id/dateTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="10dp"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="normal"
android:typeface="sans" />
</LinearLayout>
I think It showed correctly but you can't see because your text color is white
**issue in these lines**
holder.title.setText(news.get(holder.getAdapterPosition()).getTitle());
holder.date.setText(news.get(holder.getAdapterPosition()).getDate());
holder.link.setText(news.get(holder.getAdapterPosition()).getLink());
change these like
holder.title.setText(news.get(position).getTitle());
RecyclerView is not showing at all. Only the TextView is displayed.
I looked at other questions and used all answers from previous questions.
Other answers suggested that recycler's width can't be set wrap content or that setAdapter should be called after setting layout manager and I meet these conditions.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.main_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final MainAdapter adapter = new MainAdapter();
recyclerView.setAdapter(adapter);
List<FirebaseProduct> firebaseProductList = new ArrayList<>();
firebaseProductList = getData(); //Here is my Firebase code
adapter.setList(firebaseProductList);
}
}
activity_main.xml
<RelativeLayout 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=".MainActivity">
<TextView
android:id="#+id/main_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/aktualne_produkty"
android:gravity="center"
android:background="#color/colorPrimary"
android:textAppearance="#style/TextAppearance.AppCompat.Large">
</TextView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/main_recycler"
android:layout_height="wrap_content"
android:layout_width="match_parent"
tools:listitem="#layout/card_main"
android:layout_below="#id/main_text"/>
</RelativeLayout>
MainAdapter
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainHolder> {
private List<FirebaseProduct> productList = new ArrayList<>();
#NonNull
#Override
public MainHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_main, parent, false);
return new MainHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull MainHolder holder, int position) {
FirebaseProduct product = productList.get(position);
Log.d("Prod holder", "name - " + product.getProductName());
Log.d("Prod holder", "num - " + product.getProductNumber());
holder.nameView.setText(product.getProductName());
holder.numView.setText(product.getProductNumber());
}
#Override
public int getItemCount() {
return productList.size();
}
public void setProductList(List<FirebaseProduct> productList1){
this.productList = productList1;
notifyDataSetChanged();
}
public List<FirebaseProduct> getList(){
return productList;
}
public static class MainHolder extends RecyclerView.ViewHolder{
private TextView nameView;
private TextView numView;
public MainHolder(View itemView){
super(itemView);
nameView = itemView.findViewById(R.id.product_name);
numView = itemView.findViewById(R.id.product_count);
}
}
}
card_main
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:layout_weight="3">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
</TextView>
<TextView
android:id="#+id/product_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/product_name">
</TextView>
</RelativeLayout>
<ImageView
android:id="#+id/product_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="#string/product_description">
</ImageView>
</LinearLayout>
</androidx.cardview.widget.CardView>
You are calling adapter.setList(firebaseProductList); in MainActivity but the function name in MainAdapter is setProductList(). So change it to adapter. setProductList(firebaseProductList)
Also, you want to be sure getData(); in MainActivity should return a non-empty list. you can verify it using debugger or by just adding a log statement before adapter.setList(firebaseProductList). like this:
Log.d("LIST_SIZE", firebaseProductList.size()); //this will print list size
adapter.setList(firebaseProductList); //you have to change it to adapter. setProductList(firebaseProductList)
I decided to make an e-commerce store app and I want to include two or more same layouts which contains a recycler view which takes it's data from java code (for now).
I tried to add different ids for both of them but I don't see the products
Layout I want to include (horizontal_scroll_layout.xml) :
<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_margin="8dp"
android:paddingBottom="8dp"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/horizontal_scroll_layout_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Most Popular"
android:fontFamily="#font/cera_pro_medium"
android:textColor="#000000"
android:textAlignment="center"
android:textSize="17dp"
app:layout_constraintBottom_toBottomOf="#+id/horizontal_scroll_view_more"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/horizontal_scroll_view_more" />
<TextView
android:id="#+id/horizontal_scroll_view_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:fontFamily="#font/cera_pro_regular"
android:text="View More"
android:textColor="#color/colorPrimary"
android:textSize="14dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/horizontal_product_recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/horizontal_scroll_view_more" />
</androidx.constraintlayout.widget.ConstraintLayout>
What I'm doing (fragment_home.xml):
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
<include android:id="#+id/test1"
layout="#layout/horrizontal_scroll_layout" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
<include
android:id="#+id/test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/horrizontal_scroll_layout" />
</LinearLayout>
Java File Code (HomeFragment.java):
//////////// HS Product Layout (HS = Horizontal Scroll)
hSlayoutTextView = view.findViewById(R.id.horizontal_scroll_layout_title);
hSViewMoreTextView = view.findViewById(R.id.horizontal_scroll_view_more);
hSRecyclerView = view.findViewById(R.id.horizontal_product_recycler_view);
List<HorizontalProductScrollModel> horizontalProductScrollModelList = new ArrayList<>();
horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.drawable.brd, "₹35", "₹40", "English Oven Premium \n" + "Sandwich Bread", "350 g"));
.
.
.
.
.
horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.drawable.brd, "₹35", "₹40", "English Oven Premium \n" + "Sandwich Bread", "350 g"));
HorizontalProductScrollAdapter horizontalProductScrollAdapter = new HorizontalProductScrollAdapter(horizontalProductScrollModelList);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
hSRecyclerView.setLayoutManager(linearLayoutManager);
hSRecyclerView.setAdapter(horizontalProductScrollAdapter);
horizontalProductScrollAdapter.notifyDataSetChanged();
/////// HS Product Layout
Adapter Class (HorizontalProductScrollAdapter.java)
public class HorizontalProductScrollAdapter extends RecyclerView.Adapter {
private List<HorizontalProductScrollModel> horizontalProductScrollModelList;
public HorizontalProductScrollAdapter(List<HorizontalProductScrollModel> horizontalProductScrollModelList) {
this.horizontalProductScrollModelList = horizontalProductScrollModelList;
}
#NonNull
#Override
public HorizontalProductScrollAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_scroll_item_layout, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull HorizontalProductScrollAdapter.ViewHolder viewHolder, int position) {
int resource = horizontalProductScrollModelList.get(position).getProductImage();
String price = horizontalProductScrollModelList.get(position).getProductPrice();
String mrp = horizontalProductScrollModelList.get(position).getProductMRP();
String name = horizontalProductScrollModelList.get(position).getProductName();
String weight = horizontalProductScrollModelList.get(position).getProductWeight();
viewHolder.setProductImage(resource);
viewHolder.setProductPrice(price);
viewHolder.setProductMRP(mrp);
viewHolder.setProductName(name);
viewHolder.setProductWeight(weight);
}
#Override
public int getItemCount() {
return horizontalProductScrollModelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView productImage;
private TextView productPrice;
private TextView productMRP;
private TextView productName;
private TextView productWeight;
public ViewHolder(#NonNull View itemView) {
super(itemView);
productImage = itemView.findViewById(R.id.hs_product_image);
productPrice = itemView.findViewById(R.id.hs_product_price);
productMRP = itemView.findViewById(R.id.hs_product_mrp);
productName = itemView.findViewById(R.id.hs_product_name);
productWeight = itemView.findViewById(R.id.hs_product_weight);
}
private void setProductImage(int resource){
productImage.setImageResource(resource);
}
private void setProductPrice(String price){
productPrice.setText(price);
}
private void setProductMRP(String mrp) {
productMRP.setText(mrp);
}
private void setProductName(String name) {
productName.setText(name);
}
private void setProductWeight(String weight) {
productWeight.setText(weight);
}
}
}
I see the two text views but I don't see any of the products. For better reference of what I want to achieve, I've added a link below with both the screenshots:
What I am getting: https://snag.gy/uKfVT4.jpg
What I want: https://snag.gy/n2GHyg.jpg
EDIT: Log shows this E/RecyclerView: No adapter attached; skipping layout
where do I attach it?
I am writing a ListView with 2 TextViews per row, but when I execute I get only one TextView.
Here is my adapter extending Base adapter
public class GlossaryListViewAdapter extends BaseAdapter {
List<Glossary_Entry> glossary_entryList;
Context context;
public GlossaryListViewAdapter(Context context,List<Glossary_Entry> glossary_entryList){
this.glossary_entryList = glossary_entryList;
this.context = context;
}
#Override
public int getCount() {
return glossary_entryList.size();
}
#Override
public Object getItem(int postion) {
return glossary_entryList.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view == null){
view = LayoutInflater.from(context).inflate(R.layout.glossary_single_row,viewGroup,false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.titleTextView.setText(glossary_entryList.get(position).getTitle());
viewHolder.definitionTextView.setText(glossary_entryList.get(position).getDefinition());
return view;
}
private static class ViewHolder{
TextView titleTextView;
TextView definitionTextView;
public ViewHolder(View view){
titleTextView = (TextView)view.findViewById(R.id.glossary_title_textview);
definitionTextView = (TextView)view.findViewById(R.id.glossary_word_definition_textview);
}
}
}
Here is my Activity code
public class GlossaryAcvtivity extends AppCompatActivity {
ListView glossaryListView;
ListAdapter listAdapter;
List<Glossary_Entry> glossary_entryList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glossary);
/**
* Sample data
*/
glossary_entryList = new ArrayList<>();
glossary_entryList.add( new Glossary_Entry("Force","This is a push or push on an object.They can be duew to a phenomenon such as gravity, magnetism, or anything that might cause a mass to accelerate."));
glossary_entryList.add( new Glossary_Entry("Motion","Change in position of an object over time, described in terms of displacement,distance,velocity,acceleration,time and speed"));
glossary_entryList.add( new Glossary_Entry("Quantum Leap","An Abrupt transition of an electron,atom, or molecule from one quantum state to another,with the absorption or emission of a quantum"));
glossary_entryList.add( new Glossary_Entry("Force","This is a push or push on an object.They can be duew to a phenomenon such as gravity, magnetism, or anything that might cause a mass to accelerate."));
glossaryListView = (ListView)findViewById(R.id.glossary_list_view);
listAdapter = new GlossaryListViewAdapter(getApplicationContext(),glossary_entryList);
glossaryListView.setAdapter(listAdapter);
}
}
The source of my data is an Entry class
public class Glossary_Entry {
public String title;
public String definition;
public String getTitle() {
return title;
}
public String getDefinition() {
return definition;
}
public Glossary_Entry(String title,String definition){
this.title = title;
this.definition = definition;
}
}
Here is my glossary_single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#color/blue"
android:textStyle="bold"
android:textSize="16sp"
android:layout_margin="4dp"
android:id="#+id/glossary_title_textview"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_description"
android:layout_margin="4dp"
android:textSize="12sp"
android:id="#+id/glossary_word_definition_textview"/>
</LinearLayout>
Here is my glossary_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/white"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:id="#+id/glossary_toolbar"
android:background="#color/colorPrimary"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<ListView
android:layout_width="match_parent"
android:id="#+id/glossary_list_view"
android:layout_margin="8dp"
android:layout_below="#+id/glossary_toolbar"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
Please help when I execute I get only one TextView per row...
Apparently, I needed to change the text color to black in the second TextView. The text was there just not visible.
<LinearLayout xmlns:android="http:// schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#color/blue"
android:textStyle="bold"
android:textSize="16sp" android:layout_margin="4dp"
android:id="#+id/glossary_title_textview"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_description"
android:layout_margin="4dp"
android:textSize="12sp"
android:textColor="#color/black"
android:id="#+id/glossary_word_definition_textview"/>
</LinearLayout
I am new in android development and I need to modify my code to make it show the text with image function:
print_view xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BFAF80">
<ListView
style="14dp"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:id="#+id/operations"
android:layout_below="#+id/search_view"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_below="#+id/operations"
android:layout_centerHorizontal="true"
android:src="#drawable/printer" />
list_row_item xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:padding="20dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="#drawable/wifi"
android:layout_height="wrap_content"
android:id="#+id/imageForList" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_height="wrap_content"
android:text="Nearby Wifi"
android:id="#+id/textForList" />
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="#drawable/bluetooth"
android:layout_height="wrap_content"
android:id="#+id/imageForList2" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_height="wrap_content"
android:text="Nearby Blutooth"
android:id="#+id/textForList2" />
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="#drawable/usb"
android:layout_height="wrap_content"
android:id="#+id/imageForList3" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_height="wrap_content"
android:text="Direct USB Connected"
android:id="#+id/textForList3" />
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="#drawable/cloud"
android:layout_height="wrap_content"
android:id="#+id/imageForList4" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_height="wrap_content"
android:text="Google Cloud Print"
android:id="#+id/textForList4" />
</LinearLayout>
SettingsList java class:
public class SettingsList {
private int[] images;
private String[] items;
public SettingsList(int[] images, String[] items) {
this.images = images;
this.items = items;
}
public int[] getImages() {
return images;
}
public void setImages(int[] images) {
this.images = images;
}
public String[] getItems() {
return items;
}
public void setItems(String[] items) {
this.items = items;
}
}
SettingsAdapter java class :
public class SettingsAdapter extends ArrayAdapter<SettingsList> {
public SettingsAdapter(Context context, int resource, List<SettingsList> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItems holder;
if(convertView == null){
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_row_item, parent, false);
holder = new ViewHolderItems();
holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList);
holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList2);
holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList3);
holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList4);
holder.holderText = (TextView)convertView.findViewById(R.id.textForList);
holder.holderText = (TextView)convertView.findViewById(R.id.textForList2);
holder.holderText = (TextView)convertView.findViewById(R.id.textForList3);
holder.holderText = (TextView)convertView.findViewById(R.id.textForList4);
convertView.setTag(holder);
}else{
holder = (ViewHolderItems)convertView.getTag();
}
SettingsList current = getItem(position);
holder.holderImage.setImageResource(current.getImages()[position]);
holder.holderText.setText(current.getItems()[position]);
return convertView;
}
static class ViewHolderItems{
ImageView holderImage;
TextView holderText;
}
}
PrintView mainactivity :
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class PrintView extends ActionBarActivity { private ListView lv;
private SettingsAdapter adapter;
private List<SettingsList> itemList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.print_view);
int[] imgs = new int[]
{
R.drawable.wifi,
R.drawable.bluetooth,
R.drawable.usb,
R.drawable.cloud,
};
String[] values = new String[]
{"Nearby Wifi",
"Nearby Blutooth",
"Direct USB Connected",
"Google Cloud Print"
};
lv = (ListView)findViewById(R.id.operations);
itemList = new ArrayList<>();
itemList.add(new SettingsList(imgs, values));
adapter = new SettingsAdapter(PrintView.this, R.layout.list_row_item, itemList);
lv.setAdapter(adapter);
}
}
Try this:
1) Create a res/layout/list_row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:padding="20dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:src="#mipmap/ic_launcher"
android:layout_height="wrap_content"
android:id="#+id/imageForList" />
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textForList" />
</LinearLayout>
</LinearLayout>
2) Create your custom List item:
public class SettingsList {
private int image;
private String item;
public SettingsList(int image, String item) {
this.image = image;
this.item = item;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
}
3) Create your custom adapter
public class SettingsAdapter extends ArrayAdapter<SettingsList> {
public SettingsAdapter(Context context, int resource, List<SettingsList> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItems holder;
if(convertView == null){
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_row_item, parent, false);
holder = new ViewHolderItems();
holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList);
holder.holderText = (TextView)convertView.findViewById(R.id.textForList);
convertView.setTag(holder);
}else{
holder = (ViewHolderItems)convertView.getTag();
}
SettingsList current = getItem(position);
holder.holderImage.setImageResource(current.getImage());
holder.holderText.setText(current.getItem());
return convertView;
}
static class ViewHolderItems{
ImageView holderImage;
TextView holderText;
}
}
4) In your Activity that you want the listview to be shown do this:
public class YourActivity extends ActionBarActivity {
private ListView lv;
private SettingsAdapter adapter;
private List<SettingsList> itemList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.operations);
itemList = new ArrayList<>();
itemList.add(new SettingsList(R.drawable.wifi, Nearby Wifi));
itemList.add(new SettingsList(R.drawable.bluetooth, Nearby Blutooth));
itemList.add(new SettingsList(R.drawable.usb, Direct USB Connected));
itemList.add(new SettingsList(R.drawable.cloud, Google Cloud Print));
adapter = new SettingsAdapter(YourActivity.this, R.layout.list_row_item, itemList);
lv.setAdapter(adapter);
}
}
5) The print_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BFAF80">
<ListView
style="14dp"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:id="#+id/operations"
android:layout_below="#+id/search_view"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Hope it helps!!!