2

当我正在努力获取自定义微调器并在 toast 中显示值时。这些值正确设置为微调器,但我无法在吐司中显示它们。我的代码是

主.java

public class Main extends Activity {
ArrayList<String> ast = new ArrayList<String>();
Spinner customspinner;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    customspinner = (Spinner)findViewById(R.id.spinner1);

    ast.add("111111");
    ast.add("222222");
    ast.add("333333");
    ast.add("444444");

    MyAdapter adp = new MyAdapter();
    customspinner.setAdapter(adp);
    System.out.println(ast);
    customspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapter, View v,
                int position, long val) {
            // TODO Auto-generated method stub
    //here when spinner was selected I need to save those two custom textviews in two string and need to display them in toast....
            //String getspineer = customspinner.getSelectedItem().toString();
            //Toast.makeText(getApplicationContext(), "spinner  --- " + getspineer , Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub  
        }
    });
}


public class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return ast.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View v = null;
        LayoutInflater inflater = getLayoutInflater();
        v = inflater.inflate(R.layout.customspinner, null);
        TextView tv = (TextView)v.findViewById(R.id.textView1);
        TextView tv1 = (TextView)v.findViewById(R.id.textView2);
        String arlstdate[] = ast.get(position).split("~");
        for (int i = 0; i < arlstdate.length; i++) {
            tv.setText(arlstdate[i]);
            tv1.setText(arlstdate[i]);
        }
        return v;
    }
} 

}

我在这里寻找的是,当我选择微调器时,我需要将以下两个 textview 值保存在两个字符串中,并且需要分别在 Toast 混乱中显示它们。

谁能帮我这个。经过大量搜索后,我找到了此代码,但无法保存到字符串和显示中的文本值..

4

2 回答 2

0

抱歉,我并没有完全理解您的意思,但是作为指针,微调器上该位置的视图内容与您用于填充适配器的 ArrayList 上相同位置的字符串相同。

因此,您可以从 ArrayList 中获取值,而不是从 Spinner 视图中获取值。

    @Override
    public void onItemSelected(AdapterView<?> adapter, View v, int position, long val) {
        String getspineer = ast.get(position);
        Toast.makeText(getApplicationContext(), "spinner  --- " + getspineer , Toast.LENGTH_SHORT).show();
    }
于 2012-11-03T14:51:29.710 回答
0

为了显示 Toast 消息:

Toast.makeText(getApplicationContext(), "put strings here", Toast.LENGTH_SHORT).show();

从 TextView 获取文本:

String text = tv.getText();

但是,如果您只想在 Toast 中显示消息,则不需要 TextView,只需使用字符串数组即可。

抱歉,如果我没有正确理解您的问题。

于 2012-11-03T16:31:21.053 回答