简体   繁体   English

从另一个活动中单击按钮后,从活动中删除列表视图项

[英]Delete a listview item from activity on button click from another activity

I have a MainActivity that shows a listview with items that I add dynamically to it. 我有一个MainActivity ,它显示一个listview其中包含我动态添加到其中的项目。 So far everything works. 到目前为止,一切正常。 Now wanted to create a button that should delete the listview item that was clicked on. 现在想创建一个button ,该button应该删除被单击的listview item Here is the code I came with. 这是我附带的代码。

MainActivity 主要活动

package news;

import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayAdapter<String> newslist_adapter;
    ArrayList<String> new_subject = new ArrayList<>();
    ArrayList<String> new_post = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView post_view = findViewById(R.id.news_feed);

        FloatingActionButton add_post_button = findViewById(R.id.post_btn);

        //create click event and pass values of arrays
        post_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), full_post_activity.class);
                intent.putExtra("Subject", new_subject);
                intent.putExtra("Post", new_post);
                intent.putExtra("position", id);
//                getApplicationContext().startActivity(intent);
                startActivityForResult(intent, 2);
            }
        });

        //create button connection and create keylistener
        add_post_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, addpost_activity.class);
                startActivityForResult(intent, 1);

            }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        final ListView post_view = findViewById(R.id.news_feed);
        if (requestCode == 1) {
            if(resultCode == Activity.RESULT_OK){
                //get subject and post from second activity
                String new_subject_value = data.getStringExtra("newSubject");
                String new_post_value = data.getStringExtra("newPost");
                new_subject.add(new_subject_value);
                new_post.add(new_post_value);
                newslist_adapter = new ArrayAdapter<>(
                        MainActivity.this,
                        android.R.layout.simple_expandable_list_item_1, new_subject);
                post_view.setAdapter(newslist_adapter);

            }
        }
        if (requestCode == 2) {
            if(resultCode == Activity.RESULT_OK){
                String item2delete = data.getStringExtra("id");
                new_subject.remove(item2delete);
                newslist_adapter = new ArrayAdapter<>(
                        MainActivity.this,
                        android.R.layout.simple_expandable_list_item_1, new_subject);
                post_view.setAdapter(newslist_adapter);
            }
        }
    }
} 

SecondActivity SecondActivity

package news;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class full_post_activity extends AppCompatActivity {

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

        final int id = getIntent().getExtras().getInt("id");

        //create view reference
        final TextView subject_edit = findViewById(R.id.subject_input);
        final TextView post_edit = findViewById(R.id.post_input);

        //create button reference
        Button delete_button = findViewById(R.id.full_post_delete_btn);

        //create click event
        delete_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.putExtra("id", id);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

        ArrayList<String> subject_array = (ArrayList<String>) getIntent().getSerializableExtra("Subject");
        ArrayList<String> post_array = (ArrayList<String>) getIntent().getSerializableExtra("Post");

        String subject_string = subject_array.get(0);
        String post_string = post_array.get(0);

        //set textview text
        subject_edit.setText(subject_string);
        post_edit.setText(post_string);
    }
}

My problem now is that the delete button doesn't do anything besides returning to the MainActivity . 我现在的问题是, delete button除了返回MainActivity之外什么也不做。 What am I doing wrong? 我究竟做错了什么?

You cannot get id value to MainActivity. 您无法将ID值获取给MainActivity。 This line in second activity cause problem 第二行中的此行导致问题

final int id = getIntent().getExtras().getInt("id");

In Main Activity, You can put id value using name index "position" 在主要活动中,您可以使用名称索引“ position”放置id值

intent.putExtra("position", id);

So you should change them to 所以你应该将它们更改为

In Second Activity 第二活动中

final int id = getIntent().getExtras().getInt("position");

or Main Activity 主要活动

intent.putExtra("id", id);

UPDATED try this in Main Activity 更新在主要活动中尝试此

intent.putExtra("id", position);

If you want to stay in the activity where the delete button is I would suggest creating a getter and a setter for the list behind your ListView(Easily generate them with ALT+INSERT). 如果您想停留在删除按钮所在的活动中,建议您为ListView后面的列表创建一个吸气剂和一个吸气剂(用ALT + INSERT轻松生成)。

You can then make an instance of your MainActivity inside the delete_buttons OnClick methode and get said List with the getter. 然后,您可以在delete_buttons OnClick方法内创建MainActivity的实例,并使用getter获取所说的List。 Remove the Item you need to remove and update the list with your setter, again using your MainActivity instance. 再次使用MainActivity实例,删除您需要删除的Item并使用setter更新列表。

Edit: here are some code samples 编辑:这是一些代码示例

Getter and Setter: Getter和Setter:

public ArrayList<String> getNewPost() {
    return this.new_post;
}

public void setNewPost(ArrayList<String> np) {
    this.new_post = np;
}

delete_button OnClick methode: delete_button OnClick方法:

delete_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MainActivity main=new MainActivity();
            ListView<String> np=main.getNewPost();
            np.remove("StringToRemove"); 
            main.setNewPost(np);
        }
    });

I would also suggest you to make a back_button to check if the list was updated, you can use your old delete_button onclick for that. 我还建议您创建一个back_button来检查列表是否已更新,您可以为此使用旧的delete_button onclick。

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

相关问题 通过数据库从另一个活动中的列表视图中删除项目 - delete an item from listview from another activity through database 从另一个Activity更新ListView的项目 - Updating an item of ListView from another Activity Android-从另一个活动将项目添加到ListView - Android - Adding item to ListView from another Activity 无法在另一个活动上显示listView中的项目 - Unable to display item from listView on another activity 如何从另一个活动中删除recycleview中的项目? - how to delete an item in recycleview from another activity? 添加新的ListView项目并从另一个活动中获取项目的价值 - Adding new ListView item and get value of item from another activity 将项目从另一个活动添加到列表视图(仅添加一个项目) - Add item to listview from another activity (it only adds one item) 当我单击 listView 项目时,如何在另一个活动中显示来自 firestore 的详细信息? - How to display detail informations from firestore in another activity when I click a listView item? Android:通过单击按钮,将一个活动的TextView添加到另一个活动的ListView中 - Android: Add TextView from one activty, to ListView in another activity through a button click 如何将值从json列表视图中的选定项目发送到另一个活动? - How to send values from a selected item on a json listview to another activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM