4

我有一个只有 TextView 的 ListView。我想要一个实现,如果我单击 ListView 行,应该会出现一个带有替换按钮的编辑文本,并且无论我在该编辑文本中键入什么并单击替换,列表行都应该更新。我面临的主要问题是夸大列表行上的布局。有人能告诉我如何实现吗?

public class ContextMenuActivity extends Activity {
    private ListView list;
    TextView tv;
    ArrayList<String> alistItems;
    int loopCount;
    CustomAdapter adapter;
    LayoutInflater inflater;
    TextView textView;
    EditText edtTextToReplace;
    RelativeLayout rl_inflate;
    View child;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contextmenulist);
        initComponents();
        setActionListener();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Options");
        menu.add(0, v.getId(), 0, "Edit");
        menu.add(0, v.getId(), 0, "Delete");
        /*
         * MenuInflater inflater = getMenuInflater();
         * inflater.inflate(R.menu.context_menu, menu);
         */
    }



    private void initComponents() {
        inflater = ContextMenuActivity.this.getLayoutInflater();

        list = (ListView) findViewById(R.id.contextmenu_lst_list);
        tv = (TextView) findViewById(R.id.listitem_txt_item);
        alistItems = new ArrayList<String>();
        for (loopCount = 1; loopCount < 30; loopCount++) {
            alistItems.add("Item " + loopCount);
        }
        prepareView();
    }

    private void prepareView() {
        adapter = new CustomAdapter(getApplicationContext(), R.layout.listitem,
                alistItems);
        list.setAdapter(adapter);
        registerForContextMenu(list);
    }

    private void setActionListener() {

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, final View view,
                    final int arg2, long arg3) {
                System.out.println("alist is " + alistItems);
                textView = (TextView) view.findViewById(R.id.listitem_txt_item);
                rl_inflate = (RelativeLayout) view
                        .findViewById(R.id.rl_inflate);
                child = getLayoutInflater().inflate(R.layout.clicklistitem,
                        null);
                rl_inflate.addView(child);
                textView.setVisibility(View.GONE);
                rl_inflate.setVisibility(View.VISIBLE);
                Button my_btn = (Button) child
                        .findViewById(R.id.clicklistitem_btn_replace);
                edtTextToReplace = (EditText) child
                        .findViewById(R.id.clicklistitem_edt);
                my_btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        textView.setText(edtTextToReplace.getText().toString());
                        alistItems.set(arg2, edtTextToReplace.getText()
                                .toString());
                        rl_inflate.setVisibility(View.GONE);
                        rl_inflate.removeViewInLayout(child);
                        textView.setVisibility(View.VISIBLE);
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    public class CustomAdapter extends ArrayAdapter<String> {
        ArrayList<String> alistItems;
        int resource;

        public CustomAdapter(Context context, int resource,
                ArrayList<String> alistItems) {
            super(context, resource);
            this.alistItems = alistItems;
            this.resource = resource;
        }

        @Override
        public int getCount() {
            return alistItems.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = new Holder();
            if (convertView == null) {
                convertView = inflater
                        .inflate(R.layout.listitem, parent, false);
            }
            holder.tvRow = (TextView) convertView
                    .findViewById(R.id.listitem_txt_item);
            convertView.setTag(holder);
            holder = (Holder) convertView.getTag();
            holder.tvRow.setText(alistItems.get(position));
            return convertView;
        }
    }

    class Holder {
        TextView tvRow;
    }
}
4

2 回答 2

9

您可以通过两种方式实现这一目标:-

  1. 首先是在 listview 的项目布局中添加 EditText 和一个 Button 并隐藏它们。现在设置listview的onItemClickListener,其中隐藏textview并显示editext和replace按钮。

  2. 其次是创建一个带有编辑文本和按钮的新布局,并为 listview 设置 onItemClickListener,当单击行时,您可以轻松地将该布局膨胀到列表视图的项目布局中。

第二个解决方案代码: -

膨胀.xml

<EditText
            android:id="@+id/enter_txt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
/>

<Button
            android:id="@+id/btn_replace"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
/>

*list_view_item.xml*

<TextView
            android:id="@+id/my_txt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="ABCD"
/>

<RelativeLayout
            android:id="@+id/rl_inflate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
/>

列表视图 onItemClickListener

        listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

                           TextView txt_view = (TextView)view.findViewById(R.id.my_txt);

                           txt_view.setVisibility(View.GONE);

                       RelativeLayout rl_inflate = (RelativeLayout)view.findViewById(R.id.rl_inflate);
                           View child = getLayoutInflater().inflate(R.layout.inflate);
                           rl_inflate.addView(child);


                          Button my_btn = (Button)child.findViewById(R.id.btn_replace);
                          EditText enter_txt = (EditText)child.findViewById(R.id.enter_txt);

                          my_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                                 txt_view.setText(enter_txt.getText().toString());
                                 txt_view.setVisibility(View.VISIBLE);
            }
            });
        }
    });
于 2013-09-17T05:17:20.447 回答
0

您可以使用以下两种解决方案中的任何一种:

  1. 包括一个edittext和replace按钮以及textview布局,它应该处于隐藏状态。单击列表项时,将编辑文本和按钮的可见性更改为可见,并将 textview 的可见性更改为 Gone。单击替换按钮时,反转可见性更改。

  2. 单击列表项时,显示带有编辑文本和替换按钮的警告框。更改内容并单击替换按钮后,将 textview 内容更改为更改值。

于 2013-09-17T05:15:00.680 回答