I have a question for Android Intent.

Asked 2 years ago, Updated 2 years ago, 97 views

This is the main activity.

ImageView likeButton;
TextView likeCountView;
ImageView hateButton;
TextView hateCountView;
CommentAdapter adapter;
Button allView;
ArrayList<CommentItems> items;

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

    likeButton = findViewById(R.id.likeButton);
    likeCountView = findViewById(R.id.likeCountView);

    hateButton = findViewById(R.id.hateButton);
    hateCountView = findViewById(R.id.hateCountView);

    TextView writeButton = findViewById(R.id.button_write);

    allView = findViewById(R.id.button_allView);

    ListView listView = findViewById(R.id.listView);
    items = new ArrayList<CommentItems>();
    items.add("kim78**", 5, "It's moderately interesting". I haven't seen a sleepless movie in a long time.", R.drawable.user1);
    items.add("kim78**", 5, "It's moderately interesting". I haven't seen a sleepless movie in a long time.", R.drawable.user1);
    items.add("kim78**", 5, "It's moderately interesting". I haven't seen a sleepless movie in a long time.", R.drawable.user1);
    items.add("kim78**", 5, "It's moderately interesting". I haven't seen a sleepless movie in a long time.", R.drawable.user1);
    items.add("kim78**", 5, "It's moderately interesting". I haven't seen a sleepless movie in a long time.", R.drawable.user1);
    items.add("kim78**", 5, "It's moderately interesting". I haven't seen a sleepless movie in a long time.", R.drawable.user1);
    items.add("kim78**", 5, "It's moderately interesting". I haven't seen a sleepless movie in a long time.", R.drawable.user1);
    adapter = new CommentAdapter(items);
    listView.setAdapter(adapter);

    //View all button
    allView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), AllCommentView.class);
            intent.putExtra("items", items);
            startActivity(intent);

        }
    });

    //Create button
    writeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), CommentWriteView.class);
            startActivityForResult(intent, 101);
        }
    });
    }


class CommentAdapter extends BaseAdapter {
    final ArrayList<CommentItems> items;

    CommentAdapter(ArrayList<CommentItems> items) {
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommentItemView view = new CommentItemView(getApplicationContext());
        CommentItems item = items.get(position);

        view.setComment(item.getComment());
        view.setId(item.getId());
        view.setProFileImage(item.redId);
        view.setRatingBar(item.rating);

        return view;
    }
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        String comment = data.getStringExtra("comment");
        int rating = Math.round(data.getFloatExtra("rating", 0.0f));
        if (comment != null) {
            items.add(new CommentItems("kim78**", rating, comment, R.drawable.user1));
            adapter.notifyDataSetChanged();

        }
    }
}

View All Activity.

public class AllCommentView extends AppCompatActivity {
Intent intent;
ArrayList<CommentItems> items;



TextView allViewWriteButton;

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

    intent = getIntent();
    items = (ArrayList<CommentItems>) intent.getSerializableExtra("items");
    ListView allViewListView = findViewById(R.id.allview_listview);
    CommentAdapter adapter = new CommentAdapter(items);
    allViewListView.setAdapter(adapter);



    allViewWriteButton = findViewById(R.id.allviewwrite_button);
    allViewWriteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), CommentWriteView.class);
            startActivity(intent);
            finish();
        }
    });
}

class CommentAdapter extends BaseAdapter {
    final ArrayList<CommentItems> items;

    CommentAdapter(ArrayList<CommentItems> items) {
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommentItemView view = new CommentItemView(getApplicationContext());
        CommentItems item = items.get(position);

        view.setComment(item.getComment());
        view.setId(item.getId());
        view.setProFileImage(item.redId);
        view.setRatingBar(item.rating);

        return view;
    }
}
}

Create activity.

public class CommentWriteView extends AppCompatActivity {
EditText commentText;
RatingBar ratingBar;
Button saveButton;

String comment;
float rating;


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

    commentText = findViewById(R.id.editText_comment);
    ratingBar = findViewById(R.id.write_ratingbar);
    saveButton = findViewById(R.id.saveButton);
    Button exitButton = findViewById(R.id.exitButton);

    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            returnToMain();
        }
    });

    exitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

}

public void returnToMain() {
    comment = commentText.getText().toString();
    rating = ratingBar.getRating();
    Intent intent = getIntent();
        intent.putExtra("comment", comment);
        intent.putExtra("rating", rating);
        setResult(RESULT_OK, intent);
        finish();
}
}

The intent between the main activity and the writing activity works normally by processing it. View all In the activity, press the button to the writing screen, create it, and update the list, but I have no idea...

intent android listview adapter

2022-09-21 21:05

1 Answers

Main activity

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

    ....
    //View all button
    allView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), AllCommentView.class);
            intent.putExtra("items", items);
            startActivityForResult(intent, 101);
        }
    });
}

View all activities

public class AllCommentView extensions AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_comment_view);
        ....

        allViewWriteButton = findViewById(R.id.allviewwrite_button);
        allViewWriteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), CommentWriteView.class);
                startActivityForResult(intent, 101);
            }
        });
    }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101) {
            if (resultCode == RESULT_OK) {
                final String comment = data.getStringExtra("comment");
                final float rating = data.getFloatExtra("rating", 0.0f);
                if (comment != null) {
                    final Intent intent = new Intent();
                    intent.putExtra("comment", comment);
                    intent.putExtra("rating", rating);
                    setResult(RESULT_OK, intent);
                }
            }
            finish();
        }
    }
}

I think you just need to add the code above.

If you call finish() as soon as you move on to write all like the existing code, RESULT_CANCELD will come over when you return to the main from writing, so in the scenario run in the order of main - write all-view-write, write a comment after the on activity result (res) of all views.


2022-09-21 21:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.