2012-03-14 79 views
0

我的代碼如下Android的ListView和EDITTEXT箱

佈局1

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

    <ListView 
     android:id="@+id/mylist" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 


</LinearLayout> 

佈局2

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

     <EditText android:id="@+id/newmessagecontent" 
       android:layout_height="wrap_content" 
       android:singleLine="false" 
       android:gravity="top" 
       android:layout_width="250dp" 
       android:layout_alignParentTop="true" 
        /> 

</LinearLayout> 

我的Java代碼

public class CheckitoutActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.row); 
     ListView listView = (ListView) findViewById(R.id.mylist); 
     //EditText view = (EditText)findViewById(R.id.newmessagecontent); 
     //listView.addHeaderView(view, null, true); 
     listView.setOnItemClickListener(new OnItemClickListener() { 



      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), 
         "Click ListItem Number " + position, Toast.LENGTH_LONG) 
         .show(); 

      } 
     }); 



     String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
      "Linux", "OS/2" }; 


     // First paramenter - Context 
     // Second parameter - Layout for the row 
     // Third parameter - ID of the View to which the data is written 
     // Forth - the Array of data 
     final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, android.R.id.text1, values); 

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


    } 
} 

我想在編輯框中先分開來。用戶應該輸入數據,然後我的應用程序將所需的數據存儲到字符串數組中,然後顯示它。我該如何去做這件事?

+1

有啥問題? – Mayank 2012-03-14 15:09:37

+0

編輯框與列表視圖一起顯示。我希望編輯框只能在開頭顯示一次,而在下面顯示。 – user1092042 2012-03-14 15:14:50

回答

1

那麼,你得添加的EditText作爲一個列表視圖頭不ListView項

創建下面的代碼爲翹楚佈局,然後將此視圖添加到標題。

<TableLayout 
    android:layout_weight="0" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <TableRow> 
     <EditText android:id="@+id/newmessagecontent" 
      android:layout_height="wrap_content" 
      android:singleLine="false" 
      android:gravity="top" 
      android:layout_width="250dp" 
      android:layout_alignParentTop="true" 
       /> 

     <Button android:layout_height="wrap_content" 
      android:id="@+id/sendmessage" 
      android:text="Send" 
      android:layout_width="wrap_content" 
      /> 
    </Table> 
     </TableLayout> 



EditText view = (EditText)findViewById(R.id.....); 
listView.addHeaderView(view, null, true); 

UPDATE

這裏

ListView lv = getListView(); 
LayoutInflater inflater = getLayoutInflater(); 
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false); //header is above XML code 
lv.addHeaderView(header, null, false); 
+0

當我將編輯文本添加爲​​標題視圖時,它會在listview.setAdapter(..)中拋出異常。我已經編輯了這個問題。請在看看 – user1092042 2012-03-15 05:42:03

+0

之前將它添加到列表視圖中。 – Mayank 2012-03-15 06:07:01

+0

你可以請告訴我這樣做的代碼。如果我想在編輯框旁邊添加一個按鈕,如何將它們兩個充氣到一起 – user1092042 2012-03-15 10:34:10