怎么获取ListView和GridView适配器中item上TextView和ListView控件的值

CharSequence num = ((TextView) mListView.getChildAt(i).findViewById(R.id.item_editgood_num)).getText();
            CharSequence price = ((EditText) mListView.getChildAt(i).findViewById(R.id.item_edit_price)).getText();
            CharSequence slogan = ((EditText) mListView.getChildAt(i).findViewById(R.id.item_edit_slogan)).getText();

原理start:
ListView list= (ListView)findViewById(R.id.list);//获得listview

for (int i = 0; i < list.getChildCount(); i++) {
LinearLayout layout = (LinearLayout)list.getChildAt(i);// 获得子item的layout
EditText et = (EditText) layout.findViewById(R.id.et);// 从layout中获得控件,根据其id
// EditText et = (EditText) layout.getChildAt(1)//或者根据位置,在这我假设TextView在前,EditText在后

 System.out.println("the text of "+i+"'s EditText:----------->"+et.getText());

}

原理end
利用上面的方法就可以得到对应的控件值

public class EditGoodActivity extends BaseActivity implements OnClickListener{
    private static final String TAG= EditGoodActivity.class.getSimpleName();
    private TopBarView mTopBarView;
    private Context mContext;
    private ImageView mImageView;
    private TextView activity_edit_description,activity_edit_size,activity_edit_stopshall,activity_edit_save;
    private GridView mListView;
    private EditGoodAdapter mAdapter;
    private ArrayList<Lists>  editGoods = new ArrayList<Lists>();
    private String image;
    private String description;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_cf_good);
        mContext = this;
        hpinApp.addActivity(TAG, this);
        mTopBarView = (TopBarView) findViewById(R.id.activity_edit_good_topbar);
        mTopBarView.setActivity(this);
        mTopBarView.setTitle("大家好");
        initData();
        init();
    }

    /**初始化控件*/
    private void init() {
        mImageView = (ImageView) findViewById(R.id.activity_edit_iv);  
        activity_edit_description = (TextView) findViewById(R.id.activity_edit_description);
        activity_edit_size = (TextView) findViewById(R.id.activity_edit_size);
        activity_edit_stopshall = (TextView) findViewById(R.id.activity_edit_stopshall);
        activity_edit_save = (TextView) findViewById(R.id.activity_edit_save);
        mListView = (GridView) findViewById(R.id.activity_edit_listview);

        activity_edit_description.setText(description);
        activity_edit_size.setText("颜色:" + editGoods.get(0).color + "   尺码:" + editGoods.get(0).style + "   数量:" + editGoods.get(0).num);
        ImageLoader.getInstance().displayImage(image,mImageView, mImageLoad.getOptions(), mImageLoad.getImageLoadingListener());

        activity_edit_stopshall.setOnClickListener(this);
        activity_edit_save.setOnClickListener(this);

        mAdapter = new EditGoodAdapter(editGoods, mContext); 
        mListView.setAdapter(mAdapter);
    }

    /**获取数据*/
    @SuppressWarnings("unchecked")
    private void initData() {
         Intent intent = getIntent();
         image = intent.getStringExtra("pic");
         description = intent.getStringExtra("name"); 
         Bundle b = intent.getExtras();
        ArrayList< ProductList.Info.Lists> name = (ArrayList< ProductList.Info.Lists>) b.getSerializable("list"); 
         for (int i = 0; i < name.size(); i++) {
             Lists lists = name.get(i); 
             editGoods.add(lists);
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.activity_edit_stopshall) {

        } else if (v.getId() == R.id.activity_edit_save) {

        ArrayList<String>   arrayList = new ArrayList<String>();
        ModifyGood modifyGood = null;
        for (int i = 0; i < editGoods.size(); i++) {
            CharSequence num = ((TextView) mListView.getChildAt(i).findViewById(R.id.item_editgood_num)).getText();
            CharSequence price = ((EditText) mListView.getChildAt(i).findViewById(R.id.item_edit_price)).getText();
            CharSequence slogan = ((EditText) mListView.getChildAt(i).findViewById(R.id.item_edit_slogan)).getText();

            modifyGood = new ModifyGood();
            modifyGood.id = editGoods.get(i).id; 
            modifyGood.num = num.toString();
            modifyGood.price = price.toString();
            modifyGood.introduce = slogan.toString();
            Gson gson = new Gson();
            String json = gson.toJson(modifyGood);
            arrayList.add(json);
        }
            push(arrayList);
        } else {
        }
    }

    private void push(final ArrayList<String>  put) {
        SimpleHUD.dismiss();    
        StringRequest stringRequestPost = new StringRequest(Method.POST, URLs.MODIFYSHALL,  
                new Response.Listener<String>() {  
                    @Override  
                    public void onResponse(String response) {  
                        Log.i(TAG, response);  
                    }  
                }, new Response.ErrorListener() {  
                    @Override  
                    public void onErrorResponse(VolleyError error) {  
                        Log.e(TAG, error.getMessage(), error); 
                    }
                }) {  
            @Override  
            protected Map<String, String> getParams() throws AuthFailureError {  
                Map<String, String> map = new HashMap<String, String>();  
                map.put("uid", SharedPreferencesUtil.getStringData(mContext, "uid", "")); 
                map.put("password", SharedPreferencesUtil.getStringData(mContext, "password", "")); 
                map.put("lists", put.toString());  
                return map;  
            }  
        };  
        mVolleyTools.getQueue().add(stringRequestPost);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
        MobclickAgent.onPause(this);
    }

}

