简体   繁体   English

使用ArrayAdapter从LinearLayout添加和删除项目

[英]Add & delete item from LinearLayout using ArrayAdapter

我正在制作的屏幕截图

I am doing bubble display of selected contacts. 我正在对所选联系人进行气泡显示。 Each telephone bubble is a LinearLayout, which contains ImageView and TextView. 每个电话气泡都是一个LinearLayout,其中包含ImageView和TextView。 These bubbles are then displayed in another LinearLayout which is child of HorizontalScrollView. 这些气泡然后显示在另一个LinearLayout中,它是Horizo​​ntalScrollView的子级。

It child/parent tree looks like this: 它的子树/父树看起来像这样:

 - HorizontalScrollView
 |- LinearLayout (id="@+id/telField")
  |- LinearLayout (id="@+id/telBox")  <- is programmatically added to parent
   |- TextView (id="@+id/telNumber")
   |- ImageView (id="@+id/delNumber")

In my .java class I call this method to display "telBox" LinearLayout in "telField" LinearLayout: 在我的.java类中,我调用此方法在“ telField” LinearLayout中显示“ telBox” LinearLayout:

public void createAdapter() {
    telList = new ArrayAdapter<>(this, R.layout.text_buble, R.id.telNumber, telNumList);
    telField = (LinearLayout) findViewById(R.id.telField);

    telField.removeAllViews();
    final int adapterCount = telNumList.size();

    for (ik = 0; ik < adapterCount; ik++) {
        final View item = telList.getView(ik, null, null);
        telField.addView(item);

        item.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                telField.removeView(item);
                telNumList.remove(ik-1);
                telList.notifyDataSetChanged();
                refresh();
            }
        });
    }
}

Method refresh(); 方法refresh(); – is custom method which helps to "reload" Activity: it gets App values, refreshes warning ImageViews and cals createAdapter() method . –是自定义方法,有助于“重新加载”活动:它获取应用程序值,刷新警告ImageViews并校准createAdapter()方法

Big button "SELECT" calls an Intent which returns a selected phone number from a contacts book. 大按钮“ SELECT”调用一个Intent,该Intent返回通讯录中选定的电话号码。 I call this code to update my LinearLayout with id "telField": 我调用以下代码来更新ID为“ telField”的LinearLayout:

telNumList.add(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
createAdapter();

Problem I face is: 我面临的问题是:

After I click on LinearLayout with id "telNumber" it one by one deletes every bubble (no matter which I clicked) until it reaches first added bubble. 在单击ID为“ telNumber”的LinearLayout之后,它会逐个删除每个气泡(无论单击哪个气泡),直到到达第一个添加的气泡为止。 It also crashes 50/50 when reaches first added element, I have not figured out a dependency. 当到达第一个添加的元素时,它也会崩溃50/50,我还没有弄清依赖项。 The error it returns is "out of bounds error", so I think it is connected with ik - 1 line. 它返回的错误是“越界错误”,因此我认为它与ik - 1行连接。

My question is: How do I better construct my ArrayAdapter? 我的问题是:如何更好地构造ArrayAdapter?

In your code you are trying to remove the view by ik which is getting change continuously because of which your coding is removing last view I have modified your code as given below 在您的代码中,您尝试通过ik删除视图,该视图正在不断变化,因此您的代码正在删除最后的视图。我已修改了您的代码,如下所示

for (ik = 0; ik < adapterCount; ik++) {
        final int position=ik;
        final View item = telList.getView(ik, null, null);
        telField.addView(item);

        item.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                telField.removeView(item);
                telNumList.remove(position);
                telList.notifyDataSetChanged();
                //refresh();
                createAdapter();
            }
        });
    }

Here position will help you to remove the particular view which you want to remove. 在此position将帮助您删除要删除的特定视图。 I hope this is what you are asking for. 我希望这是您要的。

Inside your for loop, write these line of code: 在您的for循环中,编写以下代码行:

for (ik = 0; ik < adapterCount; ik++) {

    final View item = telList.getView(ik, null, null);
    item.setTag(ik);
    telField.addView(item);

    item.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int index = Integer.parseInt(v.getTag().toString());
            telNumList.remove(index);
            refresh();
        }
    });
}

Hope it will help you out. 希望它能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM