ListView showing only one element - android

Here is my layout activity layout:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp">
// There are lots of views in here
</FrameLayout>
<include layout="#layout/story_entry_children_listview"/>
</LinearLayout>
Here is the included layout:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="#+id/story_entry_children_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</merge>
My list adapter is correct because If I apply on another ListView of another activity layout it's showing all elements are showing. Probably there might have a problem in this activity layout. Please help.
Here is my Adapter Class:
public class ChildEntryListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<StoryEntry> mListData;
private LayoutInflater mLayoutInflater;
public ChildEntryListAdapter(Context context, ArrayList<StoryEntry> listData) {
mContext = context;
mListData = listData;
if(mListData == null) mListData = new ArrayList<>();
mLayoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mListData.size();
}
#Override
public Object getItem(int i) {
return mListData.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
convertView = (new StoryEntryChildView(mContext));
}
return convertView;
}
}

Related

basic gridview android problem. It does not show anything

i am currently in the middle of making a basic gridview with custom layout. although i am only showing text in the gridview for now. i follow some tutorial online and follow everything step by step, i dont get any error from the android studio and i run it. But when i run it, it only show a blank white page. i did initialize the adapter this time, but it still show nothing. help what did i miss in this.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:id="#+id/firsttext"
android:layout_width="match_parent"
android:layout_height="#dimen/ButtonSize"
android:text="#string/hello"
android:gravity="center"/>
<GridView
android:layout_below="#+id/firsttext"
android:id="#+id/firstList"
android:layout_width="match_parent"
android:layout_height="200dp"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth">
</GridView>
</RelativeLayout>
gridlist.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="match_parent">
<TextView
android:id="#+id/gridtext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello"
android:textSize="#dimen/ButtonSize" />
</RelativeLayout>
gridadapter.java
public class gridAdapter extends BaseAdapter {
Context context;
String[] names;
LayoutInflater layoutInflater;
public gridAdapter(Context context,String[] names) {
this.context = context;
this.names = names;
}
#Override
public int getCount() {
return names.length;
}
#Override
public Object getItem(int position) {
return names[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View gridView = convertView;
if (convertView != null){
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = layoutInflater.inflate(R.layout.gridlist,null);
}
TextView textView = (TextView)gridView.findViewById(R.id.gridtext);
textView.setText(names[position]);
return gridView;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
String[] Names = {"great","good","average","great","good","average","great","good","average","great","good","average","great","good","average","great","good","average"};
GridView grid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridView)findViewById(R.id.firstList);
gridAdapter adapter = new gridAdapter(this,Names);
grid.setAdapter(adapter);
}
}
Don't use match_parent in your gridlist and TextView's layout_heiht repleace it by wrap_content and set special color for your TextView except for white color.
Change this line to: in gridadapter:
if (convertView == null) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = layoutInflater.inflate(R.layout.gridlist, parent, false);
}
In adapter change this :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= layoutInflater.inflate(R.layout.gridlist,null);
TextView textView = (TextView)convertView.findViewById(R.id.gridtext);
textView.setText(names[position]);
return convertView;
}
Use RecyclerView instead of GridView in future. Your problem is with setting color for TextView, check to set textColor to black

Why would a RecyclerView not show on the device but be visible in Layout Inspector?

I want to display a slide with pictures on my app and I implemented a little horizontal RecyclerView.
The screen on the left is a runtime screenshot from the same screen on the right which is shown in Layout Inspector, after I added a photo to the recyclerview
The RecyclerView's original place is inside the CardView and I moved it out because it didn't show there either. Any ideas of why?
Some code:
activity.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TheActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="TheFragment"
android:tag="#string/the_tag"
tools:layout="#layout/the_layout"
android:id="#+id/the_id"/>
</FrameLayout>
fragment.xml
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto"
tools:context="TheActivity" >
<data>
<!-- Some vars -->
</data>
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView android:visibility="visible"
android:id="#+id/frag_add_property_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView android:id="#+id/the_rv"
android:layout_width="match_parent"
android:layout_height="150dp"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:orientation="horizontal"
android:layout_marginBottom="#dimen/activity_half_vertical_margin"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
public class TheFragment extends BaseFragment implements TheView {
private PictureFilesAdapter adapter;
#BindView(R.id.rv_add_property_pics)
RecyclerView imageRv;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
bind = DataBindingUtil.inflate(inflater, getLayout(), container, false);
unbinder = ButterKnife.bind(this, bind.getRoot());
Drawable dividerDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.rv_item_hor_divider);
imageRv.addItemDecoration(new CustomItemDecoration(dividerDrawable));
return bind.getRoot();
}
class PictureFilesAdapter extends RecyclerView.Adapter<PropertyPicturesViewHolder> {
Context context;
public ArrayList<File> files;
public ArrayList<File> getFiles() {
return files;
}
AddPropertyView view;
PictureFilesAdapter(AddPropertyView view, Context context) {
this.context = context;
this.files = new ArrayList<>();
this.view = view;
}
#Override
public PropertyPicturesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item_rv_add_property, parent, false);
return new AddPropertyFragment.PropertyPicturesViewHolder(v);
}
#Override
public void onBindViewHolder(PropertyPicturesViewHolder holder, int position) {
holder.textView.setText(files.get(position).getAbsolutePath());
Picasso.with(context)
.load(files.get(position))
.into(holder.imageView);
}
#Override
public int getItemCount() {
return files.size();
}
void addFile(File file) {
this.files.add(file);
notifyDataSetChanged();
}
}
class PropertyPicturesViewHolder extends RecyclerView.ViewHolder {
private Uri uri;
#BindView(R.id.item_rv_add_prop_image) ImageView imageView;
#BindView(R.id.item_rv_add_prop_image_src) TextView textView;
PropertyPicturesViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
view.setOnClickListener((v) -> {
// some logic
});
}
}
}
Turns out the bitmap was too large even for Picasso. Downscaling the bitmap prior to showing it did the magic.
Can you provide your code?
There are several possibilities why it isn't showing up.
It has no layout manager
It has no adapter
The adapter.getCount() return 0

Android ListView displays only first element of List<string passed to it

public class MyAdapter extends BaseAdapter implements ListAdapter {
private List<String> events = new ArrayList<String>();
private Context context;
public MyAdapter(List<String> events, Context context) {
this.events = events;
this.context = context;
String a = String.valueOf(events.size());
Toast.makeText(context,a,Toast.LENGTH_LONG).show();
}
#Override
public int getCount() {
return events.size();
}
#Override
public Object getItem(int position) {
return events.get(position);
}
#Override
public long getItemId(int position) {
//return events.get(position).getId();
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item_layout, null);
}
TextView listtv = (TextView) view.findViewById(R.id.label1);
listtv.setText(events.get(position));
Button delbutton = (Button) view.findViewById(R.id.del_button);
delbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"You didn' code this feature yet",Toast.LENGTH_LONG).show();
}
});
return view;
}
}
I am creating an object of MyAdapter and calling it by passing a List and Activity.this
when i try to toast the size of the list passed i get the proper size
however only the first item of the list gets displayed on the screen
MY Xml`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#00000000">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/label1"
android:padding="10dp"
android:textSize="15dp"
android:textStyle="bold"
android:gravity="center"
>
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/del_button"
android:background="#00000000"
android:drawableTop="#drawable/ic_close_black_24dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingTop="10dp"
>
</Button>
</RelativeLayout>`
and my main layout file is
<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:id="#+id/content_cal_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.test123.CalView"
tools:showIn="#layout/activity_cal_view">
<ListView
android:id="#+id/list_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</RelativeLayout>
public static class ViewHolder{
public TextView listTV;
public Button delButton;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item_layout, null);
//Edited this part
holder = new ViewHolder
holder.listTV = (TextView) view.findViewById(R.id.label1);
holder.delbutton = (Button) view.findViewById(R.id.del_button);
view.setTag(holder);
} else holder=(ViewHolder)view.getTag();
listtv.setText(events.get(position));
delbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"You didn' code this feature yet",Toast.LENGTH_LONG).show();
}
});
return view;
}

Android parse two array into ListView

my ListView extends from ListFragment and after define subClass for customAdapter extends from BaseAdapter could not parse two array to layout elements.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
</LinearLayout>
setListAdapter function:
public class ResivedSMS extends ListFragment {
.
.
.
public ResivedSMS() {
testArray1 = new String[] {
"1111111111",
"2222222222",
"3333333333",
"4444444444",
"5555555555",
"6666666666",
};
testArray2 = new String[] {
"AAAAA",
"BBBBB",
"CCCCC",
"DDDDD",
"FFFFF",
"GGGGG",
};
}
.
.
.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewResivedSMSDetailes customListView = new ViewResivedSMSDetailes(getActivity(),testArray1,testArray2);
setListAdapter( customListView ); //call the method if listFragment
}
.
.
.
class ViewResivedSMSDetailes extends BaseAdapter
{
private LayoutInflater inflater;
private String[] values1;
private String[] values2;
private class ViewHolder {
TextView txt1;
TextView txt2;
}
public ViewResivedSMSDetailes(Context context,String[] values1,String[] values2)
{
this.values1=values1;
this.values2=values2;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return values1.length;
}
#Override
public Object getItem(int index) {
return values1[index];
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if(convertView ==null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_fragment, null);
holder.txt1 = (TextView)convertView.findViewById(R.id.txt1);
holder.txt2 = (TextView)convertView.findViewById(R.id.txt2);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.txt1.setText(values1[position]);
holder.txt2.setText(values2[position]);
return convertView;
}
}
UPDATED POST:
list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
</LinearLayout>
logCat Result:
08-24 14:48:03.245 851-851/ir.tsms E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at ir.tsms.ResivedSMS$ViewResivedSMSDetailes.getView(ResivedSMS.java:105)
ResivedSMS.java:105 is:
holder.txt2.setText(values2[position]);
From what it looks when you are inflating the view in the getView() callback method you are not setting the ViewGroup parent.
Try changing the following line:
convertView = inflater.inflate(R.layout.list_fragment, null);
To:
convertView = inflater.inflate(R.layout.list_fragment, parent,false);
And I would suggest creating a shared class of both of the views, and have a single data set and not two data sets to avoid index out of bounds exceptions.

Display data vertically in ListView

I have created an android application in that I want to display some Image data vertically in ListView.
How to make it possible ?
I tried using this code-
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_above="#+id/relativeLayout2"
android:layout_below="#+id/relativeLayout1"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
app:layout_gravity="fill_vertical"
android:entries="#array/country_image_list" >
</ListView>
</LinearLayout>
public class ItemImagesAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> paths;
private ImageLoader iml;
public ItemImagesAdapter(FragmentActivity activity, ArrayList<String> image_paths) {
context = activity;
paths = image_paths;
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity).build();
ImageLoader.getInstance().init(config);
iml = ImageLoader.getInstance();
}
#Override
public int getCount() {
return paths.size();
}
#Override
public Object getItem(int position) {
return paths.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_image, parent, false);
}
String path = paths.get(position);
ImageView image = (ImageView) convertView.findViewById(R.id.image1);
iml.displayImage(path, image);
return convertView;
}
}
take one more layout for list item as R.layout.list_item_image.
here i am taking image loader to load the images from server.
if you are getting images by means local...then set them directly
hope this may helps you
You can try to manage your requirement using TwoWayView for Horizontal scrolling of ListView items with Vertical scrolling. You can aslo check this demo for the same on github.
XML for first activity
<LinearLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".FirstActivity"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
XML for row layout
<?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:orientation="vertical"
>
<ImageView
android:id="#+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Create this layouts and then using custom adapter class you can fill the images for each row in image view...Something like this
Custom Adapter class
public class customAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String,String>>();
Activity activity;
private static LayoutInflater inflater = null;
public customAdapter(Activity a, ArrayList<HashMap<String, String>> al) {
Log.e("check", "");
activity = a;
this.al = al;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
imageLoader=new ImageLoader(activity.getApplicationContext());
View vi=convertView;
if(convertView==null)
{
vi = inflater.inflate(R.layout.row_layout, null);
}
ImageView myImage = (ImageView) vi.findViewById(R.id.myImage);
imageLoader.DisplayImage(//your image from resource here),myImage);
return vi;
}
}
Any problem in this you can follow the link of the tutorial I have mentioned in the comments...

Categories

Resources