如何在自定义 ListView 中获取 EditText 的所有值

Posted

技术标签:

【中文标题】如何在自定义 ListView 中获取 EditText 的所有值【英文标题】:How get to all value of EditText in Custom ListView 【发布时间】:2016-05-06 16:30:17 【问题描述】:

大家好, 我在我的活动中成功填充了我的自定义列表视图布局, 但问题是我无法在我的列表视图中获得填充的 EditText 的所有值,请帮助我应该采取什么方法, 谢谢

图片适配器.java

    public View getView(int position, View convertView, ViewGroup parent) 
    View row;
    row = convertView;
    final dataHandler handler;
    if(convertView == null)
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate( R.layout.row_layout,parent, false);
        handler = new dataHandler();
        handler.pictures = (ImageView) row.findViewById(R.id.pictures);
        handler.name = (TextView) row.findViewById(R.id.picturename);
        handler.price= (EditText) row.findViewById(R.id.price);
        handler.add = (Button) row.findViewById(R.id.btnplus);
        handler.minus = (Button) row.findViewById(R.id.btnminus);
        row.setTag(handler);
    else
        handler = (dataHandler) row.getTag();
    
    PSP psp;
    psp =(PSP) this.getItem(position);
    Picasso.with(getContext()).load(psp.getPicture()).resize(200, 155).into(handler.pictures);
    handler.name.setText(psp.getName());
    handler.price.setText(psp.getPrice());
return row;

MainActivity.java

    PictureAdapter adapter;
listView = (ListView) findViewById(R.id.ls);
adapter = new PictureAdapter(this,R.layout.row_layout);
listView.setAdapter(adapter);
try 
    JSONArray users = response.getJSONArray("user");
    for (int x = 0; x <= users.length()-1; x++) 
        JSONObject user = users.getJSONObject(x);
        PSP psp = new PSP(imageUri+user.getString("image")+".png",user.getString("username"),"0");
        adapter.add(psp);
    

 catch (JSONException e) 
    e.printStackTrace();

PSP.java

public class PSP

private String picture;
private  String name;
private String price;

public String getPicture() 
    return picture;


public PSP(String picture, String name, String price)
    this.setPicture(picture);
    this.setName(name);
    this.setPrice(price);


public void setPicture(String picture) 
    this.picture = picture;


public String getName() 
    return name;


public void setName(String name) 
    this.name = name;


public String getPrice() 
    return price;


public void setPrice(String price) 
    this.price = price;

row_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_ android:layout_
android:background="#000000">

<ImageView
    android:id="@+id/pictures"
    android:layout_
    android:layout_
    android:layout_alignParentLeft="true"

    />
<TextView
    android:id="@+id/picturename"
    android:layout_
    android:layout_
    android:layout_toRightOf="@+id/pictures"
    android:text="Kim Domingo"
    android:gravity="center"
    android:textColor="#FFFFFF"
    />
<Button
    android:id="@+id/btnplus"
    android:layout_
    android:layout_
    android:gravity="center"
    android:text="+"
    android:textSize="50px"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/picturename"
    android:layout_toEndOf="@+id/picturename"
    />


<EditText
    android:id="@+id/price"
    android:layout_
    android:layout_
    android:focusable="false"
    android:textColor="#FFFFFF"
    android:inputType="number"
    android:gravity="center"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/btnplus"
    android:layout_toEndOf="@+id/btnplus" />

<Button
    android:id="@+id/btnminus"
    android:layout_
    android:layout_
    android:gravity="center"
    android:text="-"
    android:textSize="50px"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/price"
    android:layout_toEndOf="@+id/price" />

<View
    android:layout_
    android:layout_
    android:background="#FFFFFF"
    android:layout_below="@+id/pictures"
    android:id="@+id/editText"></View>

【问题讨论】:

你从listview获得价值的代码在哪里,你得到的响应或错误是什么? 不知道应该怎么做,我是新手 :) 只需在你的 PSP 类中添加一个number 字段来保存每个 EditText 的值。 @CrazyOrr 请详细说明:) 每个 PSP 对象对应一行,因此您将该行的 EditText 的值放入该位置的 PSP 对象(psp =(PSP) this.getItem(position);)。 【参考方案1】:

我以前也这样创建过

您可以使用 HashMap map = new HashMap(); 来确定用户点击的项目。我假设您在适配器类中使用两个按钮单击,如果没有,则添加它。

第一步首先在adapter中声明HashMap map = new HashMap();

第二步然后把value放入HashMap ma​​p.put("key","value");这段代码放入加减按钮点击事件。

第三步调用下面的ShowHashMapValue();方法到map.put("key","value");有关查看 HashMap 值,请检查 logcat。

如果有任何问题,请比较此适配器代码以便于理解,只需在下面评论即可。

