Android 从edittext获取值,并在自定义listview中设置该值

Android 从edittext获取值,并在自定义listview中设置该值,android,listview,android-edittext,Android,Listview,Android Edittext,作为一名初学者,他成功地创建了一个自定义列表。 以下是自定义列表的java类: 我实现了一个新的监听器 public class Area_Acre extends ListActivity implements OnClickListener { String[] stringarray = new String[7]; private EditText editAcre ; private ListView list ; private Button buttonConvertAcre; @

作为一名初学者,他成功地创建了一个自定义列表。 以下是自定义列表的java类: 我实现了一个新的监听器

public class Area_Acre extends ListActivity implements OnClickListener {
String[] stringarray = new String[7];
private EditText editAcre ;
private ListView list ;
private Button buttonConvertAcre;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.area_acre);

    Button buttonConvert = (Button) findViewById (R.id.buttonConvertAcre);
    EditText editAcre = (EditText) findViewById(R.id.editAcre);   
    buttonConvert.setOnClickListener(this);

    setListAdapter(new MyAdapter(this,
            android.R.layout.simple_list_item_1,
            R.id.textView_list,
            getResources().getStringArray(R.array.Area)));
}
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.custom_list, parent,false);
        String[] items = getResources().getStringArray(R.array.Area);

        ImageView iv = (ImageView) row.findViewById(R.id.imageView_list);
        TextView tv = (TextView) row.findViewById(R.id.textView_list);

        tv.setText(items[position]);

        if (items[position].equals("Acre")){
            iv.setImageResource(R.drawable.area_acre);
        }

        else if (items[position].equals("Hectar")){
            iv.setImageResource(R.drawable.area_hectar);
        }

        else if (items[position].equals("Square Inch")){
            iv.setImageResource(R.drawable.area_sinch);
        }

        else if (items[position].equals("Square KM")){
            iv.setImageResource(R.drawable.area_skm);
        }

        else if (items[position].equals("Square Meter")){
            iv.setImageResource(R.drawable.area_smeter);
        }

        else if (items[position].equals("Square Mile")){
            iv.setImageResource(R.drawable.area_smile);
        }

        else if (items[position].equals("Square Yard")){
            iv.setImageResource(R.drawable.area_syard);
        }

        return row;
    }
下面是xml:

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

    <View
        android:layout_width="fill_parent"
        android:layout_height="@dimen/viewline_hight"
        android:layout_marginBottom="@dimen/insideline_marginUpDonw"
        android:layout_marginLeft="@dimen/insideline_marginLeftRight"
        android:layout_marginRight="@dimen/insideline_marginLeftRight"
        android:layout_marginTop="@dimen/insideline_marginUpDonw"
        android:background="@color/inside_line" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="@dimen/linearlayout_marginhight"
        android:layout_marginLeft="@dimen/linearlayout_marginLeftRight"
        android:layout_marginRight="@dimen/linearlayout_marginLeftRight"
        android:gravity="center"
        android:orientation="horizontal" >

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

    <View
        android:layout_width="fill_parent"
        android:layout_height="@dimen/viewline_hight"
        android:layout_marginBottom="@dimen/insideline_marginUpDonw"
        android:layout_marginLeft="@dimen/insideline_marginLeftRight"
        android:layout_marginRight="@dimen/insideline_marginLeftRight"
        android:layout_marginTop="@dimen/insideline_marginUpDonw"
        android:background="@color/inside_line" />

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

    </ListView>
</LinearLayout>

下面是自定义列表视图xml:

    <ImageView
    android:id="@+id/imageView_list"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:src="@drawable/e_incalc" />

<TextView
    android:id="@+id/textView_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="TextView"
    android:textSize="@dimen/main_text_size" />

就这样。。。 谢谢你的帮助, 非常感谢

这就是我想要的。。。有edittext和listview以及一个按钮,或者如果我能自动得到结果会更好。

将“textview.Text”字符串添加到stringArray中

android中listview的名称和代码:-

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              android.R.layout.yourLayoutName, stringarray);


            // Assign adapter to ListView
            listView.setAdapter(adapter); 

            // ListView Item Click Listener
            listView.setOnItemClickListener(new OnItemClickListener() {

                  @Override
                  public void onItemClick(AdapterView<?> parent, View view,
                     int position, long id) {

                   //Your business logic goes here

                  }

             }); 
        }
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.yourLayoutName,stringarray);
//将适配器分配给ListView
setAdapter(适配器);
//ListView项目单击侦听器
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//你的商业逻辑在这里
}
}); 
}

我不确定是否收到了您的问题,但下面是如何使用onClick()方法

您已经定义了一个按钮
buttonConvertAcre
,因此在onClick方法中可以执行以下操作:

@Override
public void onClick(View v) {
    if(v == buttonConvertAcre) {        
        // now you want to receive the value of the edit text as an Double or Integer, right?
        String valueString = editAcre.getText();
        Double value = Double.valueOf(valueString);

        // now do your calculation
        value = value * 0.404686; // I would recommend to use static final variables for calculation

        // now you want to set the result to your TextView, right?
        // make sure your TextView is initialized, e.g.
        // TextView acreResultTextView = findViewById(R.id.acreResultTextView);
        // Note, if you like to change a TextView within your listAdapter, then you can
        // do the calculation in the adapter using the TextView in the adapter at its positon
        // in your case: R.id.textView_list
        acreResultTextView.setText(String.valueOf(value));
    }
}
注意,我在onClick中使用了
editAcre
,这要求您更改onCreate()方法以初始化此变量:

private EditText editAcre ;
private ListView list ;
private Button buttonConvertAcre;

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

    // now use your private variable defined in the class
    editAcre = (EditText) findViewById(R.id.editAcre);   
    buttonConvertAcre = (Button) findViewById (R.id.buttonConvertAcre);
    buttonConvertAcre.setOnClickListener(this);

    setListAdapter(new MyAdapter(this,
            android.R.layout.simple_list_item_1,
            R.id.textView_list,
            getResources().getStringArray(R.array.Area)));
}
这应该有助于你开始

致以最良好的祝愿, 迈克尔

[编辑]:以下是使用LinearLayout而不是ListView和list适配器时的解决方案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <Button
        android:id="@+id/convertButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="convert"
        android:layout_gravity="right"
        />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Acre:"
            android:layout_marginRight="10dp"
            />
        <EditText 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="enter value"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">
        <ImageView 
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/imageAcre"/>
        <TextView 
            android:id="@+id/textViewAcre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Acre:"/>
        <TextView 
            android:id="@+id/textViewAcreValue"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="1.0283"
            android:gravity="right"/>
    </LinearLayout>

    <!-- Now doing the same for Hectar ... -->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">
        <ImageView 
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/imageHectar"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hectar:"/>
        <TextView 
            android:id="@+id/textViewHectarValue"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="2.024"
            android:gravity="right"/>
    </LinearLayout>

    <!-- Now doing the same for all others. Simply copy the layout and rename the image, the text and the id -->     

</LinearLayout>


我不太理解你的问题。顺便说一句,对初学者来说格式不错(Y)。谢谢。。。我想从edittext(即editAcre)中获取值,并将结果发送到我的自定义listview。@Mothy..您好,我将尝试您的方法。。。我太慢了:)@Michael。。。你好,我想把结果带到listview:)我已经用textview完成了。。但是使用listview会更好更简单。。。我从一些教程中了解到,使用listview方法可以节省大量时间和布局。你的目标是什么?是否希望有一个EditText和一个convertButton,如果单击该按钮,则希望计算所有值(且值显示在listview中)??因为我不明白你为什么要使用列表视图,而不是线性布局,并使用固定的文本视图,例如TextViewResultToHector、TextViewResultToSquareKm等。你能发布你的应用程序的屏幕截图吗?编辑:我已经修复了上面的onCreate()方法,以使用正确的按钮
buttonConvertAcre.setOnClickListener(此)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <Button
        android:id="@+id/convertButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="convert"
        android:layout_gravity="right"
        />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Acre:"
            android:layout_marginRight="10dp"
            />
        <EditText 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="enter value"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">
        <ImageView 
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/imageAcre"/>
        <TextView 
            android:id="@+id/textViewAcre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Acre:"/>
        <TextView 
            android:id="@+id/textViewAcreValue"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="1.0283"
            android:gravity="right"/>
    </LinearLayout>

    <!-- Now doing the same for Hectar ... -->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">
        <ImageView 
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/imageHectar"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hectar:"/>
        <TextView 
            android:id="@+id/textViewHectarValue"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="2.024"
            android:gravity="right"/>
    </LinearLayout>

    <!-- Now doing the same for all others. Simply copy the layout and rename the image, the text and the id -->     

</LinearLayout>