【问题标题】:Want to call Main Activity when button in Expandablelistview is clicked android想要在单击 Expandablelistview 中的按钮时调用 Main Activity android
【发布时间】:2017-11-19 23:01:10
【问题描述】:

我有一个带有按钮的Expandablelistview,它可以从数据库中删除项目,该数据库提供listview。这些项目正在被删除,但我需要离开该活动并返回它以查看该项目已消失。我想只调用主要活动,但startActivity(intent) 带有红色下划线。我已经尝试了各种。请有人帮忙。谢谢

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup)
{
    final String item = (String)this.getGroup(i);

    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.parent_layout, null);
    }

    final TextView itemTV = view.findViewById(R.id.parent_shopping_item);
    itemTV.setText(item);
    CheckBox checkBox = view.findViewById(R.id.checkBox);
    Button deleteItemBtn = view.findViewById(R.id.button);
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked)
        {
            itemTV.setPaintFlags(itemTV.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            if (! isChecked)
            {
                itemTV.setPaintFlags(itemTV.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
            }
        }
    });

    deleteItemBtn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            DBManager dbManager = new DBManager(context, null, null, 1);
            dbManager.deleteItem(item);
            Intent intent = new Intent(context, MainActivity.class);
            startActivity(intent);
        }
    });
    return view;
}

【问题讨论】:

    标签: android expandablelistview expandablelistadapter


    【解决方案1】:

    我想打电话

     yourAdapter.notifyDataSetChanged();
    

    不会再次尝试调用您的活动,而是会更新您的 listView。

    【讨论】:

    • 从数据库中删除项目后是否更新列表?
    • 这就是我想要实现的目标,但运气不佳。如果我离开带有列表的活动,然后返回已删除的项目就消失了。我希望它是即时的。
    • 在更新数据库并调用 notify 方法后,您可以将数据库中的项目提取到列表中。我的意思是您需要重新创建列表并在更改后通知适配器。如果您不想在每次发生更改时重新创建列表,您可以从数据库和列表中删除一个项目并通知适配器。
    【解决方案2】:

    需要在onClick方法中添加:

    @Override
    public void onClick(View view)
    {
        DBManager dbManager = new DBManager(context, null, null, 1);
        dbManager.deleteItem(item);
        Intent intent = new Intent(context, MainActivity.class);
        Bundle bundle = new Bundle();
        startActivity(context, intent, bundle);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多