ListAdapter.java

    public class ListAdapter extends BaseAdapter 

    public ArrayList<Integer> quantity = new ArrayList<Integer>();
    public ArrayList<Integer> price = new ArrayList<Integer>();
    private String[] listViewItems, prices, static_price;
    TypedArray images;
    View row = null;

    static String get_price, get_quntity;
    int g_quntity, g_price, g_minus;

    private Context context;
    CustomButtonListener customButtonListener;

    static HashMap<String, String> map = new HashMap<>();


    public ListAdapter(Context context, String[] listViewItems, TypedArray images, String[] prices) 
        this.context = context;
        this.listViewItems = listViewItems;
        this.images = images;
        this.prices = prices;

        for (int i = 0; i < listViewItems.length; i++) 
            quantity.add(0);
        
    

    public void setCustomButtonListener(CustomButtonListener customButtonListner) 
        this.customButtonListener = customButtonListner;
    

    @Override
    public int getCount() 
        return listViewItems.length;
    

    @Override
    public String getItem(int position) 
        return listViewItems[position];
    


    @Override
    public long getItemId(int position) 
        return 0;
    

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 

        final ListViewHolder listViewHolder;
        if (convertView == null) 
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.activity_custom_listview, parent, false);
            listViewHolder = new ListViewHolder();
            listViewHolder.tvProductName = (TextView) row.findViewById(R.id.tvProductName);
            listViewHolder.ivProduct = (ImageView) row.findViewById(R.id.ivproduct);
            listViewHolder.tvPrices = (TextView) row.findViewById(R.id.tvProductPrice);
            listViewHolder.btnPlus = (ImageButton) row.findViewById(R.id.ib_addnew);
            listViewHolder.edTextQuantity = (EditText) row.findViewById(R.id.editTextQuantity);
            listViewHolder.btnMinus = (ImageButton) row.findViewById(R.id.ib_remove);
            static_price = context.getResources().getStringArray(R.array.Price);
            row.setTag(listViewHolder);
         else 
            row = convertView;
            listViewHolder = (ListViewHolder) convertView.getTag();
        

        listViewHolder.ivProduct.setImageResource(images.getResourceId(position, -1));
        listViewHolder.edTextQuantity.setText(quantity.get(position) + "");
        listViewHolder.tvProductName.setText(listViewItems[position]);
        listViewHolder.tvPrices.setText(prices[position]);


        listViewHolder.btnPlus.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 
                if (customButtonListener != null) 
                    customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, 1);

                    quantity.set(position, quantity.get(position) + 1);
                    //price.set(position, price.get(position) + 1);

                    row.getTag(position);

                    get_price = listViewHolder.tvPrices.getText().toString();

                    g_price = Integer.valueOf(static_price[position]);

                    get_quntity = listViewHolder.edTextQuantity.getText().toString();
                    g_quntity = Integer.valueOf(get_quntity);

                    map.put("" + listViewHolder.tvProductName.getText().toString(), " " + listViewHolder.edTextQuantity.getText().toString());
//                    Log.d("A ", "" + a);
//                    Toast.makeText(context, "A" + a, Toast.LENGTH_LONG).show();
//                    Log.d("Position ", "" + position);
//                    System.out.println(+position + " Values " + map.values());
                    listViewHolder.tvPrices.getTag();
                    listViewHolder.tvPrices.setText("" + g_price * g_quntity);
                    ShowHashMapValue();

                


            

        );
        listViewHolder.btnMinus.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 
                if (customButtonListener != null) 

                    customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, -1);
                    if (quantity.get(position) > 0)
                        quantity.set(position, quantity.get(position) - 1);

                    get_price = listViewHolder.tvPrices.getText().toString();
                    g_minus = Integer.valueOf(get_price);
                    g_price = Integer.valueOf(static_price[position]);
                    int minus = g_minus - g_price;
                    if (minus >= g_price) 
                        listViewHolder.tvPrices.setText("" + minus);
                    
                    map.put("" + listViewHolder.tvProductName.getText().toString(), " " + listViewHolder.edTextQuantity.getText().toString());
                    ShowHashMapValue();
                
            
        );


        return row;
    

    private void ShowHashMapValue() 
        /**
         * get the Set Of keys from HashMap
         */
        Set setOfKeys = map.keySet();

/**
 * get the Iterator instance from Set
 */
        Iterator iterator = setOfKeys.iterator();

/**
 * Loop the iterator until we reach the last element of the HashMap
 */
        while (iterator.hasNext()) 
/**
 * next() method returns the next key from Iterator instance.
 * return type of next() method is Object so we need to do DownCasting to String
 */
            String key = (String) iterator.next();

/**
 * once we know the 'key', we can get the value from the HashMap
 * by calling get() method
 */
            String value = map.get(key);

            System.out.println("Key: " + key + ", Value: " + value);
        
    

【讨论】:

工作正常先生感谢伟大的示例代码:) 您好我还有一个问题,我应该使用什么方法来维护那些edittext的值,例如当我旋转屏幕时,那些edittext的值消失了,谢谢,也许是这个问题迟到了,但迟到总比没有好:) 使用 onSaveInstanceState() 发布您的问题,以便我可以轻松理解代码。 是的,如果您发布您的问题,那么我可以给您示例代码。我不能在评论中给出完整的例子。【参考方案2】:

所以这是逻辑。您需要在 PSP 中声明一个布尔值。默认设置为false

现在当 + 按钮被触发时,您需要将布尔值 check 设置为 true

然后在您的设定价格中创建此逻辑。

public String getPrice() 

    if(check==true)
      price++;
     
    else
     price--;
    
    return price;

如果我正确理解你,那么这肯定会对你有所帮助。祝你好运!

【讨论】:

以上是关于如何在自定义 ListView 中获取 EditText 的所有值的主要内容,如果未能解决你的问题,请参考以下文章

如何使用edittexts在自定义ListView中实现监听器文本更改?

找不到有关如何在自定义小部件中使用 ListView 的正确布局选项

在自定义适配器类中发送服务器请求后,如何更改 Listview 按钮名称?

如何在自定义 ListView 中设置按钮的可见性?

listview 项目未在自定义适配器的 getview 方法中显示分配的值

按下键后如何获取编辑文本的文本