简体   繁体   中英

How to get data from ListView in Android when I use custom adapter

I have one ListView in my android app where I get some color from database and put some number against it (basically I maintain stock for each color) which I need to save in database. For colors I use Color entity class (Stock) and I need to save it in some different entity.

My custom adapter code

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;
}
}

My ListView layout is

<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" >

and inner layout in list

<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>

Now I need to get values for all colors and save them on click of a button.

Now the problem is, I'm not sure how to get all data (countEditText) for each color (colorCode). I tried many ways but still not getting it. I even referred to many such questions on stackoverflow but I think my limited android knowledge didn't help me much here. just to highlight again, I'm not capturing data on row click but click of separate button.

EDIT This is the method where I'm trying to get ListView data

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();
}

Ok I'm finally able to get around this. Here's my updated method 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);
}

Define a field List<Color> items , set it in constructor and get it when you need it.

public class ColorStockListViewAdapter extends ArrayAdapter<Color> {

    ...

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


    private List<Color> items

    public List<Color> getColors() {
        return items;
    }
    ...
}

and in other places call adapter.getColors()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM