I just created a list view with a custom view to create a country list for select
<?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:background="#color/primary_light"
android:orientation="vertical"
tools:context=".RnActivity.RnView.RnUser.ActivitySignUpOptions">
<ListView
android:id="#+id/country_list_for_select_list_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/primary"
/>
</LinearLayout>
custom view
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/view_layout_country_list_item_country_icon_image"
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="#drawable/gjh_logo_b"
/>
<TextView
android:id="#+id/view_layout_country_list_item_country_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="start|center_vertical"
android:layout_weight="1"
android:text="India"
android:textSize="18sp" />
<TextView
android:id="#+id/view_layout_country_list_item_country_code"
android:layout_width="50dp"
android:layout_height="match_parent"
android:gravity="center|center_vertical"
android:layout_gravity="right"
android:text="IN"
android:textSize="18sp"
/>
</LinearLayout>
In Activity
ListView country_list_for_select_list_area;
#Override
public void lodeControls() {
country_list_for_select_list_area=findViewById(R.id.country_list_for_select_list_area);
}
#Override
public void setEvents() {
}
RxAdopterCountry countryAdopter;
#Override
public void createAdopters() {
countryAdopter=new RxAdopterCountry(getActivity(),R.layout.view_layout_country_list_item, RsXtraCountryList.getInstance().getCountryList());
}
#Override
public void setData() {
country_list_for_select_list_area.setAdapter(countryAdopter);
}
In Adopter
private final ArrayList<RentCountry> countryList;
private Context context;
public RxAdopterCountry(#NonNull Context context, int resource, ArrayList<RentCountry> countryList) {
super(context, resource);
this.context=context;
this.countryList=countryList;
}
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if(convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.view_layout_country_list_item, parent, false);
}
ImageView view_layout_country_list_item_country_icon_image=convertView.findViewById(R.id.view_layout_country_list_item_country_icon_image);
TextView view_layout_country_list_item_country_name=convertView.findViewById(R.id.view_layout_country_list_item_country_name);
TextView view_layout_country_list_item_country_code=convertView.findViewById(R.id.view_layout_country_list_item_country_code);
view_layout_country_list_item_country_name.setText(countryList.get(position).getName());
view_layout_country_list_item_country_code.setText(countryList.get(position).getCode());
view_layout_country_list_item_country_icon_image.setImageResource(countryList.get(position).getFlag_id());
return super.getView(position, convertView, parent);
}
i debugged the whole code and there are 250 items are coming in
private final ArrayList<RentCountry> countryList;
but list view is showing nothing
What I am doing wrong? I tried everything and debugged the whole code 10 times. yes I am getting the data in the ArrayList bu still noting is appearing in the list view.
Please help
The problem with your code is you are returning super.getView(position, convertView, parent) from getView while you should be returning the View you just created that is convertView .
For super constructor call you have to use super(context, resource, countryList) so that Adapter can return the size of list.
Change getView like below
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
//...
return convertView;
}
Also you should be using RecyclerView now. ListView and GridView are legacy widgets for a long time . RecyclerView is much more flexible.
Related
I want to create Fragment with ListView, so, I have a ListView with listitem in my Fragment activity, but my listitem is not displayed when i open my fragment, i need help with it, how display items in fragment? When i make this ListView in Activity, all is working well, how fix this? My code here:
FollowFragment.java
public class FollowFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
getActivity().setTitle("Подписки");
return inflater.inflate(R.layout.fragment_follow,container,false);
}
}
follow_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:padding="10dp">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/item_follow"/>
</FrameLayout>
item_follow.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/favourite_match_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/team_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="#font/roboto_medium"
android:text="FC Chelsea"
android:textColor="#color/black"
tools:ignore="MissingConstraints" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/team_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_medium"
android:text="Manchester City"
app:layout_constraintTop_toBottomOf="#+id/team_1"
android:textColor="#color/black"
tools:ignore="MissingConstraints" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/match_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="#font/roboto"
android:text="25 сентября 2021 года"
android:textColor="#color/colorGrey"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="#id/team_2"
tools:ignore="MissingConstraints" />
<ImageButton
android:layout_width="48dp"
android:layout_height="50dp"
android:layout_marginEnd="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="#drawable/ic_love"
android:background="#00000000"
app:layout_constraintLeft_toRightOf="#id/team_2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
well that's because you didn't assign an adapter with the correct data to it yet.
your adapter should extend ArrayAdapter and override getView in which you should set your view with the correct content based on your data.
what an adapter might look like :
public class MyAdapter extends ArrayAdapter<YourDataCustomClassHere> {
ArrayList<YourDataClassHere> data;
Context context ;
public MyAdapter(#NonNull Context context, int resource, #NonNull ArrayList<YourDataCustomClassHere> objects) {
super(context, resource, objects);
this.data = objects;
this.context = context;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_follow, parent, false);
}
//example on setting one of your fields
((AppCompatTextView) convertView.findViewById(android.R.id.team_1))
.setText(data.get(position).getText());
//set Other Fields here
return convertView;
}
}
where your data class should be a custom class that holds the correct information for each item in the list and getText in data.get(position).getText() is an arbitary method I assumed that will be in your custom class to get some sort of text but you're free to replace it with what you want.
then in your onCreateView you should set the adapter to your ListView like that :
public class FollowFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
getActivity().setTitle("Подписки");
View rootView= inflater.inflate(R.layout.fragment_follow,container,false);
ListView list = rootView.findViewById(R.id.listview);
MyAdapter myadapter = new MyAdapter(getContext(),R.layout.item_follow,YourDataArrayListHere);
list.setAdapter(myadapter);
return rootView;
}
}
I trust that you'll find your way and replace YourDataArrayListHere with the correct ArrayList of your data custom class.
anyway I hope you reconsider your choice of ListView as it consume much Ram (Memory) during runtime and use a RecyclerView instead
I keep getting the error "E/RecyclerView: No adapter attached; skipping layout" when the RecyclerView list is shown. I'm getting data correctly from API but I got "void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"
I know this question has been asked a lot before but I checked most of the questions and none has worked for me.
This is my fragment:
public class HomeFragment extends Fragment {
private View view;
private RecyclerView recyclerView;
private ArrayList<Result> arrayList;
private SwipeRefreshLayout refreshLayout;
private PostAdapter postAdapter;
private MaterialToolbar toolbar;
private SharedPreferences userPref;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.layout_home,container,false);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
userPref = getContext().getApplicationContext().getSharedPreferences("user", Context.MODE_PRIVATE);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerHome);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
refreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.swipeHome);
toolbar =(MaterialToolbar) view.findViewById(R.id.toolbarHome);
((HomeActivity)getContext()).setSupportActionBar(toolbar);
getResults();
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
getResults();
}
});
}
private void getResults() {//Working Fine}
}
My adapter:
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostsHolder>{
private ArrayList<Result> list;
private Context context;
public PostAdapter(Context context, ArrayList<Result> list) {
this.list = new ArrayList<Result>(list);
this.context = context;
}
#NonNull
#Override
public PostsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_home, parent, false);
return new PostsHolder(view);
}
#Override
public void onBindViewHolder(#NonNull PostsHolder holder, int position) {
Result result = list.get(position);
holder.imgPostDate.setText(result.getDate());
}
#Override
public int getItemCount() {
return list.size();
}
public static class PostsHolder extends RecyclerView.ViewHolder{
private TextView imgPostProfile,imgPostName,imgPostDate;
private ImageButton btnPostOption;
public PostsHolder(#NonNull View itemView) {
super(itemView);
imgPostProfile =(TextView) itemView.findViewById(R.id.imgPostProfile);
imgPostName = (TextView)itemView.findViewById(R.id.imgPostName);
imgPostDate =(TextView) itemView.findViewById(R.id.imgPostDate);
btnPostOption =(ImageButton) itemView.findViewById(R.id.btnPostOption);
}
}
}
I modified my code according to the answers I found to similar questions but none of them worked.
layout_home
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.MaterialToolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbarHome"
android:elevation="1dp"
android:theme="#style/Theme.MaCoSISBottomAppBar"
app:title=""
tools:targetApi="lollipop">
<!-- <ImageView-->
<!-- android:layout_width="100dp"-->
<!-- android:layout_height="40dp"-->
<!-- android:src="#drawable/logo_dark"/>-->
</com.google.android.material.appbar.MaterialToolbar>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:id="#+id/swipeHome"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerHome"
android:layout_height="match_parent"
android:layout_width="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
layout_post
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="#+id/imgPostProfile"
android:layout_width="60sp"
android:layout_height="60sp"
android:background="#drawable/shape_circle"
android:text="MC"
android:gravity="center"
android:textColor="#color/colorWhite"
android:textSize="14sp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingLeft="6dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgPostName"
android:text="BBAD1252 Social Harmony and Business Skills Development"
android:textColor="#color/colorBlack"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgPostDate"
android:text="12 Novermber 2020"
android:textColor="#color/colorLightGrey"
android:textSize="12dp" />
</LinearLayout>
<ImageButton
android:id="#+id/btnPostOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:background="#android:color/transparent"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_baseline_more_vert_24"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#color/colorLightGrey"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
In onCreateViewHolder you inflated the wrong xml. It should've been R.layout.layout_post
#NonNull
#Override
public PostsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_post, parent, false);
return new PostsHolder(view);
}
To prevent from error E/RecyclerView: No adapter attached; skipping layout, you should call recyclerView.setAdapter(adapter); inside your init(); method.
Then after you have your data you should notify the adapter in order to display your data by calling adapter.notifyDataSetChanged(); or using any other notify methods. Check this answer for other notify methods.
If your getResults() is where you're calling your recyclerview.setAdapter(), you're calling setAdapter too late. You need to make sure your adapter is set during onCreateView() so that the Recyclerview isn't being skipped while drawing the layout.
No Adapter Attached; Skipping layout:
you should setadapter in your main thread methods of fragment; then add or update data to adapter
Nullpointer at settext [at adapter]: In onCreateViewHolder you inflated the wrong xml. It should be R.layout.layout_post instead of R.layout.layout_home
Hello I have custom adopter for a ListView how can I add clickListener on item and I also need a menu in item with some other button(DropDwons) I am new to android development I tried many solutions from internet but not working for me.
please help me
My Code
public class TrackAdapter extends ArrayAdapter<Result> {
private Context mContext;
private List<Result> trackList = new ArrayList<>();
public TrackAdapter(#NonNull Context context, List<Result> list) {
super(context, 0 , list);
mContext = context;
trackList = list;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null)
listItem = LayoutInflater.from(mContext).inflate(R.layout.track,parent,false);
Result currentTrack = trackList.get(position);
return listItem;
}
}
Item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:padding="8dp"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView_poster"
android:layout_width="45dp"
android:src="#drawable/default_album"
android:layout_height="45dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView_release"
android:layout_width="wrap_content"
android:text="Artist Name"
app:layout_constraintStart_toEndOf="#+id/imageView_poster"
app:layout_constraintTop_toBottomOf="#+id/textView_name" />
</android.support.constraint.ConstraintLayout>
Just give id to your android.support.constraint.ConstraintLayout like:
android:id="#+id/parent"
And then create like
ConstraintLayout parent = (ConstraintLayout)listItem.findViewById(R.id.parent);
parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Code stuff
}
});
I Want Something Like Shown In Image Below... As Item 3, Item 4 And Item 7 Has A Toggle Switch But Item 1, Item 2, Item 5, Item 6 Doesn't Have. Can Anyone Help Me To Make This Layout And Make Toggle Switch Work Too
I Want This (Made In Photoshop)
My Java File
import android.content.*;
import android.view.*;
import android.widget.*;
class CustomSettingsAdapter extends ArrayAdapter<String> {
String[] settingItems = {
"Themes",
"Entry Tune",
"Remember Last Location",
"About Us",
"Exit"
};
public CustomSettingsAdapter(Context context, String[] Items) {
super(context, R.layout.main_settings_listview, Items);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.main_settings_listview, parent, false);
String itemName = getItem(position);
TextView textView =(TextView) customView.findViewById(R.id.itemName);
Switch mButton = (Switch) customView.findViewById(R.id.Switch);
if (position == 1 || position == 2) {
mButton.setVisibility(View.VISIBLE);
}
textView.setText(settingItems[position]);
return customView;
}
}
** 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:minHeight="48dp"
android:id="#+id/mainActivityListBackground"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Number"
android:id="#+id/itemName"
android:layout_marginLeft="5dp"
android:textSize="18sp"
android:layout_centerVertical="true"
/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Switch"
android:visibility="invisible"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</RelativeLayout>
Use this code it help you.
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/code"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:textSize="16dp" />
<Switch
android:id="#+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:gravity="center"
android:text="Off"
android:visibility="invisible" />
</LinearLayout>
and use this adapter
public class PhoneAdapter extends BaseAdapter {
private Context context;
public PhoneAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return 7;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
convertView = View.inflate(context, R.layout.item, null);
TextView mCode = (TextView) convertView.findViewById(R.id.code);
Switch mButton = (Switch) convertView.findViewById(R.id.toggleButton);
mCode.setText("item"+i+1);
if (i == 2 || i == 3 || i == 6)
mButton.setVisibility(View.VISIBLE);
return convertView;
}}
and this is output:-
feel free to ask if you stuck anywhere in between.
EDIT:- getView() is use for identify which button is clicked so you don't want to care about it .In the getView() the i variable is used for identify which item is clicked.
Just set your OnchangeListner inside getView() and your problem solve.
You can use recycler view.You can do this by creating two xml for two different designs,and on basis of condition you can set view in layout inflater.Use these methods for extra views.
#Override
public int getViewTypeCount() {
return VIEW_TYPE_COUNT;
}
#Override
public int getItemViewType(int position) {
return position;
}
You can also refer to this link.
in my spinner i am trying to attach an image and a text View,
Following is my layout xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/UICurrencyCurrencyFlag"
android:layout_height="20dp"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/cross_btn" />
<TextView
android:id="#+id/UICurrencyCurrencyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:layout_toRightOf="#+id/UICurrencyCurrencyFlag"
android:text="TextView" />
</RelativeLayout>
Following is the constructor i am using for my Custom Array Adapter
public class CustomArrayAdapterForCurrencies extends ArrayAdapter<CharSequence>
{
private final Activity context;
public final CharSequence[] keys;
public CustomArrayAdapterForCurrencies(Activity context, CharSequence[] keys)
{
super(context, R.layout.ui_currency_layout,keys);
this.context = context;
this.keys = keys;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.ui_currency_layout, null);
final ViewHolder viewHolder = new ViewHolder();
When my activity starts, spinner does show image and text but when i try to select spinner it gives error that Adapter need resouce id for the text view
How can i resolve this issue,
Best Regards
You should add this(row layout) in getView(...) in your CustomArrayAdapterForCurrencies
see this tuts.... http://android-er.blogspot.in/2010/12/custom-arrayadapter-for-spinner-with.html
Change your super statement to
super(context, R.layout.ui_currency_layout, R.id.YourTextId , keys);