I tried to set up a custom ListView as a table to display Product Details. As you see in my Code I use a custom adapter and a additional xml file to populate the ListView. My problem is that the ListView ist empty. There are no items displayed and I do not see my mistake. Can you help me?
Fragment:
public class ProductDetail1Fragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_product_detail1,container,false);
HashMap<String,String> mapProduct = new HashMap<String,String>();
for(int i=0;i<10;i++) {
mapProduct.put("Key" + i, "Value" + i);
}
ListView listView=(ListView) view.findViewById(R.id.productListview);
ProductDetailAdapter adapter = new ProductDetailAdapter(getActivity(),mapProduct);
listView.setAdapter(adapter);
return view;
}
Adapter:
public class ProductDetailAdapter extends BaseAdapter {
private HashMap<String,String> list;
private Context context;
public ProductDetailAdapter(Context c, HashMap<String,String> list){
super();
this.context = c;
this.list=list;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView=inflater.inflate(R.layout.product_detail_data_row,null);
}
TextView textViewKey = (TextView)convertView.findViewById(R.id.productDataKey);
TextView textViewValue = (TextView)convertView.findViewById(R.id.productDataValue);
textViewKey.setText("tst");
textViewValue.setText("ddfadfs");
return convertView;
}
}
Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.parker.tfdeapp.ProductDetail2Fragment">
<ListView
android:id="#+id/productListview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Listview_row.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="match_parent">
<TextView
android:id="#+id/productDataKey"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold" />
<TextView
android:id="#+id/productDataValue"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
Add getCount method in your adapter
#Override
public int getCount() {
return list.size();
}
Add this Override methods in your adapter class
#Override
public int getCount() {
return list.size();
}
#Override
public ListData getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
Missing class implementations of BaseAdapter.
See the documentation
#Override
public int getCount() {
return list.size();
}
#Override
public ListData getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
Implementing getCount() only makes items visible. Because it provides
item counts.
1) Need to change ListView to match parent height (its bad performance):
<ListView
android:id="#+id/productListview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2) Just to override missing functions in your BaseAdapte:
#Override
public int getCount() {
return mList.size();
}
#Override
public ListData getItem(int index) {
return mList.get(index);
}
#Override
public long getItemId(int index) {
return index;
}
3) Use ViewHolder... its very good pattern
Related
I would like to create a simple populated GridView using BaseAdapter but I keep getting empty list - application runs but there are no Views displayed.
Below are all files are use for this task:
Main:
String[] items = {"Some", "items", "to", "display"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView grid = (GridView)findViewById(R.id.grid);
final TextView text = (TextView)findViewById(R.id.text);
MyAdapter adapter = new MyAdapter(getApplicationContext(), items);
grid.setAdapter(adapter);
MyAdapter:
LayoutInflater inflater;
Context context;
String[] myData;
public MyAdapter(Context context, String[] myData) {
this.context = context;
this.myData = myData;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return myData[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.grid, parent, false);
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(myData[position]);
return convertView;
}
activity_main.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:layout_height="match_parent"
tools:context="com.example.android.random.MainActivity">
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="40dip"
android:horizontalSpacing="5dip"
android:numColumns="auto_fit"
android:columnWidth="100dip"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</android.support.constraint.ConstraintLayout>
grid.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="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout>
I have checked in various sources countles times and I did not manage to find an error in my code. It would be amazing if someone would take a look at it and find a reason for empty activity as the reason is probably obvious.
Your getCount method always return 0. Return the number of elements in your myData array instead.
#Override
public int getCount() {
return myData.length;
}
You can try this my friend
public class MyAdapter extends BaseAdapter {
Context context;
String[] data;
public MyAdapter(Context context, String[] myData) {
this.context = context;
this.data = myData;
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return data[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.grid, null);
}
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(data[position]);
return convertView;
}
}
I am not able to achieve proper UI for card stack.
Please check this picture this is requirement which I have to fulfill soon.
This is image for Card Stack UI please refer this
I had used https://github.com/aaronbond/Swipe-Deck too but UI is not as per requirement I tried to customize also but not success.
Your help will be highly appreciated
Thank you.
Use the below library which is working perfectly.
https://github.com/flschweiger/SwipeStack
XML file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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:clipChildren="false">
<link.fls.swipestack.SwipeStack
android:id="#+id/swipeStack"
android:layout_width="320dp"
android:layout_height="240dp"
android:padding="32dp"/>
</FrameLayout>
Adapter Code
public class SwipeStackAdapter extends BaseAdapter {
private List<String> mData;
public SwipeStackAdapter(List<String> data) {
this.mData = data;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.card, parent, false);
TextView textViewCard = (TextView) convertView.findViewById(R.id.textViewCard);
textViewCard.setText(mData.get(position));
return convertView;
}
}
I wanted to do a generic ListAdapter, where instead of inserting predefined view structures, you can add different views in each list item.
I have my ListAdapter:
public class ListAdapter extends BaseAdapter {
private HashMap<Integer, Integer> listDataTypes; //For each element of the list, defines if its an ítem or a separator
private List<View> listData;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
public ListAdapter() {
listDataTypes = new HashMap<Integer, Integer>();
listData = new ArrayList<>();
}
public void addItem(final View item) {
listDataTypes.put(listData.size(), TYPE_ITEM);
listData.add(item);
notifyDataSetChanged();
}
public void addSeparator(final View item) {
listDataTypes.put(listData.size(), TYPE_SEPARATOR);
listData.add(item);
notifyDataSetChanged();
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public int getItemViewType(int position) {
return listDataTypes.get(position);
}
#Override
public View getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
View itemView = listData.get(position);
convertView = itemView;
}
return convertView;
}
}
Here is an example of a list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/tvInfo1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvInfo2"
android:layout_toLeftOf="#+id/tvInfo3"
android:layout_toStartOf="#+id/tvInfo3"
android:layout_toEndOf="#id/tvInfo1"
android:layout_toRightOf="#id/tvInfo1"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:id="#+id/tvInfo3"
android:layout_centerVertical="true"/>
</RelativeLayout>
</LinearLayout>
And how I use the list adapter:
protected void onCreate(Bundle savedInstanceState) {
...
ListAdapter listAdapter = new ListAdapter();
TextView tvHeader = new TextView(this);
tvHeader.setText("I am a header");
listAdapter.addSeparator(tvHeader);
for(int i=0; i<50; i++){
View itemView = layoutInflater.inflate(R.layout.layout_two_items_list_item, null);
TextView tvInfo1= (TextView) itemView .findViewById(R.id.tvInfo1);
TextView tvInfo2= (TextView) itemView .findViewById(R.id.tvInfo2);
TextView tvInfo3= (TextView) itemView .findViewById(R.id.tvInfo3);
tvInfo1.setText("Id " + i);
tvInfo2.setText("Info " + i);
tvInfo3.setText("Hour " + i);
listAdapter.addItem(itemView);
}
ListView listView = (ListView)findViewById(R.id.listview);
listView.setAdapter(listAdapter);
listView.setSelector(android.R.color.transparent);
}
But for some reason, when I scroll the list, some items start to float, other are not shown, other are repeated, other changes places, etc. What am I doing wrong?
This is how is shown (incorrectly)
It is a very bad practice what you are doing. Keeping an array of views in the memory. The whole purpose of a listview to minimise that. In order to avoid the repetition replace
if (convertView == null) {
View itemView = listData.get(position);
convertView = itemView;
}
return convertView;
with
return listData.get(position);
But if I were you I would seriously consider rewriting the way you are doing your listview.
Change your getView to something like:
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = listData.get(position);
return itemView;
}
I suspect that you're re-using old views when you don't really want to be.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the right procedure to implement a swipeable view as a row item in a vertical listview?
I tried googling, but I haven't found a good way of implementing this.
Here is a screenshot of my requirement.
In my opinion, the correct way to handle this behaviour is to override getViewTypeCount() and getViewItemType(), where the latter should return as type a normal and the swipeable, probably a ViewPager. The drawing part is not that tricky. I would rather expect issues in the Vertical/Horizontal scroll
Here is an example using ViewPager inside ListView. (Seems to work fine.)
I've also tried FragmentPagerAdapter and Fragments within the ListView, but that produced odd results.
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listview = (ListView) findViewById(R.id.listView);
MultiLayoutAdapter adapter = new MultiLayoutAdapter();
adapter.addItem(new Content("First", Content.TYPE_TEXT));
adapter.addItem(new Content("Second", Content.TYPE_TEXT));
adapter.addItem(new Content("Imageset 1", Content.TYPE_IMAGESET));
adapter.addItem(new Content("Third", Content.TYPE_TEXT));
adapter.addItem(new Content("Imageset 2", Content.TYPE_IMAGESET));
adapter.addItem(new Content("Fourth", Content.TYPE_TEXT));
listview.setAdapter(adapter);
}
/* Simple class for multi-type content */
private class Content {
private static final int TYPE_TEXT = 0;
private static final int TYPE_IMAGESET = 1;
private static final int TYPE_COUNT = TYPE_IMAGESET + 1;
private String mString;
private int mType;
public Content(String s, int type) {
mString = s;
mType = type;
}
public String getString() { return mString; }
public int getType() { return mType; }
}
/* Adapter supporting multiple layouts */
private class MultiLayoutAdapter extends BaseAdapter {
private ArrayList<Content> mData = new ArrayList<Content>();
private LayoutInflater mInflater;
public MultiLayoutAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final Content item) {
mData.add(item);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) { return mData.get(position).getType(); }
#Override
public int getViewTypeCount() { return Content.TYPE_COUNT; }
#Override
public int getCount() { return mData.size(); }
#Override
public String getItem(int position) { return mData.get(position).getString(); }
#Override
public long getItemId(int position) { return position; }
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = mInflater.inflate(R.layout.row_text, parent, false);
/* TODO: Recycle convertView! */
switch (getItemViewType(position)) {
case Content.TYPE_TEXT:
rowView = mInflater.inflate(R.layout.row_text, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView);
textView.setText(mData.get(position).getString());
break;
case Content.TYPE_IMAGESET:
rowView = mInflater.inflate(R.layout.row_imageset, parent, false);
ViewPager viewPager = (ViewPager) rowView.findViewById(R.id.viewPager);
viewPager.setAdapter(new ImagePagerAdapter(getApplicationContext()));
break;
}
return rowView;
}
}
}
ImagePagerAdapter.java
public class ImagePagerAdapter extends PagerAdapter {
int NumberOfPages = 3;
LayoutInflater mInflater;
int[] res = {
android.R.drawable.ic_menu_camera,
android.R.drawable.ic_menu_compass,
android.R.drawable.ic_menu_directions};
ImagePagerAdapter(Context c) {
mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() { return NumberOfPages; }
#Override
public boolean isViewFromObject(View view, Object object) { return view == object; }
#Override
public Object instantiateItem(ViewGroup container, int position) {
View v = mInflater.inflate(R.layout.image, container, false);
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
imageView.setImageResource(res[position]);
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
And here are the layouts for reference:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingLeft="16dp" android:paddingRight="16dp">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</RelativeLayout>
row_text.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="50dp"
android:id="#+id/textView"/>
</LinearLayout>
row_imageset.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</LinearLayout>
and image.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"/>
</LinearLayout>
I am having a custom GridView which displays data dynamically. The data is loaded, but scrolling seems not working. When I scroll down the gridview, the scroll bar moves up without retaining the scrolled position.
Following is my code for Custom GridView,
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<PropertiesList> myList;
public CustomAdapter(List<PropertiesList> paramList, Context paramContext)
{
this.myList = paramList;
this.context = paramContext;
}
public int getCount()
{
return this.myList.size();
}
public Object getItem(int paramInt)
{
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt)
{
return paramInt;
}
public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup)
{
View localView = View.inflate(this.context, R.layout.grid_content, null);
localView.setMinimumHeight((int)(0.3D * ((WindowManager)this.context.getSystemService("window")).getDefaultDisplay().getWidth()));
((TextView)localView.findViewById(R.prop_type)).setText(((PropertiesList)this.myList.get(paramInt)).PropertyAddress);
localView.findViewById(R.id.prop_type).setBackgroundResource(color.darker_gray);
((TextView)localView.findViewById(R.id.prop_price)).setText(((PropertiesList)this.myList.get(paramInt)).PropertyPrice);
localView.setId(paramInt);
((TextView)localView.findViewById(R.id.prop_type)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "PropertyID: "+myList.get(paramInt).PropertyID, Toast.LENGTH_SHORT).show();
}
});
return localView;
}
}
xml,
<GridView
android:id="#+id/camera_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:columnWidth="110.0dip"
android:gravity="center"
android:horizontalSpacing="5.0dip"
android:numColumns="auto_fit"
android:orientation="vertical"
android:scrollingCache="false"
android:stretchMode="columnWidth"
android:verticalSpacing="5.0dip"
android:scrollbars="vertical" />
Any wrong implementation in my code? Please help me out in solving this.
Thanks in advance!!
public Object getItem(int paramInt)
{
return Integer.valueOf(paramInt);
}
this method return statement change to
mylist.get(paramInt);`