Android Listview带有微调框和一个复选框 [英] Android Listview with spinner and a checkbox

查看:381
本文介绍了Android Listview带有微调框和一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手。我试图创建一个列表,其中包含微调框,编辑文本和复选框。微调框和复选框的数据来自数据库。我有以下文件。

  NewTransac类扩展ListActivity {

private PayDbAdapter mDbHelper;
private Spinner paySpinner;
private CheckBox mCheckBox;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.new_transac_listview);
mDbHelper = new PayDbAdapter(this);
mDbHelper.open();

populatedata();
}

private void populatedata(){

paySpinner =(Spinner)findViewById(R.id.payerspinner);
mCheckBox =(CheckBox)findViewById(R.id.paidforcheckboxname);

Cursor mCursor = mDbHelper.fetchAllTransactionValue();
startManagingCursor(mCursor);

//创建一个数组,以指定要在列表中显示的字段。
String [] from = new String [] {PayDbAdapter.KEY_NAME};

int [] to = new int [] {android.R.id.text1};
int [] cbto = new int [] {R.id.paidforcheckboxname};

//现在创建一个简单的游标适配器并将其设置为显示
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item,mCursor,from, );

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
paySpinner.setAdapter(adapter);

SimpleCursorAdapter cbAdapter =
new SimpleCursorAdapter(this,R.layout.show_new_transac_data,mCursor,from,cbto);
setListAdapter(cbAdapter);
}

列表视图xml

 < ListView android:id =@ android:id / list
android:layout_width =match_parent
android:layout_height = wrap_content
android:layout_weight =1
android:drawSelectorOnTop =false
android:textSize =14sp
/>

< TextView android:id =@ android:id / empty
android:layout_width =wrap_content
android:layout_height =wrap_content
android :text =@ string / no_friends
android:textSize =14sp
/>

< Button android:id =@ + id / confirmpay
android:text =@ string / confirm
android:layout_width =wrap_content
android:layout_height =wrap_content
android:gravity =center_vertical | center_horizo​​ntal
android:layout_gravity =center_vertical | center_horizo​​ntal | center>
< / Button>





p>

 < TextView 
style = attr / listSeparatorTextViewStyle
android:text =@ string / listSeparatorPay
android:layout_marginTop =5dip
android:layout_marginBottom =5dip
/>

< Spinner android:id =@ + id / payerspinner
android:layout_width =match_parent
android:layout_height =wrap_content drawSelectorOnTop =true
android:prompt =@ string / selectpayer
/>

< TextView android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =@ string / paytext
/>

&EditText android:id =@ + id / payamount
android:layout_width =match_parent
android:layout_height =wrap_content layout_weight =1
android:inputType =text
/>

< TextView
style =?android:attr / listSeparatorTextViewStyle
android:text =@ string / listSeparatorPayedFor
android:layout_marginTop =5dip
android:layout_marginBottom =5dip
/>

< CheckBox android:id =@ + id / paidforcheckboxname
xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =match_parent
android:layout_height =wrap_content
/>

< EditText android:id =@ + id / paidforamount
xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =match_parent
android:layout_height =wrap_content
android:inputType =number
/>



问题

我根据数据库中的字段数量获得了多个纺纱器,复选框和编辑文本。我看到我们不能为复选框设置适配器,因为我为微调器设置。
我需要获得一个微调器,一个编辑文本和多个复选框(数据库行的总数)。请帮助!

解决方案

编辑 - 请参阅评论,此解决方案可能不正确 >

我知道这个问题古老,但它是谷歌的第一个结果,我正在一个应用程序使用Spinners在ListView以及。我使用了来自此处的示例代码开始使用。我希望这个例子回答你的问题。我没有实现CheckBoxes,但他们非常相似的旋转 - 更容易,事实上。此示例具有带有TextView和微调框的ListView。每当用户更改微调框中的选择,TextView就会改变以反映这一点。



我将这个项目分成了3个类:




  • ListViewTestActivity - 主要活动

  • DataAdapter-扩展ArrayAdapter并用于显示ListView中的元素

  • DataHolder - 简单对象,只保存有关元素的一些信息。



我修改/创建了3个关键的Android XML文件: / p>


  • main.xml - 修改 - 主布局

  • rowview.xml - ListView中的每个元素

  • strings.xml - 修改 - 默认Android字符串文件



