我的自定义ListView
包含CheckBox
和EditText
。我希望EditText
只有在选中CheckBox
时才可以点击和修改。如何实现?我尝试使用:
public class ScrapListAdapter extends ArrayAdapter {
ArrayList<ScrapOffer> arrayScrapItems;
Context context;
public ScrapListAdapter(Context context, ArrayList<ScrapOffer> arrayScrapItems) {
super(context, R.layout.scrap_list_row_item, arrayScrapItems);
// TODO Auto-generated constructor stub
this.context = context;
this.arrayScrapItems = arrayScrapItems;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.scrap_list_row_item, parent, false);
final EditText kg = (EditText) convertView.findViewById(R.id.editText1);
final CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
final TextView txtKg = (TextView) convertView.findViewById(R.id.textView1);
final float scale = context.getResources().getDisplayMetrics().density;
cb.setPadding(cb.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
cb.getPaddingTop(),
cb.getPaddingRight(),
cb.getPaddingBottom());
//cb.setText(modelItems[position].getName());
cb.setText(arrayScrapItems.get(position).getItemName());
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked)
{
// Toast.makeText(context, "You checked " + arrayScrapItems.get(position).getItemName(), Toast.LENGTH_SHORT).show();
cb.setButtonDrawable(R.drawable.checkbox_background_active);
cb.setTag(arrayScrapItems.get(position).getItemId());
kg.setBackgroundResource((R.drawable.kg_box));
txtKg.setTextColor(Color.parseColor("#000000"));
kg.setFocusable(true);
kg.setEnabled(true);
kg.setInputType(InputType.TYPE_CLASS_NUMBER);
}
else
{
//Toast.makeText(context, "You unchecked " + arrayScrapItems.get(position).getItemName(), Toast.LENGTH_SHORT).show();
cb.setButtonDrawable(R.drawable.checkbox_background);
kg.setBackgroundResource((R.drawable.kg_box_inactive));
cb.setTag("");
txtKg.setTextColor(Color.parseColor("#D3D3D3"));
kg.setFocusable(false);
kg.setEnabled(false);
kg.setInputType(InputType.TYPE_NULL);
}
}
}
);
return convertView;
}
}
但这不起作用。
scrap_list_row_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:button="@drawable/checkbox_background"
android:text="item"
android:paddingRight="10dp"
android:textColor="#000000"
android:textSize="13dp" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/kg_box_inactive"
android:ems="10"
android:paddingLeft="5dp"
android:textSize="10dp"
android:inputType="number" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="kg"
android:textSize="13dp"
android:textColor="#D3D3D3" />
</LinearLayout>
</LinearLayout>
非常感谢任何帮助。
答案 0 :(得分:0)
当您单击listview-item时,您可以修改某些listview-item-gui-elements的状态。
当重新绘制listview-item时,将重新创建listview-item-gui-elements,并且gui-element-states将丢失。
如何解决:
在onCheckedChanged()
中记住检查状态:arrayScrapItems.get(position).setCheckstate(isChecked)
当您(重新)在getView()
中创建一个viewItem时,您还必须根据arrayScrapItems.get(position).getCheckstate()