我在我的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>
现在我需要获取所有颜色的值,并在点击按钮时保存它们。
现在的问题是,我不确定如何获取每种颜色的所有数据(countEditText)(colorCode)。我尝试了很多方法,但仍然没有得到它。我甚至在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();
}
答案 0 :(得分:3)
好的,我终于可以解决这个问题了。这是我更新的方法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);
}
答案 1 :(得分:0)
定义一个字段List<Color> items
,在构造函数中设置它并在需要时获取它。
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;
}
...
}
并在其他地方拨打adapter.getColors()