如何在Toast上显示自定义列表视图的选定记录

时间:2014-09-09 07:25:04

标签: android android-listview

我在自定义ListView中创建了两个textView和一个EditText,我想在按钮点击时在toast上显示所有在edittext中输入的记录,在我的代码中它只显示第一条记录,我想显示两个textview和edittext值进入......

public class Mmnue extends Activity {

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


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

    /*** 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));

    // Get Item Input
    lisView1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub


        Toast.makeText(Mmnue.this,""+position  ,Toast.LENGTH_LONG).show();


        }
    });





    Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
    btnGetItem.setOnClickListener(new OnClickListener() 
    {
    public void onClick(View v) 
    {
        TextView txtCode = (TextView)findViewById(R.id.ColCode);
        TextView itemnm = (TextView)findViewById(R.id.ColID);
        EditText txtInput = (EditText)findViewById(R.id.txtInput);

         rate = txtCode.getText().toString();
         itemnam = txtInput.getText().toString();
         quan= itemnm.getText().toString();


        int count = lisView1.getAdapter().getCount();

    Toast.makeText(Mmnue.this, itemnam + ", " + rate+ " , " + quan    ,Toast.LENGTH_LONG).show();






    }
    });



    }


    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 CountryAdapter(OnClickListener onClickListener) {
        // TODO Auto-generated constructor stub


    }



    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"));

    return convertView;

    }

    }


    }




@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

只需移动getview中的按钮点击代码即可。在此之前创建一个持有者类并在

中添加您的按钮
//Sample clickevent inside of getview
holder.b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //get your edittext value 
                //example
                String text = edittext.getText().toString();
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();

            }
        });

同时删除onitemclick

listView