【问题标题】:Remove Item from GridView onClick from another Activity从另一个活动的 GridView onClick 中删除项目
【发布时间】:2014-06-15 15:21:24
【问题描述】:

更新了我的代码..

当我单击(Day03 活动)中的 btn 按钮时,我想从我的 GridView(主要活动)中删除一个项目。

这是我的活动:

公共类 Main 扩展 Activity {

GridView gridView;
public static int deletePos;

static ArrayAdapter adapter;

static final String[] numbers = new String[] { "1", "2", "3", "4", "5",
        "6", "7" };
static final String[] activities = new String[] { "Day01", "Day02",
        "Day03", "Day04", "Day05", "Day06", "Day07" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.grid);

    gridView = (GridView) findViewById(R.id.grid1);

    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, numbers);

    gridView.setAdapter(adapter);

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            String val = activities[arg2]; // arg2 is the index of item
            Class ourClass = null;
            try {
                ourClass = Class.forName("com.example.cahllenge." + val);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Intent intent = new Intent(Main.this, ourClass);

            deletePos = arg2;
            adapter.notifyDataSetChanged();

            startActivity(intent);

        }
    });

}

还有 Day03 活动 公共类 Day03 扩展了 Activity {

Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn = (Button) findViewById(R.id.ok);

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int deletePos = Main.deletePos;
            Main.adapter.remove(deletePos);
            Main.adapter.notifyDataSetChanged();

        }
    });
}

【问题讨论】:

  • 为什么需要其他活动?您可以长按侦听器并删除项目您可以使用带有网格项目的复选框并删除选中的项目。除非您想做其他事情,否则不需要仅删除项目的其他活动
  • 我在 Day03 Activity 中有一个测验,我希望用户完成它时自动从 GridView 中删除该项目。
  • 您可以考虑重新设计您的应用程序。第二个真的没用
  • 你有例子吗?
  • 不,我不是在设计你的应用程序,也没有类似的代码

标签: android button gridview android-activity


【解决方案1】:

如果你真的想这样做,并且不使用静态,我会这样做:

在你的主要活动上创建一个 onNewIntent()

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    int deleteMe = intent.getIntExtra('hasDeleted', 0);
    if (deleteMe > 0) {
        // delete numbers if it was List or update setAdapter with new numbers list
        adapter.notifyDataSetChanged();
    }
}

在您完成 onClick 后的其他活动:

Intent intent = new Intent(Day03.this, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("hasDeleted", toDeleteNumber);
startActivity(intent);
finish();

【讨论】:

  • ohhh 在 notifyDataSetChanged 之前添加您要实际删除的删除代码和变量 - 更新代码
  • 我已经这样做了,但它不起作用:@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (intent.getBooleanExtra("hasDeleted", false)) { adapter.remove(numbers);适配器.notifyDataSetChanged(); } }
  • 啊尝试把finsih放在startactivity之后
  • 哦,没注意到你是在适配器上做的,我没做过。我通常会将其从数字数组中删除并执行 notifyDatasetChanged。由于 numbers 是字符串数组,您必须重新创建它并执行另一个 setAdapter,或使用 List 和 numbers.remove(toDeleteStr);
【解决方案2】:

嗨首先,从另一个活动中删除 grdView 项目并不是一个好习惯。无论如何,如果你想从另一个活动中删除它,你可以试试这个(没有什么是不可能的)。这可能会给你一些技巧来解决你的问题。

在您的 MainActivity 和 Day03 Activity 中创建一个 deletegridViewItem(){} 方法,如果您单击按钮然后调用 deletegridViewItem() 方法。您需要管理应该删除的项目。希望这会对您有所帮助。

看到这个编辑了一个.....

嘿试试这可能会给你一些线索...不要打电话 适配器.notifyDataSetChanged();在 deletePos = arg2 之后;只需调用您的 startActivityForResult(intent, REQUEST_CODE);在此之前使用intent.putExtra 将您的deletePos 置于意图中- 接下来在您的day03 活动onclick 按钮调用完成方法,如下所示

 @Override
 public void finish() {
 // Prepare data intent 
 Intent data = new Intent();
 data.putExtra("put deletepos back to intent ");

 // Activity finished ok, return the data
 setResult(RESULT_OK, data);
 super.finish();
} 

接下来在你的 mainActivity 中处理意图的 onActivityresult() 方法并在 onActivityResult 方法中调用 remove 方法。并通知 Dataset 方法...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    相关资源
    最近更新 更多