2014-09-10 75 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