i have list view with 2 rows, I would like that the first row contains a text and a spinner, and the second row contains a text and an editText...someone can help me with the method getview?
Asked
Active
Viewed 451 times
-2
-
a listview to do that? spinner inside a listview item? don't do that... it's a terrible UI/UX. If you only need 2 "rows", use a linear layout with a scrollview (if goes beyond the screen) – Mariano Zorrilla Sep 24 '15 at 13:59
2 Answers
1
--Edit: sorry, I didn't notice you wanted a Spinner in there as well. I'm leaving my post up for the #getView() part, should it be of any use. --
First of make a separate .xml file containing the layout properties of each ListView item in your res/layout folder like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hm"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"
android:id="@+id/text1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2"
android:id="@+id/text2" />
</RelativeLayout>
Then you have to create a new ArrayAdapter and override #getView() as such:
@Override
public View getView(final int position, View view, final ViewGroup parent) {
final LayoutInflater inflater = activity.getLayoutInflater();
view = inflater.inflate(R.layout.list_single, parent, false); //R.layout.list_single is what I named above .xml layout
TextView label = (TextView) view.findViewById(R.id.text1); //R.id.text1 is the id we gave the first TextView in the .xml layout
TextView txt = (TextView) view.findViewById(R.id.text2); //R.id.text2 is the id we gave the second TextView in the .xml layout
final Item details = list.get(position); //Item = object type you want to get information from, list = a List of the items that are in the ListView
label.setText(details.name); //sets the text
txt.setText(details.surname); //sets the text
return view;
}
Please note: this is a basic version of #getView().
Look into the ViewHolder pattern to read about a more efficient way to handle getting the views.

nbokmans
- 5,492
- 4
- 35
- 59
0
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
int posType = getItemViewType(position);
if (v == null) {
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (posType == 0) {
v = inflater.inflate(R.layout.layout1, parent, false);
}
else {
v = inflater.inflate(R.layout.layout2, parent, false);
}
}
return v;
}

Chaitanya Reddy
- 43
- 9
-
You can have textview and spinner in first layout and textview and edit text in another layout in above shared code – Chaitanya Reddy Sep 24 '15 at 22:28
-
-
If i will do it only in linear layout it will not seem like listview – Idan Ayash Sep 25 '15 at 08:44