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

时间:2017-11-16 14:28:32

标签: java android

我有MainActivity,其中显示listview项,其中包含我动态添加的项目。到目前为止一切正常。现在想要创建一个应该删除点击的button的{​​{1}}。这是我附带的代码。

MainActivity

listview item

SecondActivity

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);
            }
        }
    }
} 

我现在的问题是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); } } 除了返回delete button之外没有做任何事情。我做错了什么?

2 个答案:

答案 0 :(得分:1)

您无法获得MainActivity的ID值。第二个活动中的这一行导致问题

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

在主要活动中,您可以使用名称索引“位置”

来输入id值
intent.putExtra("position", id);

所以你应该把它们改成

第二项活动

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

主要活动

intent.putExtra("id", id);

更新在主要活动

中尝试此操作
intent.putExtra("id", position);

答案 1 :(得分:0)

如果您想留在删除按钮所在的活动中,我建议您为ListView后面的列表创建一个getter和setter(使用ALT + INSERT轻松生成它们)。

然后,您可以在delete_buttons OnClick方法中创建MainActivity的实例,并使用getter获取所述List。 删除需要删除的项目并使用您的setter更新列表,再次使用您的MainActivity实例。

编辑:这里有一些代码示例

Getter和Setter:

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

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

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);
        }
    });

我还建议您制作一个back_button来检查列表是否已更新,您可以使用旧的delete_button onclick。