android rootview malfunctionning - android

I'm creating a fragment that contains two listviews ,the two in a linearlayout that has as orientation:horizontal;
the second listview is in a horizontalscrollview;
the problem is in the second listview;
I should have in each row of the listview 6 textview there's one 100 textview in the first row. The other rows have the correct number of textviews.
When I was debugging, I deduced that the problem is in my adapter in the getview function, I'm not sure what the exact issue is.
Here is the adapter of the listview that I'm referring to:
#Override
public View getView(int position,View convertView,ViewGroup parent)
{
View row = convertView;
if(row==null)
{
row =inflater.inflate(R.layout.list_live_2,parent,false);
}
List<String> stages = getItem(position);
LinearLayout linear_layout= (LinearLayout) row.findViewById(R.id.layout_stages);
LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 71);
layoutparams.setMargins(0,0,10,0);
for(int j=0;j<stages.size();++j)
{
TextView textView =new TextView (row.getContext());
textView.setText(stages.get(j));
textView.setLayoutParams(layoutparams);
linear_layout.addView(textView);
}
return row;
}

Related

List of TextViews in Listview

I have a question about List in Listview,
what I want to achieve is to create a listView with multiple TextViews in each item(like another listview)
I don't know how many elements would be in each items
From what I know creating listview in listview is a bad idea
So I want to create a normal listview (with item1 / item 2 / item 3..)
inside each row add linearlayout and populate it with textviews
I made this, but in every iteration it adds new linearlayout, how to change that? I want to add only textviews inside custom LinearLayout (R.layout.suplerowlist)
Or maybe is a better way to achieve that?
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
String menu_opcja = getItem(position);
if (convertView==null){
convertView = inflater.inflate(R.layout.suplementacja_row, null);
holder = new ViewHolder();
holder.suplement = (TextView) convertView.findViewById(R.id.nazwa_suplementu);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
((LinearLayout) convertView).removeAllViews();
}
holder.suplement.setText(menu_opcja);
for (int i = 0; i < menu.length; i++) {
View holders = inflater.inflate(R.layout.suplerowlist, parent, false);
TextView textViewTitles = (TextView) holders.findViewById(R.id.nazwa_supla);
textViewTitles.setText(menu[i]);
((LinearLayout) convertView).addView(holders);
}
return convertView;
}
Using ExpandableListView would be the best solution for this type of situation see this tutorial on how to use expandable list view.

Items being added to a list item multiple times. How do I fix this?

I am trying to create a listview with each list item is different and their layouts are created by code. The initial layouts look fine, but when i scroll out and in, all the layout items added programatically are added again, resulting duplicate items. How can I solve that problem?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
MyHolder holder;
if (v == null) {
LayoutInflater li = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.empty_item, null);
holder = new MyHolder(v);
v.setTag(holder);
} else {
holder = (MyHolder) v.getTag();
}
TextView textView = new TextView(activity);
textView.setText(items.get(position).getItems().get(i).getText());
textView.setLayoutParams(params);
holder.linearLayout.addView(textView);
return v;
}
class MyHolder{
public TextView tvTitle;
public LinearLayout linearLayout;
public CardView cardView;
public MyHolder(View base){
tvTitle = (TextView)base.findViewById(R.id.tvTitle);
cardView = (CardView)base.findViewById(R.id.card_view2);
linearLayout = (LinearLayout)base.findViewById(R.id.linearLayoutCard);
}
}
I'm not even sure how this code is running, because you have no return statement in your getView() method. But you need to return the convertView in getView() so that the ListView can reuse those when it needs to. Otherwise, it'll just keep asking for new views every time it needs them. So you would just put return v; at the end of getView().
Additionally, you are creating and adding new TextViews to your MyHolder objects outside of the if (v == null) block. Normally you would instantiate new views like this if the convertView is null. If it isn't, you just pass it through to the ListView or make updates to it before passing it back. So what's happening is, the convertView is available (not null), but instead of using it, you're adding a brand new TextView instead, which is why you are getting duplicates.

Android - add Layout views into ListView

the problems is, that I wan't to make ListView with elements which are containing image, description and two buttons. I'm making them in my own BaseAdapter extension, but fragment which is containing ListView is closing (wihtout errors in logcat..). I've found, that ListView is working well, when I'm not returning layout-type elements. So there is my sample with 'sample linear layout', which is not working.. Is there any possibility, to show layouts in ListView?
Here is my code:
Creating part:
lv = (ListView) getView().findViewById(R.id.main_wall_ambajes_lv);
AmbajAdapter aa = new AmbajAdapter(getActivity().getApplicationContext(), StaticData.ambajes);
lv.setAdapter(aa);
My getView method from adapter:
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
ImageView iv = new ImageView(getActivity());
iv.setImageBitmap(placeholderBitmap);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
return ll;
}
I don't know why you don't have any error however I don't think you proceed the correct way.
Usually you create the layout in the xml file of the layout folder and only inflate it in the getView(), for example as follow :
private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = mInflater.inflate(R.layout.your_custom_layout, parent, false);
}
//your code for setting the image or other things goes here
//for example if you have a textView
TextView textView = (TextView) view.findViewById(R.id.my_textview_id);
textView.setText("my custom text for this cell");
return (view);
}
and your_custom_layout is simply the xml file of your layout.
Note that for performance reason due to cell recycling I only inflate the view when it is null and I only read once the LayoutInflater context and put it in mInflater. However for the best performance you should use a ViewHolder, but it is out of the scope of your question.

