2012-01-30 43 views
1

的資料,因此我做了3個EditTexts和兩個按鈕的形式,「確定」和「取消」填充自定義列表視圖與來自多個編輯文本

這裏是我的代碼是:

package com.examples.edtTxtCustomList; 

import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 


public class InputPage extends Activity implements OnClickListener{ 

    private Button btnGo,btnCancel; 
    EditText et_name,et_email,et_phone; 

    List<String> data = new ArrayList<String>(); 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.custom_list_view); 


     et_name = (EditText) findViewById(R.id.et_name); 
     et_email =(EditText)findViewById(R.id.et_email); 
     et_phone = (EditText)findViewById(R.id.et_phno); 
     btnGo = (Button) findViewById(R.id.btn_ok); 

     btnGo.setOnClickListener(this); 
     data.add("Welcome"); 

    } 

     @Override 
     public void onClick(View src) { 

      String nm=et_name.getText().toString(); 
      String no = et_email.getText().toString(); 
      String mail= et_phone.getText().toString(); 

      Intent i = new Intent(this,CustomLayout.class); 

      i.putExtra("name", nm); 
      i.putExtra("email", mail); 
      i.putExtra("phone", no); 
      startActivity(i); 

     }  
} ` 

而且這裏是我的形式的XML文件:

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

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TextView 
      android:id="@+id/tv_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="5dip" 
      android:layout_marginTop="20dip" 
      android:layout_marginBottom="10dip" 
      android:text="Name :" 
      android:textSize="20dip"/> 

     <EditText 
      android:id="@+id/et_name" 
      android:layout_width="230dip" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/tv_name" 
      android:layout_alignTop="@id/tv_name" 
      android:layout_marginBottom="10dip" 
      android:layout_marginLeft="8dip" 
      android:textSize="15dip"/> 

     <TextView 
      android:id="@+id/tv_email" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="5dip" 
      android:layout_marginTop="20dip" 
      android:layout_marginBottom="10dip" 
      android:layout_below="@id/tv_name" 
      android:text="E-Mail :" 
      android:textSize="20dip"/> 

     <EditText 
      android:id="@+id/et_email" 
      android:layout_width="230dip" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/tv_email" 
      android:layout_alignTop="@id/tv_email" 
      android:layout_below="@id/et_name" 
      android:inputType="textEmailAddress" 
      android:layout_marginLeft="6dip" 
      android:layout_marginBottom="10dip" 
      android:textSize="15dip"/> 

     <TextView 
      android:id="@+id/tv_phno" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="5dip" 
      android:layout_marginTop="20dip" 
      android:layout_marginBottom="10dip" 
      android:layout_below="@id/tv_email" 
      android:text="Phone :" 
      android:textSize="20dip"/> 

     <EditText 
      android:id="@+id/et_phno" 
      android:layout_width="230dip" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/tv_phno" 
      android:layout_alignTop="@id/tv_phno" 
      android:layout_below="@id/et_email" 
      android:inputType="phone" 
      android:layout_marginLeft="5dip" 
      android:layout_marginBottom="10dip" 
      android:textSize="15dip"/> 

     <Button 
      android:id="@+id/btn_ok" 
      android:layout_width="100dip" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@id/et_phno" 
      android:layout_marginTop="30dip" 
      android:layout_marginLeft="50dip" 
      android:text="OK" 
      android:textSize="20dip"/> 

     <Button 
      android:id="@+id/btn_cancel" 
      android:layout_width="100dip" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_below="@id/et_phno" 
      android:layout_marginTop="30dip" 
      android:layout_marginRight="50dip" 
      android:text="Cancel" 
      android:textSize="20dip"/>  
    </RelativeLayout> 
</LinearLayout> 

現在我想的是,當我在「OK」,應在customlistview顯示所有的所有三種編輯文本的數據點擊。

我試圖做一個動態列表視圖如下:

package com.example.customlist; 

import java.util.ArrayList; 
import java.util.HashMap; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.SimpleAdapter; 


public class CustomListLayoutActivity extends ListActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.custom_list_view); 

     SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.custom_row_layout, 
       new String[] {"Name","Email","Phone"}, 
       new int[] {R.id.text1,R.id.text2, R.id.text3} 
       ); 
     populateList(); 
     setListAdapter(adapter); 
    } 

    static final ArrayList<HashMap<String,String>> list = 
     new ArrayList<HashMap<String,String>>(); 

    private void populateList() { 
     HashMap<String,String> temp = new HashMap<String,String>(); 
     temp.put("Name","Anurag Kulkarni"); 
     temp.put("Email", "[email protected]"); 
     temp.put("Phone", "+91-9904475805"); 
     list.add(temp); 
     HashMap<String,String> temp1 = new HashMap<String,String>(); 
     temp1.put("Name","Rahul Shah"); 
     temp1.put("Email", "[email protected]"); 
     temp1.put("Phone", "+91-9898434909"); 
     list.add(temp1); 
     HashMap<String,String> temp2 = new HashMap<String,String>(); 
     temp2.put("Name","Pratik Thakkar"); 
     temp2.put("Email", "[email protected]"); 
     temp2.put("Phone", "+91-8539524925"); 
     list.add(temp2); 
     HashMap<String,String> temp3 = new HashMap<String,String>(); 
     temp3.put("Name","Utsav Patel"); 
     temp3.put("Email", "[email protected]"); 
     temp3.put("Phone","+91-9843500999"); 
     list.add(temp3); 
     HashMap<String,String> temp4 = new HashMap<String,String>(); 
     temp4.put("Name","Karan Mohan"); 
     temp4.put("Email", "[email protected]"); 
     temp4.put("Phone", "+91-9944843974"); 
     list.add(temp4); 

    } 
} 

能否請你幫我呢?我仍然在學習基礎知識,所以任何幫助都會受到關注。

謝謝。

回答

0

正如在我的另一篇文章...

每當你想要做的處理,在一個ListView的看法,你 需要創建一個自定義的適配器將處理您的邏輯 執行和傳遞信息必要時提供意見。

一個自定義的adater會逐個膨脹視圖,這可以是 動態的固定。

實施例:

Link 1

相關問題