适配器

public class EditGoodAdapter extends BaseAdapter {
    private ArrayList<Lists>  editGoods;
    private Context context;

    public EditGoodAdapter(ArrayList<Lists> editGoods, Context context) {
        super();
        this.editGoods = editGoods;
        this.context = context;
    }

    @Override
    public int getCount() {
        return editGoods.size();
    }

    @Override
    public Object getItem(int arg0) {
        return editGoods.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View contentView, ViewGroup arg2) {
        final Lists list = editGoods.get(position);
        final ViewHolder holder;
        if (contentView == null) {
            contentView = View.inflate(context,R.layout.item_edit_good, null);
            holder = new ViewHolder();
            holder.item_editgood_color = (TextView) contentView.findViewById(R.id.item_editgood_color);
            holder.item_editgood_size = (TextView) contentView.findViewById(R.id.item_editgood_size);
            holder.item_editgood_reduce = (TextView) contentView.findViewById(R.id.item_editgood_reduce);
            holder.item_editgood_num = (TextView) contentView.findViewById(R.id.item_editgood_num);
            holder.item_editgood_add = (TextView) contentView.findViewById(R.id.item_editgood_add);
            holder.item_edit_price = (EditText) contentView.findViewById(R.id.item_edit_price);
            holder.item_edit_slogan = (EditText) contentView.findViewById(R.id.item_edit_slogan);
            contentView.setTag(holder);
        }else{
            holder = (ViewHolder) contentView.getTag();
        }

        holder.item_editgood_color.setText(list.color);
        holder.item_editgood_size.setText(list.style + "码");

        holder.item_editgood_reduce.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                String num_reduce=Integer.valueOf(holder.item_editgood_num.getText().toString())-1+"";
                holder.item_editgood_num.setText(num_reduce);
            }
        });

        holder.item_editgood_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (Integer.parseInt(holder.item_editgood_num.getText().toString()) <= Integer.parseInt(list.num)) {
                    String num_add=Integer.valueOf(holder.item_editgood_num.getText().toString())+1+"";
                    holder.item_editgood_num.setText(num_add);
                }else{
                    SimpleHUD.showInfoMessage(context, "已经是最大值");
                }
            }
        });


        return contentView;
    }

    class ViewHolder{
        TextView item_editgood_color;   
        TextView item_editgood_size;   
        TextView item_editgood_reduce; 
        TextView item_editgood_num;  
        TextView item_editgood_add;  
        EditText item_edit_price;     
        EditText item_edit_slogan;       
    }
}
展开阅读全文

没有更多推荐了,返回首页