我想将一个自定义ListView记录显示到第二个Custom ListView

时间:2014-09-09 11:20:34

标签: java android android-listview

我创建了两个自定义listView,它们都有两个textView和一个EditText,我想要显示所有记录,在第二个自定义列表视图上输入edittext接受textView on Button Click事件,在我的代码中它显示了doe&t显示在第二个列表视图中的任何东西,但它显示在吐司上

ArrayList<HashMap<String, String>> MyArrList;
String rate ;
String itemnam ;
String quan;

  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) 
    {


    int count = lisView1.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
    LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i); // Find by under   LinearLayout
    TextView nam = (TextView)itemLayout.findViewById(R.id.ColID);

    TextView rat = (TextView)itemLayout.findViewById(R.id.ColCode);

    EditText quan = (EditText)itemLayout.findViewById(R.id.txtInput);

    String Code = rat.getText().toString();
    String Country = quan.getText().toString();
    String name = nam.getText().toString();

    Log.d(Code, Country);
    Toast.makeText(Mmnue.this,name + ", " + Country+ " , " +Code ,Toast.LENGTH_LONG).show(); 
    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.ColID);

    txtID.setText(MyArrList.get(position).get("ID") +".");

    // ColCode
    TextView txtCode = (TextView) convertView.findViewById(R.id.ColCode);
    txtCode.setText(MyArrList.get(position).get("Code"));

    // ColCode
    //EditText quan = (EditText) convertView.findViewById(R.id.txtInput);
//  quan.setText(MyArrList.get(position).get("Code"));
  return convertView;

    }

    }

    public class CountryAdapter2 extends BaseAdapter
    {
    private Context context;

    public CountryAdapter2(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 1;
    }

    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.ColID);
    //txtID.setText(MyArrList.get(position).get("ID") +".");
    txtID.setText(quan);
    // ColCode
    TextView txtCode = (TextView) convertView.findViewById(R.id.ColCode);
//  txtCode.setText(MyArrList.get(position).get("Code"));
    txtCode.setText(rate);

    EditText quant = (EditText) convertView.findViewById(R.id.txtInput);
//  txtCode.setText(MyArrList.get(position).get("Code"));
    quant.setText(itemnam);

    return convertView;

    }

1 个答案:

答案 0 :(得分:0)

String rate ;
String itemnam ;
String quan;

你在哪里初始化它们?

首先你是在接下来做的,但现在你做了当地的病毒:

String Code = rat.getText().toString();
String Country = quan.getText().toString();
String name = nam.getText().toString();

在设置适配器之前没有做任何事情

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

什么都不会发生:

 txtID.setText(quan);
 txtCode.setText(rate);
 quant.setText(itemnam);

你应该填充一个arrayList并在第二个适配器中使用第二个arraylist。就像你的第一次一样。