android - 当我使用自定义适配器时如何从 Android 中的 ListView 获取数据

标签 android android-listview custom-adapter

我在我的 Android 应用程序中有一个 ListView,我从数据库中获取一些颜色并在其中输入一些数字(基本上我为每种颜色维护库存),我需要将其保存在数据库中。对于颜色,我使用 Color 实体类 (Stock),我需要将它保存在一些不同的实体中。

我的自定义适配器代码

public class ColorStockListViewAdapter extends ArrayAdapter<Color> {

Context context;
int resourceId;

public ColorStockListViewAdapter(Context context, int resourceId,
        List<Color> items) {
    super(context, resourceId, items);
    this.context = context;
    this.resourceId = resourceId;
}

/* private view holder class */
private class ViewHolder {
    // ImageView vehicleImageView;
    TextView colorDesc;
    SeekBar seekBar;
    EditText countText;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    Color rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(resourceId, null);
        holder = new ViewHolder();

        holder.colorDesc = (TextView) convertView
                .findViewById(R.id.colorCode);

        holder.seekBar = (SeekBar) convertView
                .findViewById(R.id.countSeekBar);
        holder.countText = (EditText) convertView
                .findViewById(R.id.countEditText);

        holder.seekBar.setTag(holder.countText);
        holder.countText.setTag(holder.seekBar);

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

    holder.colorDesc.setText(rowItem.getDescription());
    holder.countText.setText("0");

    return convertView;
}
}

我的ListView布局是

<ListView
    android:id="@+id/colorStockList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/vehicleOption"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp" >

和列表中的内部布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >

<TextView
    android:id="@+id/colorCode"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:gravity="start"
    android:layout_weight="1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<SeekBar
    android:id="@+id/countSeekBar"
    android:layout_width="0dp"
    android:layout_weight="4"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:progress="0"
    android:secondaryProgress="0" />

<EditText
    android:id="@+id/countEditText"
    android:layout_height="50dp"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:gravity="end"
    android:inputType="number" />

</LinearLayout>

现在我需要获取所有颜色的值并在单击按钮时保存它们。

现在的问题是,我不确定如何获取每种颜色 (colorCode) 的所有数据 (countEditText)。我尝试了很多方法,但仍然没有得到它。我什至在 stackoverflow 上提到了很多这样的问题,但我认为我有限的 android 知识在这里对我帮助不大。再次强调一下,我不是在点击行时捕捉数据,而是在点击单独的按钮时捕捉数据。

编辑这是我试图获取 ListView 数据的方法

public void addStock(View view){

    int count = listView.getCount();

    for(int i = 0; i < count; i++){
        // I get color object here, so not sure how to get EditText view value I have in my list
        Color color = (Color) listView.getItemAtPosition(i);
    }

    for(int i = 0; i < adapter.getCount(); i++){
        // I get color object here, so not sure how to get EditText view value I have in my list
        Color color = adapter.getItem(i);
    }

    // My AsyncTask to save data.
    new StockTask().execute();
}

最佳答案

好吧,我终于可以解决这个问题了。这是我更新的方法 addStock

public void addStock(View view){

    List<Stock> stockList = new ArrayList<Stock>();
    Stock stock;

    int count = listView.getCount();

    for(int i = 0; i < count; i++){
        Color color = (Color) listView.getItemAtPosition(i);

        // Here's the critical part I was missing
        View childView = listView.getChildAt(i);
        EditText editText = (EditText) childView.findViewById(R.id.countEditText);
        String colorCount = editText.getText().toString();

        stock = new Stock();
        stock.setColorCode(color.getCode());
        stock.setCount(Integer.valueOf(colorCount));

        stockList.add(stock);
    }

    new StockTask().execute(stockList);
}

关于android - 当我使用自定义适配器时如何从 Android 中的 ListView 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27382404/

相关文章:

android - ListView的自定义适配器

android - 自定义微调器不调用自定义 ListView ?

java - 自定义适配器不适用于 Parse.com 的 ListView

android - 什么是适配器中的转换 View ?它是如何运作的?

java - 无法从网页下载 html 源

android - ContactsContract.Contacts.Entity IllegalArgumentException异常

android - Android M 中的互联网权限

android - 在 JNI 方法上返回一个字符串数组

带有包含 Button onClickListener 的 ListView 的 Android AsyncTask