使用Spinner的Android ListView并获得其价值

时间:2012-06-04 10:46:38

标签: android listview textview spinner

我正在尝试创建一个List,其TextViewSpinnerTextViewSpinner的数据来自数组或JSON。我有以下文件:

主要活动:

public class ListViewTestActivity extends Activity {

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

        String coba="tes";
        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);
        DataHolder data5 = new DataHolder(this);

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

        listView.setAdapter(d);
    }
}

DataHolder类:

public class DataHolder {

    private int selected;
    private String name;
    private ArrayAdapter<CharSequence> adapter;
    String[] count={"0","1","2","3","4","5","6","7","8","9","10"};

    public DataHolder(Context parent) {
        //adapter = ArrayAdapter.createFromResource(parent, R.array.choices , android.R.layout.simple_spinner_item);
        //adapter= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,count);
        //this.name="tes";
        adapter = new ArrayAdapter<CharSequence>(parent, android.R.layout.simple_spinner_item, count);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    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;
    }
}

DataAdapter类:

public class DataAdapter extends ArrayAdapter<DataHolder> {

    private Activity myContext;
    String tes="makanan";

    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(tes);

            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;
    }
}

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

rowview.xml:

<?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>

我的问题是我无法使用数组数据更改TextView,并获得SpinnerTextView的值。我该怎么做?

0 个答案:

没有答案