我制作了2个集成微调器(每个类别的第一个微调器,每个类别的“值”第二个微调器),就像中一样。
我希望从第二个微调器中选择的“值”能够与EditText(数字)连接,以得到“值”的名称+“值”的总和,当按下“ TAMBAH”按钮时,该值将显示在ListView上。将listview归档后,“ SIMPAN”按钮会将数据从ListView保存到下一个活动。
从第二个微调器中选择的值+从EditText中的总数(数字)+“ TAMBAH”按钮=数据将从下面的列表视图中保存。
我的strings.xml
<resources>
<string-array name="kategori">
<item>TANAMAN BORDIR</item>
<item>TANAMAN LATAR</item>
<item>TANAMAN PELINDUNG</item>
<item>TANAMAN PEMANIS</item>
<item>LAINNYA</item>
</string-array>
<string-array name="tn_bordir">
<item>Aglonema</item>
<item>Amarastus</item>
<item>Ararea</item>
</string-array>
<string-array name="tn_latar">
<item>Aneka Kamboja</item>
<item>Bonsai Anting Putri</item>
<item>Bonsai Beringin Korea</item>
</string-array>
<string-array name="tn_pelindung">
<item>Angsana</item>
<item>Bambu Jepang</item>
<item>Bambu Krisik</item>
</string-array>
<string-array name="tn_pemanis">
<item>Agave Hijau</item>
<item>Agave Kuning</item>
<item>Agave Putih</item>
</string-array>
<string-array name="lainnya">
<item>Masih kosong bos</item>
</string-array>
</resources>
我的布局活动activity_buatnota.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".nota.buatnota">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="150dp"
app:srcCompat="@drawable/danisflora_horizontal" />
<Spinner
android:id="@+id/spn_kategori"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textAlignment="center"
android:background="@drawable/box" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="@+id/spn_tanaman"
android:layout_width="285sp"
android:layout_height="40dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/box"/>
<EditText
android:id="@+id/txt_banyak"
android:layout_width="63sp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:background="@drawable/box"/>
</LinearLayout>
<Button
android:id="@+id/btn_tambah"
android:layout_width="120sp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="8dp"
android:text="Tambah" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
</ListView>
<Button
android:id="@+id/btn_simpan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Simpan" />
</LinearLayout>
</ScrollView>
我的Java类buatnota.java
package com.abs.danisflora.nota;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.abs.danisflora.R;
import com.abs.danisflora.helper.DBHelper;
import java.util.List;
public class buatnota extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spin1, spin2;
private EditText txbanyak;
private Button bttambah;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buatnota);
spin1 = (Spinner) findViewById(R.id.spn_kategori);
spin2 = (Spinner) findViewById(R.id.spn_tanaman);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.kategori, android.R.layout.simple_spinner_item);
spin1.setAdapter(adapter);
spin1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if (spin1.getSelectedItem().equals("TANAMAN BORDIR")) {
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(getBaseContext(),
R.array.tn_bordir, android.R.layout.simple_spinner_item);
spin2.setAdapter(adapter1);
} else if (spin1.getSelectedItem().equals("TANAMAN LATAR")){
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(getBaseContext(),
R.array.tn_latar, android.R.layout.simple_spinner_item);
spin2.setAdapter(adapter1);
} else if (spin1.getSelectedItem().equals("TANAMAN PELINDUNG")){
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(getBaseContext(),
R.array.tn_pelindung, android.R.layout.simple_spinner_item);
spin2.setAdapter(adapter1);
} else if (spin1.getSelectedItem().equals("TANAMAN PEMANIS")){
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(getBaseContext(),
R.array.tn_pemanis, android.R.layout.simple_spinner_item);
spin2.setAdapter(adapter1);
} else {
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(getBaseContext(),
R.array.lainnya, android.R.layout.simple_spinner_item);
spin2.setAdapter(adapter1);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}