0

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

        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() {

这里是 vlues

            @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" />

和编辑文本:

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

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

4

1 回答 1

0

你的列表视图在哪里?我建议您将数组定义为全局变量,然后在单击按钮时将每个双精度值分配给该数组。然后使用该数组像这样填充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

于 2013-08-20T16:39:21.670 回答