Android View.getId()在listview中始终返回相同的id,并使用holder模式
我有一个列表视图,每行都有EditText。 我需要获取焦点行的id。 在我的代码中,holder.valor.getId;总是返回相同的id。我不明白为什么 在其他活动中,我想放大编辑文本,并从actionbar按钮更改键盘类型 这是我的密码:Android View.getId()在listview中始终返回相同的id,并使用holder模式,android,listview,Android,Listview,我有一个列表视图,每行都有EditText。 我需要获取焦点行的id。 在我的代码中,holder.valor.getId;总是返回相同的id。我不明白为什么 在其他活动中,我想放大编辑文本,并从actionbar按钮更改键盘类型 这是我的密码: public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; final String ma
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final String marca;
final String producto;
try {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row_lista_form, null);
holder.item = (TextView) convertView.findViewById(R.id.item);
holder.valor = (EditText) convertView.findViewById(R.id.valor);
holder.valor.setInputType(InputType.TYPE_CLASS_PHONE);
holder.imgCam = (ImageView) convertView.findViewById(R.id.cambutton);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
marca = getlForm().get(position).getMarca();
producto = getlForm().get(position).getProducto();
final int proyectoproducto = getlForm().get(position).getProyectoProductoId();
String valor = getlForm().get(position).getCantidad();
if (valor == null || valor.equals("null"))
valor = "";
holder.item.setText(marca + "\n" + producto);
holder.valor.setText(valor);
// Listener : If focus lost, we insert value in db
holder.valor.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// update row
Formulario f = getlForm().get(position);
EditText et = (EditText) v;
f.setCantidad(et.getText().toString());
f.setSync(0);
FormHandler fh = new FormHandler(ctx);
boolean existForm = fh.existForm(f);
if (!existForm)
fh.addForm(f);
else
fh.updateForm(f);
}else{
// We have the focus, we send the id in the prefs,
// to send it the change keyboard function
int editTextId = holder.valor.getId();
prefs.edit().putInt("metricaEtId", editTextId).commit();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
使用
在
我不知道如果所有的id都相等,你为什么想要id,但是如果你想知道它在getView中的textedit的“位置”,那么在getView中用“edittext.setTagposition”设置该位置,并用int position=v.getTag检索;在onFocusChanged.ok中,但是我将丢失我的引用holder:holder=ViewHolder-convertView.getTag;当然不是。你还是可以离开。你必须理解,当我建议“edittext.setTag”时,它在getView中被翻译成holder.valor.setTag。我不太明白,但我看不出有什么意义。。。在onFocusChanged中,我可以访问位置参数。因此,没有必要这样做。然后,我不知道如何从活动中的位置获取我的edittext…您可以访问位置参数。但当焦点改变时,这不是正确的位置。测试一下,你会看到的。并测试我的代码。onFocusChanged中的编辑文本是View v。但代码已经存在了。
v.getId();
onFocusChange(View v,....