从底部开始,这个main.xml文件只包含一个ListView,而没有其他的:

 <?xml version =1.0encoding =utf-8?> 
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/androidandroid:orientation =vertical
android:layout_width =fill_parentandroid:layout_height = fill_parent>
< ListView android:id =@ + id / listView1android:layout_height =match_parentandroid:layout_width =match_parent/>
< / LinearLayout>

这里是rowview.xml。请记住,此视图对于ListView中的每一行都重复:

 <?xml version =1.0encoding = -8? 
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:orientation =horizo​​ntalandroid:layout_width =match_parent
android:layout_height =wrap_contentandroid:weightSum =1>
< TextView android:layout_width =wrap_content
android:layout_height =match_parentandroid:id =@ + id / text
android:layout_weight =0.5android:textSize =25sp/>
< Spinner android:layout_width =0dpandroid:layout_height =wrap_content
android:id =@ + id / spinandroid:prompt =@ string / choice_prompt
android:layout_weight =0.5/>
< / LinearLayout>

strings.xml文件。我添加的是一个数组的微调内容:

 <?xml version =1.0encoding =utf -8? 
< resources>
< string name =hello> Hello World,ListViewTestActivity!< / string>
< string name =app_name> ListViewTest< / string>
< string name =choice_prompt>选择一个选项< / string>
< string-array name =choices>
< item> Alpha< / item>
< item> Bravo< / item>
< item> Charlie< / item>
< / string-array>
< / resources>

现在有趣的东西。 ListViewActivity类:

  public class ListViewTestActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

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

DataHolder data = new DataHolder(this);
DataHolder data1 = new DataHolder(this);
DataHolder data2 = new DataHolder(this);
DataHolder data3 = new DataHolder(this);
DataHolder data4 = new DataHolder(this);

DataAdapter d = new DataAdapter(this,R.layout.rowview,new DataHolder [] {data,data1,data2,data3,data4});

listView.setAdapter(d);
}
}

这很简单,一个新的适配器,并将ListView的适配器设置为您所做的。这是DataHolder类:

  public class DataHolder {

private int selected;
private ArrayAdapter< CharSequence>适配器;

public DataHolder(Context parent){
adapter = ArrayAdapter.createFromResource(parent,R.array.choices,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}

public ArrayAdapter< CharSequence> getAdapter(){
return adapter;
}

public String getText(){
return(String)adaptor.getItem(selected);
}

public int getSelected(){
return selected;
}

public void setSelected(int selected){
this.selected = selected;
}

}

所有DataHolder类Spinner的适配器以及您可能希望为ListView中的每个条目存储的其他信息(例如,您可能希望存储是否已选中)。最后,应用程序的真正肉,DataAdapter类:

  public class DataAdapter extends ArrayAdapter< DataHolder> {

private Activity myContext;

public DataAdapter(Activity context,int textViewResourceId,DataHolder [] objects){
super(context,textViewResourceId,objects);
myContext = context;
}

//我们保留此ViewHolder对象以节省时间。当重新绘制时,它比findViewById()更快。
static class ViewHolder {
protected DataHolder data;
protected TextView text;
protected旋转;
}

@Override
public View getView(int position,View convertView,ViewGroup parent){
View view = null;

//检查这一行是否已经被绘了一次。
if(convertView == null){

//如果没有,设置一切:
LayoutInflater inflator = myContext.getLayoutInflater();
view = inflator.inflate(R.layout.rowview,null);

//为此行创建一个新的ViewHolder,并修改其数据和微调框:
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text =(TextView)view.findViewById(R.id.text);
viewHolder.data = new DataHolder(myContext);
viewHolder.spin =(Spinner)view.findViewById(R.id.spin);
viewHolder.spin.setAdapter(viewHolder.data.getAdapter());

//用于在用户更改Spinner选择时处理事件:
viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> arg0,View arg1,int arg2,long arg3){
viewHolder.data.setSelected(arg2);
viewHolder.text.setText(viewHolder.data.getText ());
}

@Override
public void onNothingSelected(AdapterView<?> arg0){
}

} ;

//更新TextView以反映Spinner中的内容
viewHolder.text.setText(viewHolder.data.getText());

view.setTag(viewHolder);

Log.d(DBGINF,viewHolder.text.getText()+);
} else {
view = convertView;
}

//这是每次ListView刷新时调用的方法
ViewHolder holder =(ViewHolder)view.getTag();
holder.text.setText(getItem(position).getText());
holder.spin.setSelection(getItem(position).getSelected());

return view;
}
}

这是最后一个应用程序的屏幕截图漂亮,但它的确工作):





就是这样!我希望我回答了你的问题,并帮助任何人谁绊倒它像我一样。如果要动态更改列表中的数据,请使用DataAdapter的 add() remove() code> get()和 set()方法。 要更改每个个别微调框的数据,您需要修改DataHolder类。 SpinnerAdapter是在那里创建的,所以您只需要根据数据库响应动态生成适配器。


I am a newbie to android development. I am trying to create a List which has a spinner, a edit text and a check box. The data for spinner and check box come from data base. I have the following files.

NewTransac class which extends ListActivity {

private PayDbAdapter mDbHelper;
private  Spinner paySpinner;
private CheckBox mCheckBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.new_transac_listview);
     mDbHelper = new PayDbAdapter(this);
     mDbHelper.open();

     populatedata();
}

private void populatedata() {

    paySpinner = (Spinner)findViewById(R.id.payerspinner);
    mCheckBox = (CheckBox)findViewById(R.id.paidforcheckboxname);

    Cursor mCursor = mDbHelper.fetchAllTransactionValue();
    startManagingCursor(mCursor);

    // Create an array to specify the fields we want to display in the list.
    String[] from = new String[]{PayDbAdapter.KEY_NAME};

    int[] to = new int[]{android.R.id.text1};
    int[] cbto = new int[]{R.id.paidforcheckboxname};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter adapter =
        new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, mCursor, from, to );

    adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    paySpinner.setAdapter(adapter);

    SimpleCursorAdapter cbAdapter =
        new SimpleCursorAdapter(this, R.layout.show_new_transac_data, mCursor, from, cbto );
    setListAdapter(cbAdapter);
}

The list view xml

<ListView android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    android:textSize="14sp"
/>

<TextView android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/no_friends"
    android:textSize="14sp"
/>

<Button android:id="@+id/confirmpay" 
    android:text="@string/confirm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:gravity="center_vertical|center_horizontal" 
    android:layout_gravity="center_vertical|center_horizontal|center">
</Button>

list view filled xml

<TextView
    style="?android:attr/listSeparatorTextViewStyle"
    android:text="@string/listSeparatorPay"
    android:layout_marginTop="5dip"
    android:layout_marginBottom="5dip"
/>

<Spinner android:id="@+id/payerspinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true"
    android:prompt="@string/selectpayer"
/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/paytext"
/>

<EditText android:id="@+id/payamount" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:inputType="text"
/>

<TextView
    style="?android:attr/listSeparatorTextViewStyle"
    android:text="@string/listSeparatorPayedFor"
    android:layout_marginTop="5dip"
    android:layout_marginBottom="5dip"
/>

<CheckBox android:id="@+id/paidforcheckboxname"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
/>

<EditText android:id="@+id/paidforamount"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
/>

Problem
I get multiple spinners, checkboxes and edittext based on the number of fields in the database. I see that we cannot set the adapter for the checkbox as i set for the spinner. I need to get only one spinner with one edit text and multiple checkboxes(total number of database rows). please help!

解决方案

EDIT - please see comments, this solution may not be correct

I know this question ancient, but it is the first result on Google and I am working on an application that uses Spinners in a ListView as well. I used some sample code from here to get started. I hope this example answers your question. I didn't implement the CheckBoxes but they're very similar to the Spinner - much easier, in fact. This example has a ListView with a TextView and a Spinner. Whenever the user changes a selection in the spinner, the TextView changes to reflect this.

I divided this project up into 3 classes:

  • ListViewTestActivity - main activity
  • DataAdapter - extends ArrayAdapter and works to display the elements in the ListView
  • DataHolder - simple object that just holds some information about the element. This could be implemented in many other ways to suit your needs.

There are also 3 key Android XML files I modified / created:

  • main.xml - modified - the main layout
  • rowview.xml - added - the layout for each element in the ListView
  • strings.xml - modified - the default Android strings file

To start from the bottom up, this main.xml file only contains a single ListView, and nothing else:

<?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">
    <ListView android:id="@+id/listView1" android:layout_height="match_parent" android:layout_width="match_parent" />
</LinearLayout>

And here is the rowview.xml. Remember that this view is duplicated for each row in the ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:weightSum="1">
    <TextView android:layout_width="wrap_content"
        android:layout_height="match_parent" android:id="@+id/text"
        android:layout_weight="0.5" android:textSize="25sp" />
    <Spinner android:layout_width="0dp" android:layout_height="wrap_content"
        android:id="@+id/spin" android:prompt="@string/choice_prompt"
        android:layout_weight="0.5" />
</LinearLayout>

The strings.xml file. All I added was an array for the contents of the spinner:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ListViewTestActivity!</string>
    <string name="app_name">ListViewTest</string>
    <string name="choice_prompt">Select a choice</string>
    <string-array name="choices">
        <item>Alpha</item>
        <item>Bravo</item>
        <item>Charlie</item>
    </string-array>
</resources>

Now for the fun stuff. The ListViewActivity class:

public class ListViewTestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        DataHolder data = new DataHolder(this);
        DataHolder data1 = new DataHolder(this);
        DataHolder data2 = new DataHolder(this);
        DataHolder data3 = new DataHolder(this);
        DataHolder data4 = new DataHolder(this);

        DataAdapter d = new DataAdapter(this, R.layout.rowview, new DataHolder[] { data, data1, data2, data3, data4 });

        listView.setAdapter(d);
    }
}

It's pretty simple, you just get the list, make a new adapter, and set the ListView's adapter to the one you made. This is the DataHolder class:

public class DataHolder {

    private int selected;
    private ArrayAdapter<CharSequence> adapter;

    public DataHolder(Context parent) {
        adapter = ArrayAdapter.createFromResource(parent, R.array.choices, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    public ArrayAdapter<CharSequence> getAdapter() {
        return adapter;
    }

    public String getText() {
        return (String) adapter.getItem(selected);
    }

    public int getSelected() {
        return selected;
    }

    public void setSelected(int selected) {
        this.selected = selected;
    }

}

All the DataHolder class does is hold the Spinner's adapter and whatever other information you might want to store for each entry in the ListView (you may want to store whether it is checked or not, for example). And finally the real "meat" of the app, the DataAdapter class:

public class DataAdapter extends ArrayAdapter<DataHolder> {

    private Activity myContext;

    public DataAdapter(Activity context, int textViewResourceId, DataHolder[] objects) {
        super(context, textViewResourceId, objects);
        myContext = context;
    }

    // We keep this ViewHolder object to save time. It's quicker than findViewById() when repainting.
    static class ViewHolder {
        protected DataHolder data;
        protected TextView text;
        protected Spinner spin;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;

        // Check to see if this row has already been painted once.
        if (convertView == null) {

            // If it hasn't, set up everything:
            LayoutInflater inflator = myContext.getLayoutInflater();
            view = inflator.inflate(R.layout.rowview, null);

            // Make a new ViewHolder for this row, and modify its data and spinner:
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.text);
            viewHolder.data = new DataHolder(myContext);
            viewHolder.spin = (Spinner) view.findViewById(R.id.spin);
            viewHolder.spin.setAdapter(viewHolder.data.getAdapter());

            // Used to handle events when the user changes the Spinner selection:
            viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    viewHolder.data.setSelected(arg2);
                    viewHolder.text.setText(viewHolder.data.getText());
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                }

            });

            // Update the TextView to reflect what's in the Spinner
            viewHolder.text.setText(viewHolder.data.getText());

            view.setTag(viewHolder);

            Log.d("DBGINF", viewHolder.text.getText() + "");
        } else {
            view = convertView;
        }

        // This is what gets called every time the ListView refreshes
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(getItem(position).getText());
        holder.spin.setSelection(getItem(position).getSelected());

        return view;
    }
}

Here's a screenshot of the final app (it's not very pretty, but it does work):

And that's it! I hope I answered your question and helped anyone else who stumbled upon it like I did. If you want to dynamically change the data in the list, use the DataAdapter's add(), remove(), get(), and set() methods. To change the data for each individual spinner, you need to modify the DataHolder class. The SpinnerAdapter is created there, so you just need to dynamically generate the adapters depending on the database response.

这篇关于Android Listview带有微调框和一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