如何使用 customadapter 在列表视图中添加带有列表项的微调器?

发布于 2024-12-04 18:22:57 字数 109 浏览 1 评论 0原文

我正在使用列表视图,并且有许多列表项。 我需要添加一个包含所有列表项的微调器。 我正在使用 customadapter,我的问题是添加我们可以使用 imageview 的图像。 对于旋转器我可以使用什么?

I am using a listview and there are number of listitems.
I need to add a spinner with all my listitems.
I am using customadapter and my problem is for adding a image we can use imageview.
For spinner what can i use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热情消退 2024-12-11 18:22:57

这是代码。

package com.android.main;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;

public class DemoListSpinnerActivity extends Activity {
    ListView _listview;
    String[] itemsarray=new String[]{"one","two","three"};
    ArrayAdapter<String> adapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,itemsarray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        _listview=(ListView)findViewById(R.id.listView1);
        _listview.setAdapter(new CustomAdapter(this));

    }

    private class CustomAdapter extends BaseAdapter
    {
        LayoutInflater inflater;
        public CustomAdapter(Context  context)
        {
            inflater=LayoutInflater.from(context);
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return 5;
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(int arg0, View convertview, ViewGroup arg2) {
            ViewHolder viewHolder;
            if(convertview==null)
            {
                convertview=inflater.inflate(R.layout.listrow,null);
                viewHolder=new ViewHolder();
                viewHolder.spinner=(Spinner)convertview.findViewById(R.id.spinner1);
                viewHolder.spinner.setAdapter(adapter);
                convertview.setTag(viewHolder);
            }
            else
            {
                viewHolder=(ViewHolder)convertview.getTag();
            }
            return convertview;
        }
        public class ViewHolder
        {
            Spinner spinner;
        }

    }
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ListView android:layout_height="wrap_content" android:id="@+id/listView1" android:layout_width="match_parent"></ListView>
</LinearLayout>
listrow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <Spinner android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/spinner1"></Spinner>

</LinearLayout>

Here is the code.

package com.android.main;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;

public class DemoListSpinnerActivity extends Activity {
    ListView _listview;
    String[] itemsarray=new String[]{"one","two","three"};
    ArrayAdapter<String> adapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,itemsarray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        _listview=(ListView)findViewById(R.id.listView1);
        _listview.setAdapter(new CustomAdapter(this));

    }

    private class CustomAdapter extends BaseAdapter
    {
        LayoutInflater inflater;
        public CustomAdapter(Context  context)
        {
            inflater=LayoutInflater.from(context);
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return 5;
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(int arg0, View convertview, ViewGroup arg2) {
            ViewHolder viewHolder;
            if(convertview==null)
            {
                convertview=inflater.inflate(R.layout.listrow,null);
                viewHolder=new ViewHolder();
                viewHolder.spinner=(Spinner)convertview.findViewById(R.id.spinner1);
                viewHolder.spinner.setAdapter(adapter);
                convertview.setTag(viewHolder);
            }
            else
            {
                viewHolder=(ViewHolder)convertview.getTag();
            }
            return convertview;
        }
        public class ViewHolder
        {
            Spinner spinner;
        }

    }
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ListView android:layout_height="wrap_content" android:id="@+id/listView1" android:layout_width="match_parent"></ListView>
</LinearLayout>
listrow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <Spinner android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/spinner1"></Spinner>

</LinearLayout>
箜明 2024-12-11 18:22:57

如果您使用布局 xml 构建行,请将 Spinner 放置在那里。如果您以编程方式进行,则可以使用 Spinner 类。

If you are constructing your row using a layout xml, place the Spinner there. If you are doing programmatically you can use Spinner class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文