0

I am trying to add a button in the middle of my listView. Ideally The button will split the listView and it will continue afterward, but if this is not possible I will be ok with a button inside a row in the listView. For example. My list view will have line one (image + text) , line two ( image + text) , button, and go on with the list view.

I have wrote the following code. This adds a button to a row in listView, but on the way it also adds an empty button (a button with now text) to every row in my listView. In addition the gravity setting for center is not working.

My xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

     <ImageView android:id="@+id/imgUserIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:scaleType="fitStart" />

     <Button
         android:id="@+id/buttonShowHide"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:gravity="center"
         android:text="@string/showHide" />

     <TextView android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" 
                    android:layout_marginTop="5dp"
            />

</LinearLayout>

my adapter

public class UserAdapter extends ArrayAdapter<UserAccountData> {
    Context context;
    int layoutResourceId;
    User data[] = null;

    public UserAdapter(Context context, int layoutResourceId,
            UserAccountData[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        UserAccountDataHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new UserAccountDataHolder();
            holder.imgIcon = (ImageView) row.findViewById(R.id.imgUserIcon);
            holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
            holder.showHide = (Button) row.findViewById(R.id.buttonShowHide);


            row.setTag(holder);
        } else {
            holder = (UserHolder) row.getTag();
        }

        User user = data[position];
        holder.txtTitle.setText(user.title);    
        holder.imgIcon.setImageResource(user.icon);
        holder.showHide.setText(user.buttonName);
        return row;
    }

    static class UserHolder {
        ImageView imgIcon;
        TextView txtTitle;
        Button showHide;    
    }
}

My Java object for the row. I have created two constructors one for the button and one for the image and text.

public class UserAccountData {
    public int icon;
    public String type;
    public String title;
    public CharSequence buttonName;
    public UserAccountData(){
        super();
    }
    // for image and text
    public UserAccountData(int icon, String title, String type) {
        super();
        this.icon = icon;
        this.title = title;
        this.type = type;
    }
    // for button
    public UserData(CharSequence buttonName, String type) {
        super();
        this.buttonName = buttonName;
        this.type = type;
    }

    public void setType(String type){
        this.type = type;
    }
}

In my activity I am adding the following two rows to the array , that later my adapter will use to create the listView ( I am passing it an ArrayList that being changed into an Array)

user_data.add(new UserAccountData(icon, "title,"type"));
user_data.add(new UserAccountData("show Password","button"));

a) is there a way to split the listView and the middle and just add a button? and continue the same listView? Because my current solution tries to add a button to a row.

b) any ideas why I am actually also adding an empty button to the icon, title type row? I am getting icon, title, empty button on my actual listView

Thank you very much

UPDATE: Found two blogs http://logc.at/2011/10/10/handling-listviews-with-multiple-row-types/ and http://android.amberfog.com/?p=296 , but still don't have any luck. Would appreciate some more in depth help

1 Answer 1

3

is there a way to split the listView and the middle and just add a button? and continue the same listView? Because my current solution tries to add a button to a row.

If I understand your question you want something like:

My list view will have:

  • image + text
  • image + text
  • button
  • image + text

etc...

You can have more than one type of row layout if you override getViewTypeCount() and getItemViewType().

  • getViewTypeCount() should return the number of types, in this case 2.
  • getItemViewType(int position) will return which type the row at position is, in this case either 0 or 1.

Addition

I don't really know how to make the distinction between the image text row and the button. I tried to find a way to see if my image is null (using the 2nd constructor) , but this does not seems to work

This sounds like a good approach, but since icon is an int it will never be null, the default value for an uninitialized integer is 0. Try:

@Override
public int getItemViewType(int position) {
    UserAccountData data = getItem(position);
    if(data.icon == 0)
        return 1;
    return 0;

    // The same thing in one line:
    //return getItem(position).icon == 0 ? 1 : 0;
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hey Sam, thanks that is what I am trying to get. I am new to android and was wondering if you could give me some examples or maybe suggested a good tutorial for this type of task that I am trying to accomplish? ( I have done the basic tutorials of picture + text)
I wrote out a simple example for another question a while back. Otherwise this website has a good, non-trivial example.
I just noticed your update, don't use the first example from logc.at that is terribly inefficient. The one from amberfog.com is great though, those diagrams make it better than the link I posted. What specifically are you having trouble with?
There are few thing I am having trouble with. a) I don't really know how to make the distinction between the image text row and the button. I tried to find a way to see if my image is null (using the 2nd constructor) , but this does not seems to work b) I looked at some stack overflow posts on getViewTypeCount and getItemViewType. I was not really sure how to implement them in my code. Mainly the getItemViewType , because in my case I don't know how many rows are going to be before the button. I will look at the example you posted in the comment and report back
Looking at your example I am now more clear on what I don't understand :). In my case I don't know how many rows will be before a button, I just know what would be the last row before that. I do not know how to use getViewTypeCount and how to figure out if the next row should be a button or not.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.