gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:59:38 25 4
gpt4 key购买 nike

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

enter image description here

图片适配器.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_width="match_parent" android:layout_height="80dp"
android:background="#000000">

<ImageView
android:id="@+id/pictures"
android:layout_width="100dp"
android:layout_height="75dp"
android:layout_alignParentLeft="true"

/>
<TextView
android:id="@+id/picturename"
android:layout_width="115dp"
android:layout_height="75dp"
android:layout_toRightOf="@+id/pictures"
android:text="Kim Domingo"
android:gravity="center"
android:textColor="#FFFFFF"
/>
<Button
android:id="@+id/btnplus"
android:layout_width="50dp"
android:layout_height="50dp"
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_width="50dp"
android:layout_height="50dp"
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_width="50dp"
android:layout_height="50dp"
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_width="match_parent"
android:layout_height="2dp"
android:background="#FFFFFF"
android:layout_below="@+id/pictures"
android:id="@+id/editText"></View>

最佳答案

我之前也这样创建过

enter image description here

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

第 1 步 首先在适配器中声明 HashMap map = new HashMap<>();

第2步然后将值放入HashMap ma​​p.put("key","value");此代码放入加号和减号按钮单击事件。

第3步 调用下面的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);
}
}
}

关于java - 如何获取自定义ListView中EditText的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35076041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com