我有2行的列表视图,我希望第一行包含文本和微调器,第二行包含文本和editText ...有人可以帮我使用方法getview吗?
答案 0 :(得分:1)
- 编辑:对不起,我没注意到你也想要一个Spinner。如果它有任何用处,我将把我的帖子留给#getView()部分。 -
首先制作一个单独的.xml文件,其中包含res / layout文件夹中每个ListView项的布局属性,如下所示:
<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>
然后你必须创建一个新的ArrayAdapter并覆盖#getView():
@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;
}
请注意:这是#getView()的基本版本。
查看ViewHolder模式,了解有关处理获取视图的更有效方法。
答案 1 :(得分: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;
}