Android Adapter with Irregular format/views for GridView

I'm making a custom Adapter, FieldAdapter, that extends BaseAdapter. I am created a GridView of LinearLayout views which contain a TextView and either a Spinner OR an EditText.
It seems that, after looking around a lot, that the optimized method is to use the ViewHolder method. But this seems to be optimized for a list of items that all share the same attributes.
What makes this a potential problem is the fact that I may have 4 views with a TextView and EditText, and another 5 that contain a TextView and a spinner so I can't assume a certain layout.
Is it optimal to have logic in my getView method that checks what kind of view needs to be returned for each item or is there a better way? Should I just have ViewHolder objects to hold the corresponding layouts? ie DropdownViewHolder and TextViewHolder?
Does this render the convertView useless?
The following is my code that works, but I've noticed that if I have a large list of items and scroll up and down quickly my entire list disappears which leads me to believe I'm not doing something correctly.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layoutSection = (LinearLayout) inflater.inflate(R.layout.dropdown, null);
TextView labelText = new TextView(this.context);
labelText.setText(data.get(position).getLabelText());
labelText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layoutSection.addView(labelText);
if (data.get(position).getFieldType().equalsIgnoreCase("dropdown")) {
Spinner spinner = new Spinner(this.context);
spinner.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// Options
final List<String> options = new ArrayList<String>();
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, options);
spinner.setAdapter(spinnerAdapter);
layoutSection.addView(spinner);
} else if (data.get(position).getFieldType().equalsIgnoreCase("text")) {
EditText textbox = new EditText(this.context);
textbox.setText("Hi");
textbox.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layoutSection.addView(textbox);
}
return layoutSection;
} else {
return convertView;
}
}
see those methods: getItemViewType & getViewTypeCount

Scrolling child view in gallery

Each item in my gallery is a custom view. One of the child's view is a gridView. When I'm scrolling the gallery everything works fine, but it wont scroll by touching the gridView. Its difficult to explain, I hope someone will understand me!
Touching and scrolling any part of the custom view suppose to trigger the scrolling? Or maybe only the imageView will trigger the scrolling?
public View getView(int position, View convertView, ViewGroup parent) {
final SubProduct subProduct=subProducts.get(position);
int quantity=subProduct.getQuantity();
int size=subProduct.getSizes().get(0).getWidth();
String productName=subProduct.getProductName();
int productPrice=subProduct.getSizes().get(0).getPrice();
int columnWidth = 0;
View view=convertView;
if (view==null){
holder=new SubProductHolder();
//The main container
holder.myLinearLayout= new LinearLayout(this.myContext);
holder.myLinearLayout.setOrientation(1);//vertical
//The custome button
LayoutInflater inflater = (LayoutInflater)
myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=(View)inflater.inflate(R.layout.custom_sub_product_layout, null);
LinearLayout btnLayout = (LinearLayout)v.findViewById(R.id.customSunProductButtonLayout);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(PRODUCT_BOTTUN_WIDTH,PRODUCT_BOTTUN_HEIGHT);
btnLayout.setLayoutParams(llp);
tvSubProductDescription=(TextView)v.findViewById(R.id.tvSubProductDescription);
tvSubProductPrice=(TextView) v.findViewById(R.id.tvSubProductPrice);
holder.btnProduct=btnLayout;
holder.btnProduct.setGravity(Gravity.CENTER);
holder.TvLinearLayout= new LinearLayout(this.myContext);
holder.TvLinearLayout.addView(holder.btnProduct);
//frame
holder.image = new ImageView(this.myContext);
holder.image.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(FRAME_WIDTH,FRAME_WIDTH);
holder.image.setLayoutParams(llp2);
//grid
holder.grid=new GridView(myContext);
//the number of rows is the round number of quantity sqaure
int numberOfColomns=(int) Math.floor((int) Math.sqrt(quantity));
holder.grid.setNumColumns(numberOfColomns);
holder.grid.setLayoutParams(new RelativeLayout.LayoutParams(PRODUCT_PICTURE_WIDTH,PRODUCT_PICTURE_WIDTH));
}else{
holder=(SubProductHolder)view.getTag();
}
//set text in description tv
tvSubProductDescription.setText(quantity+" "+productName+" "+size+"X"+size);
tvSubProductPrice.setText("$"+productPrice);
//set the grid
holder.grid.setAdapter(new emptySquaresAdapter(quantity,myContext,columnWidth,columnHeight));
holder.relativeGridLayout=new RelativeLayout(myContext);
holder.relativeGridLayout.addView(holder.image);
holder.relativeGridLayout.addView(holder.grid);
//set button and picture to layout
holder.myLinearLayout.addView(holder.relativeGridLayout);
holder.myLinearLayout.addView(holder.TvLinearLayout);
return holder.myLinearLayout;
}
static class SubProductHolder
{
ImageView image;
GridView grid;
RelativeLayout relativeGridLayout;
LinearLayout btnProduct;
LinearLayout TvLinearLayout;
LinearLayout myLinearLayout;
}
You have to use TouchListeners for galleryView since on clicking on its child view will not trigger scroll for galleryView
You have to use TouchListeners. Also, you're better off going with a ViewPager and using fragments. To my knowledge, Gallery is deprecated.

Categories

Resources