带有editText和textView的Android定制适配器

时间:2017-03-29 13:19:26

标签: java android adapter

我正在尝试为数独游戏实施Android应用,我为此创建了一个自定义适配器。我想为允许用户修改的单元格编辑文本,并为程序填充的单元格的textview,ediTexts和textViews的数量将是随机的。如何在适配器中指定?

这是我的适配器:

public class SodukuAdapter extends BaseAdapter {

    ArrayList<String> items;


    static Activity mActivity;

    private static LayoutInflater inflater = null;

    public SodukuAdapter (Activity activity, ArrayList<String> tempTitle,) {
        mActivity = activity;
        items = tempTitle;


        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public final int getCount() {

        return items.size();

    }

    @Override
    public final Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public final long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View v = null;

        v = inflater.inflate(R.layout.item, null);

        EditText et = (EditText) v.findViewById(R.id.et);
        et.setText(items.get(position));

        return v;
    }

}

1 个答案:

答案 0 :(得分:0)

您可以将两个视图放在同一个父级中(例如,父级是RelativeLayoutFrameLayout),因此它们位于彼此之上。比你只需隐藏其中一个,并在适配器的setVisibility()方法中使用getView()显示另一个。

当然,您需要一个数据源对象来跟踪视图是应该显示TextView还是EditText

class SodokuItem {
    public boolean isStatic;
    public String text;
}

...

@Override
public View getView(final int position, View convertView, ViewGroup parent)     {

    View v = inflater.inflate(R.layout.item, null);

    SodokuItem item = items.get(position);

    EditText et = (EditText) v.findViewById(R.id.et);
    TextView tv = (TextView) v.findViewById(R.id.tv);

    if(item.isStatic){
        et.setVisibility(GONE);
        tv.setText(item.text);
    }else{
        tv.setVisibility(GONE);
        et.setText(item.text);
    }

    return v;
}