作为初学者成功地创建了一个自定义列表。这是自定义列表的java类:我确实实现了OnClickListener
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;
}
这是我的点击,我不知道该怎么做。我一直在将值带到 textview,现在我想将值从 edittext 带入自定义列表视图
@Override
public void onClick(View v) {
if(v.getId() == R.id.buttonConvertHectar){
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+ "";
}
这是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以及一个按钮,或者如果我能自动获得结果会更好。 这就是我希望它与 listview 一起使用的方式。单击