Android动态Multicolumn Listview

时间:2012-01-20 07:36:52

标签: android

我有一个多列列表视图,如图中所示,我使用自定义适配器填充此自定义列表。所以问题是如何获取数据单击提交按钮意味着当我单击提交按钮我应该获取数据喜欢名称,价格和数量只检查复选框.... Thanx提前。

在我的主xml中,我有一个listview,在主列表xml中,我有txtname,txtprice,edittext和checkbox,并使用高效的适配器。 我能够在列表视图中查看数据,但问题是我无法在点击提交按钮时保存数据...所以PLZ帮助我,带有示例代码,bcz m new to android ..

以下是我的代码..

public class Menu extends Activity {
ListView list;
Cursor cursorMenu;
Button btnPlaceOrder;
Button btnShowOrders;
String Descstr="";
String strtotal="";

List<String[]> lstSelectedItems = null;


DBAdapter db = new DBAdapter(this);


private  String[] strName;
private  String[] strPrice;
private  String[] strDescription;

private  class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;


    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {

        try {
            return strName.length;
        } catch (Exception e) {
            //Toast.makeText(Menu.this, "No Data !", Toast.LENGTH_LONG).show();
            return 0;
        }

    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.menulist, null);

            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.txtItemName);
            holder.text2 = (TextView) convertView
                    .findViewById(R.id.txtPrice);
            holder.text3 = (TextView) convertView
                    .findViewById(R.id.txtDescription);
            holder.etext3 = (EditText) convertView
                    .findViewById(R.id.txtQty);
            holder.chk = (CheckBox) convertView
                    .findViewById(R.id.chkBox);



            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(strName[position]);
        holder.text2.setText(strPrice[position]);
        holder.text3.setText(strDescription[position]);
        return convertView;
    }

     class ViewHolder {
        TextView text;
        TextView text2;
        TextView text3;
        EditText etext3;
        CheckBox chk;

    }
}

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

    btnPlaceOrder = (Button) findViewById(R.id.btnPlaceOrder);
    btnPlaceOrder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


        }
    });


    /*db.open();
    cursorMenu = db.menu_getAllTitles();

    int rowcount = cursorMenu.getCount();

    System.out.println("---- +++++ " + rowcount);
    System.out.println("---- column +++++ " + cursorMenu.getColumnCount());

    int index = 0;
    if (rowcount > 0) {
        strName = new String[rowcount];
        strPrice = new String[rowcount];
        strDescription = new String[rowcount];
        if (cursorMenu.moveToFirst()) {
            do {

                strName[index] = cursorMenu.getString(1);
                strDescription[index] = cursorMenu.getString(2);
                strPrice[index] = cursorMenu.getString(3);
                Log.v(TAG, "Name-- " + strName[index] + "Price-- "
                        + strPrice[index]);
                index++;
            } while (cursorMenu.moveToNext());
        }
        cursorMenu.close();

    } else {
        Toast.makeText(this, "No Data found", Toast.LENGTH_LONG).show();
    }*/




    list = (ListView) findViewById(R.id.lstMenu);
    list.setAdapter(new EfficientAdapter(this));
    System.out.println("--List Child count-----"+list.getChildCount());
    System.out.println("--List count-----"+list.getCount());

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(getBaseContext(),
                    "You clciked " + strName[arg2] + "\t" + strPrice[arg2],
                    Toast.LENGTH_LONG).show();

        }
    });

}

}

1 个答案:

答案 0 :(得分:0)

http://www.vogella.de/articles/AndroidListView/article.html通过这个例子,你可以得到关于listview的所有内容。