如何在android中显示微调器和textview

时间:2013-08-10 18:49:52

标签: java android spinner android-spinner

我正在创建一个Android应用程序,我想在列表视图中显示微调器和textview.i为spinner和textview的值使用单独的xml类。对于以下代码示例,它仅显示textview中的值而不是值从spinner..how我可以显示来自微调器和textview..i的两个值; m对于android编程来说是全新的..

package com.example.dcsd;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class Result extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);
    setListAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.textView1, getResources().getStringArray(R.array.modules)));
    setListAdapter(new MyAdapter(this, android.R.layout.simple_spinner_dropdown_item,R.id.spinner1, getResources().getStringArray(R.array.grades)));

}
private class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=inflater.inflate(R.layout.item_layout, parent,false);
        String[] items=getResources().getStringArray(R.array.modules);
        TextView tv=(TextView) row.findViewById(R.id.textView1);        
        tv.setText(items[position]);
        Log.d("num", "lu");
        return row; 
    }
    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=inflater.inflate(R.layout.item_layout, parent,false);      
        String[] items=getResources().getStringArray(R.array.grades);
        Spinner sp = (Spinner) findViewById(R.id.spinner1);
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(Result.this, android.R.layout.simple_spinner_item,items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);
        Log.d("num", "huuuu");      
        return row;
    }
}
}

0 个答案:

没有答案