1

Listとを持つを作成しようとしてTextViewいますSpinnerTextViewおよびのデータSpinnerは、配列または 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);
    }
}

データホルダー クラス:

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

メイン .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>

行ビュー.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、配列の with データを変更できず、その値を取得SpinnerできないことTextViewです。どうすればいいですか?

4

0 に答える 0