Reputation: 2446
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
Upvotes: 0
Views: 1066
Reputation: 86948
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;
}
Upvotes: 3