如何从EditText获取值到列表视图

时间:2013-08-20 16:21:21

标签: android listview

我在我的应用程序中有这个代码,这是一个小单位转换器

        setContentView(R.layout.area_acre);  

    final    EditText editAcre = (EditText) findViewById(R.id.editAcre);        
    final    EditText editHectar = (EditText) findViewById(R.id.editHectar);        
    final    EditText editSquareinch = (EditText) findViewById(R.id.editSquareinch);
    final    EditText editSquarekm = (EditText) findViewById(R.id.editSquarekm);
    final    EditText editSquaremeter = (EditText) findViewById(R.id.editSquaremeter);
    final    EditText editSquaremile = (EditText) findViewById(R.id.editSquaremile);
    final    EditText editSquareyard = (EditText) findViewById(R.id.editSquareyard);

这是我的按钮

    Button buttonConvert = (Button) findViewById (R.id.buttonConvertAcre);
    buttonConvert.setOnClickListener(new OnClickListener() {

这是线索

            @Override
        public void onClick(View v) {
            try {
                double acre = Double.valueOf (editAcre.getText().toString());
                double hectar = acre * 0.404686;
                double squareinch = acre * 6.273e+6;
                double squarekm = acre * 0.00404686;
                double squaremeter = acre * 4046.86;
                double squaremile = acre * 0.0015625;
                double squareyard = acre * 4840;

                editHectar.setText (String.valueOf(hectar));
                editSquareinch.setText (String.valueOf(squareinch));
                editSquarekm.setText (String.valueOf(squarekm));                    
                editSquaremeter.setText (String.valueOf(squaremeter));
                editSquaremile.setText (String.valueOf(squaremile));                    
                editSquareyard.setText (String.valueOf(squareyard));    

            } catch (NumberFormatException ex) {
                editHectar.setText ("");
            }

我想做什么是从EditText获取值到列表视图 我已经制作了一个字符串数组,我把它称为区域。 我做了listadapter,这是代码

        setListAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.Area)));

并且它不是自定义列表。 这是我的xml代码: 按钮:

        <Button
        android:id="@+id/buttonConvertAcre"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="5dp"
        android:minHeight="40dp"
        android:text="@string/Button" />

和edittext:

            <TextView
            android:id="@+id/textAcre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/Acre"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="@dimen/main_text_size" />

        <EditText
            android:id="@+id/editAcre"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="@dimen/layout_marginLeft"
            android:background="@android:color/transparent"
            android:gravity="right"
            android:hint="@string/IYN"
            android:inputType="number"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/red_set"
            android:textSize="@dimen/hint_text_size" />
    </LinearLayout>

以下是清单:

        <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
        </ListView>

我用这段代码得到了结果:             

        <EditText
            android:id="@+id/editHectar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="@dimen/layout_marginLeft"
            android:background="@android:color/transparent"
            android:editable="false"
            android:gravity="right"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="@dimen/hint_text_size" />

如果它可能我不想使用按钮进行转换我想要进行某种自动转换,比如我插入数字并自动获取值而不按下按钮。

坦克提前了很多。

这毕竟是我所做的,它还没有给我任何结果!!

public class Area_Acre extends ListActivity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.area_acre);

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, getResources()
            .getStringArray(R.array.Area)));
}
String[] stringarray = new String[7];

@Override
public void onClick(View v) {
    Button buttonConvert = (Button) findViewById (R.id.buttonConvertAcre);
    buttonConvert.setOnClickListener(this);
    switch(v.getId()) {
    case R.id.buttonConvertAcre:
        EditText editAcre = (EditText) findViewById(R.id.editAcre);     
        double acre = Double.valueOf (editAcre.getText().toString());
        double hectar = acre * 0.404686;
        double squareinch = acre * 6.273e+6;
        double squarekm = acre * 0.00404686;
        double squaremeter = acre * 4046.86;
        double squaremile = acre * 0.0015625;
        double squareyard = acre * 4840;

        stringarray[0] = acre + "";
        stringarray[1] = hectar+ "";
        stringarray[2] = squareinch+ "";
        stringarray[3] = squarekm+ "";
        stringarray[4] = squaremeter+ "";
        stringarray[5] = squaremile+ "";
        stringarray[6] = squareyard+ "";
        break;}
}

} 有人可以告诉我如何用一些代码和解释修复它...我正在努力学习......非常感谢

1 个答案:

答案 0 :(得分:0)

您的列表视图在哪里?我建议您将数组定义为全局变量,然后在单击该按钮时将每个double值分配给该数组。然后使用该数组填充listView,如下所示

全局变量double[] doublearray = new double[6]

@Override
    public void onClick(View v) {
        switch(v.getId){
             case: R.id.buttonConvertAcre:

            double acre = Double.valueOf (editAcre.getText().toString());
            double hectar = acre * 0.404686;
            double squareinch = acre * 6.273e+6;
            double squarekm = acre * 0.00404686;
            double squaremeter = acre * 4046.86;
            double squaremile = acre * 0.0015625;
            double squareyard = acre * 4840;

            doublearray[0] = acre + ""; 
            doublearray[1] = hectar;
            //do this for every value
             break;
}

然后将适配器设置为此

 setListAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, doublearray)

(如果双数组不起作用,它希望字符串数组通过执行我在评论中所述的方式将双数组更改为字符串数组)

编辑:

double[] doublearray = new double[6]更改为String[] stringarray = new String[6];

然后执行doublearray[0] = acre + "";

检查onclick我改变了它。

并且您正在使用onclick侦听器错误,请更改buttonConvert.setOnClickListener(new OnClickListener() {

buttonConvert.setOnClickListener(this);

并实现onclicklistener