listview adding spinner in each row?

浪子不回头ぞ 提交于 2019-12-11 23:16:44

问题


I am facing problem while putting Spinner in layout. My acctual requirement is to place Spinner once, at the top of layout.

This output I am getting:

I have Relative layout

   <Spinner
       android:id="@+id/spinner1"
        android:layout_width="fill_parent"
   android:layout_height="wrap_content" 
   android:entries="@array/string_array"
       android:prompt="@string/spinner_msg"
     />
     <TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/notesTextView" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" 
   android:textColor="@android:color/white"
   android:minHeight="?android:attr/listPreferredItemHeight"
   android:gravity="center_vertical"
   android:textAppearance="?android:attr/textAppearanceMedium"
</TextView>`

MyActivity class is extended by ListActivity here's onCreate method

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     listView=getListView();
     listView.setOnItemClickListener(listener);
     spinner1=(Spinner)findViewById(R.id.spinner1);
     getDetailsCursor();
    String[] from = new String[] {"db_column"};
    int[] to = new int[] { R.id.notesTextView};
            curAdapter=new SimpleCursorAdapter(MyActivity.this, R.layout.mylist, null, from, to);
            setListAdapter(curAdapter);
        registerForContextMenu(listView);
    }`

回答1:


Here is the code in which spinner inflates on the top of listview :

listView= (ListView) findViewById(R.id.list);

        LayoutInflater inflater = LayoutInflater.from(this);
        View mTop = inflater.inflate(R.layout.spin, null);
        authorityView.addHeaderView(mTop);

R.layout.spin is that layout which contains only spinner. And inside the List you have to inflate textView only. as you are doing in mylist.xml , just remove spinner from it and made seprate xml for spinner.

so spinner is once, at the top of layout (ListView).




回答2:


I have some difficulty making out what your actual question is, some layout.xml code would help here, too. I think, that you are placing the spinner inside the listitem.xml instead of the main.xml, so that it gets replicated for each item in your listview. Please share some code. Since you declare both the TextView and the Spinner in mylist.xml, you get both those elements in each Item of your List. If you only want one spinner, you shouldn't use a ListActivity, but instead create a normal Activity with a Spinner and a ListView in the Layout. Then define another layout to use for the list items (e.g. with only the TextView).



来源:https://stackoverflow.com/questions/15002821/listview-adding-spinner-in-each-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!