如何从android中具有相同id的多个编辑文本中获取文本

时间:2012-12-07 12:24:02

标签: android

我正在使用xml创建一个编辑文本,并使用enumerator.by在listview的适配器类.im中添加它。我能够创建两个具有相同id的文本字段。问题是如何从两者获取字符串文本字段,因为它们共享相同的id.below是代码。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View rowView = null;

    AddKeywordRow row = keywordRows.get(position);

    switch (row.getRowType()) {
    case TitleRow:
        rowView = inflater.inflate(R.layout.titlerow, null);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.rowtitle);
        txtTitle.setText(row.getMessage());
        break;
    case EditRow:
        rowView = inflater.inflate(R.layout.editrow, null);
        txtInput = (EditText) rowView.findViewById(R.id.rowedit);
        txtInput.setHint(row.getMessage());
        if (row.getHeight() != 0)
        {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                     LinearLayout.LayoutParams.MATCH_PARENT,row.getHeight());
            txtInput.setLayoutParams(layoutParams); 
            layoutParams.setMargins(7, 0, 7,0);
        //txtInput.setLayoutParams(new LinearLayout.LayoutParams(
            //      LinearLayout.LayoutParams.MATCH_PARENT, row.getHeight()));
            txtInput.setPadding(3,0,0,30);  
        }
        // set padding and margin
        break;
    case MessageRow:
        rowView = inflater.inflate(R.layout.messagerow, null);
        TextView txtMessage = (TextView) rowView
                .findViewById(R.id.rowmessage);
        txtMessage.setText(row.getMessage());
        break;
    case ButtonRow:
        rowView = inflater.inflate(R.layout.buttonrow, null);
        Button btn = (Button)rowView.findViewById(R.id.button1);

        btn.setOnClickListener(new OnClickListener() {


        }
        });

        break;
    }

    return rowView;
}

2 个答案:

答案 0 :(得分:0)

首先,我建议您使用Holder pattern作为列表项。 其次覆盖getItem(int position);在您的适配器中,以便它根据位置返回必要的文本值。例如,您可以在活动中的onItemclickListener()中使用它。

@Override
    public String getItem(int position) {
        return keywordRows.getMessage(position);
    }

您的onItemClickListener将如下所示:

private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowid) {
            String message = (String)adapterView.getItemAtPosition(position);
        }
    }

答案 1 :(得分:0)

按列表行检索它们,类似于(伪代码):

((的EditText)getAdapter()。的getItem(ID))。gettext的()