2013-08-31 99 views
0

初學者成功創建自定義列表。 這裏是java類的自定義列表的: ,我也實現OnClickListener從edittext中獲取值並將其設置在自定義列表視圖中

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

就是這樣...... 得到任何幫助, 並非常感謝

this is how the app look like at the first place.. Click

這是我希望它是...用的EditText和列表視圖和一個按鈕,或者它會更好,如果我可以自動獲得的結果。 this is how i want it to be with listview..Click

+0

我不明白你的問題那麼這裏是一個解決方案。順便說一句,適合初學者的格式(Y)。 –

+0

謝謝...我想從編輯文本中獲取editAcre的值,並將結果導入到我的自定義列表視圖中。 – user2513275

回答

0

將您的「textview.Text」字符串添加到stringArray中。

與代碼的Android列表視圖: -

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 

        } 

      }); 
     } 
0

我不知道如果我得到你的問題,但這裏是你如何可以使用的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和列表適配器..

<?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> 
+0

@Mothy ..你好,我會嘗試你的方法...我很慢:) – user2513275

+0

@邁克爾...你好,我想把結果帶到列表視圖:) – user2513275

+0

我已經使用了textview ..但它更好,更容易與listview ...我可以節省大量的時間和佈局與ListView方法,我從一些教程瞭解。 – user2513275

相關問題