2014-09-10 55 views
0

我已經創建了兩個自定義ListViews。兩者都有兩個TextViews和一個EditText。我想在第二個ListViewsEditText內顯示EditText第一個ListView的記錄。在我的代碼只顯示了兩個TextView記錄,但不是EditText記錄我想顯示第一個自定義ListView的EditText記錄到第二個自定義ListView EditText

public class Mmnue extends Activity 
    { 

    ArrayList<HashMap<String, String>> MyArrList; 

    protected void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.menuitem); 


    final ListView lisView1 = (ListView)findViewById(R.id.listView1); 
    final ListView lisView2 = (ListView)findViewById(R.id.listView2); 


    MyArrList = new ArrayList<HashMap<String, String>>(); 

    HashMap<String, String> map; 

    /*** Rows 1 ***/ 
    map = new HashMap<String, String>(); 
    map.put("ID", "Butterscotch"); 
    map.put("Code", "Rs 10"); 
    MyArrList.add(map); 

    /*** Rows 2 ***/ 
    map = new HashMap<String, String>(); 
    map.put("ID", "Birthday Cake"); 
    map.put("Code", "Rs 100"); 

    MyArrList.add(map); 

    /*** Rows 3 ***/ 
    map = new HashMap<String, String>(); 
    map.put("ID", "Black Crunch"); 
    map.put("Code", "Rs 102"); 

    MyArrList.add(map); 

    /*** Rows 4 ***/ 
    map = new HashMap<String, String>(); 
    map.put("ID", "Industrial Chocolate"); 
    map.put("Code", "Rs 200"); 

    MyArrList.add(map); 

    /*** Rows 5 ***/ 
    map = new HashMap<String, String>(); 
    map.put("ID", "Coffee Molasses Chip"); 
    map.put("Code", " Rs 500"); 

    MyArrList.add(map);  
    lisView1.setAdapter(new CountryAdapter(this)); 
    Button btnGetItem = (Button) findViewById(R.id.btnGetItem); 
    btnGetItem.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     {     

    lisView2.setAdapter(new CountryAdapter2(getApplicationContext())); 
      } 
      });} 


    public class CountryAdapter extends BaseAdapter 
    { 
    private Context context; 

    public CountryAdapter(Context c) 
    { 
    //super(c, R.layout.activity_column, R.id.rowTextView,); 
    // TODO Auto-generated method stub 
    context = c; 
    } 
    public int getCount() { 
    // TODO Auto-generated method stub 
    return MyArrList.size(); 
    } 
    public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
    } 
    public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
    } 
    public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    LayoutInflater inflater = (LayoutInflater) context 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if (convertView == null) { 
    convertView = inflater.inflate(R.layout.activity_mmnue, null); 
    } 
     // ColID 
    TextView txtID = (TextView) convertView.findViewById(R.id.nm); 
    txtID.setText(MyArrList.get(position).get("ID") +"."); 
     // ColCode 
    TextView txtCode = (TextView) convertView.findViewById(R.id.rat); 
    txtCode.setText(MyArrList.get(position).get("Code")); 

    /*EditText quan = (EditText)convertView.findViewById(R.id.txtInput); 
    quan.setText(MyArrList.get(position).get("edt")); 
    */ 

    return convertView; 

    } 

    } 


public class CountryAdapter2 extends BaseAdapter 
    { 
    private Context context; 


    public CountryAdapter2(Context c) 
    { 
     context = c; 
    } 
    public int getCount() { 
    // TODO Auto-generated method stub 
     return MyArrList.size(); 
    } 
    public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
    } 
    public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
    } 
    public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    LayoutInflater inflater = (LayoutInflater) context 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if (convertView == null) 
    {convertView = inflater.inflate(R.layout.activity_mmnue, null); 
    } 

    // ColID 
    TextView txtID = (TextView) convertView.findViewById(R.id.nm); 
    txtID.setText(MyArrList.get(position).get("ID") +"."); 
    //txtID.setText(name); 
    // ColCode 
    TextView txtCode = (TextView) convertView.findViewById(R.id.rat); 
    txtCode.setText(MyArrList.get(position).get("Code")); 
    //txtCode.setText(rate); 

    EditText quan = (EditText)findViewById(R.id.txtInput); 
    String quant = quan.getText().toString(); 
    Toast.makeText(Mmnue.this,""+quant ,Toast.LENGTH_LONG).show(); 
    return convertView; 
    } 
    } 
+0

您希望第一個編輯文本(第一個列表視圖)中的字符串自動填充第二個編輯文本視圖(第二列表視圖)中的編輯文本? – OnePunchMan 2014-09-10 11:05:47

+0

不會自動啓動按鈕單擊事件 – Ravi 2014-09-10 11:25:40

+0

可能的重複[如何在Toast上顯示自定義列表視圖的選定記錄](http://stackoverflow.com/questions/25738872/how-to-show-selected-record-of-custom -list - 視圖 - 上烤麪包) – 2Dee 2014-09-15 08:40:21

回答

0

因爲在您Button - onClick定義:

lisView2.setAdapter(new CountryAdapter2(getApplicationContext())); 

它只是簡單地從EditText值創建一個新的Adapter無信息的ListView1。你應該設置像

lisView2.setAdapter((CountryAdapter) lisView1.getAdapter())); 

該適配器將不會有任何影響,但因爲你沒有存儲EditText值。您需要在Adapter範圍內的DataObject,該值可保存EditText值。本教程可能有所幫助:http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html