我正在构建一个notes应用程序,在按下一个按钮后,我想从另一个活动中更新我的StratedRecycleView。由于某些原因,更新recycleviewadapter的不同选项对我不可用。谢谢你的时间!
这是recviewadapter
public StaggeredRecycleViewAdapter(Context mContext, ArrayList<String> arrTitle, ArrayList<String> arrNote) {
this.arrTitle = arrTitle;
this.arrNote = arrNote;
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_grid_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: called");
RequestOptions requestOptions = new RequestOptions().placeholder(R.drawable.ic_launcher_background);
//GET TEXT HÄÄÄR
holder.txtTitle.setText(arrTitle.get(position));
holder.txtNote.setText(arrNote.get(position));
// DET HÄR FÖR ATT ÖPPNA TEXT FRÅN STAGGERED VIEW
holder.txtNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked on note " + arrNote.get(position));
String rvTitleText;
String rvNoteText;
rvTitleText = MainActivity.arrTitle.get(position).toString();
rvNoteText = MainActivity.arrNote.get(position).toString();
Intent editIntent = new Intent(mContext, edTxt_activity.class);
editIntent.putExtra("headerText", rvTitleText.toString());
editIntent.putExtra("noteText", rvNoteText.toString());
editIntent.putExtra("arrIndex", position);
mContext.startActivity(editIntent);
}
});
holder.txtTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked on title " + arrTitle.get(position));
}
});
}
这是主要活动
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int NUM_COLUMNS = 2;
static ArrayList<String> arrTitle = new ArrayList<>();
static ArrayList<String> arrNote = new ArrayList<>();
FloatingActionButton btn_add;
public void StartEdTxtActivity (){
Intent editIntent = new Intent(getApplicationContext(), edTxt_activity.class);
startActivity(editIntent);
}
private void addSampleText (){
Log.d(TAG, "onCreate: test test of mainactivity text");
arrTitle.add("My first note");
arrNote.add("Lorem ipsum dolor sit amet, consectetur adipisicing elit, " +
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud");
}
private void initRecyclerView () {
System.out.println("init staggered recycleview");
RecyclerView recyclerView = findViewById(R.id.recyclerView);
StaggeredRecycleViewAdapter staggeredRecycleViewAdapter = new StaggeredRecycleViewAdapter(this, arrTitle, arrNote);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(NUM_COLUMNS, LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerView.setAdapter(staggeredRecycleViewAdapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_add = findViewById(R.id.btn_add);
initRecyclerView();
addSampleText();
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Button", "add button is pressed");
StartEdTxtActivity ();
}
});
以下是我想在按下“右功能”按钮后更新的活动
public void checkFunction (View view) {
Log.i("this is INDEX INTENT", String.valueOf(indexPosition));
}
public void right_function (View view) {
Log.i("Check", "CHECK ICON PRESSED");
titleText = edtTxt_header.getText().toString();
noteText = textInputEditText.getText().toString();
if (titleText.equals(t1) && noteText.equals(n1)) {
finish();
}
else if (titleText.equals(t1) && !noteText.equals(n1)){
MainActivity.arrNote.set(indexPosition, noteText);
StaggeredRecycleViewAdapter.notifyDataSetChanged() <------- HERE IS MY PROBLEM,CANT RUN notifydatachanger
finish();
}
else if (noteText.equals(n1) && titleText!= t1) {
MainActivity.arrTitle.set(indexPosition, titleText);
finish();
}
else {
MainActivity.arrTitle.add(titleText);
MainActivity.arrNote.add(noteText);
finish();
}
}
/**
* BACK BUTTON
* @param view
*/
public void left_function (View view) {
//Add 'are you sure dialog here`
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ed_txt_activity);
getSupportActionBar().hide();
leftIcon = findViewById(R.id.left_icon);
rightIcon = findViewById(R.id.right_icon);
edtTxt_header = (EditText)findViewById(R.id.edtTxt_header);
textInputEditText = (TextInputEditText) findViewById(R.id.textInputEditText);
btn_check = findViewById(R.id.btn_check);
Intent editIntent = getIntent();
t1 = editIntent.getStringExtra("headerText");
n1 = editIntent.getStringExtra("noteText");
indexPosition = editIntent.getIntExtra("arrIndex", 0);
edtTxt_header.setText(t1);
textInputEditText.setText(n1);
1条答案
按热度按时间vd8tlhqk1#
您有一些选项(基于您的设计模型):
当您运行第二个活动时,第一个活动将暂停,当您关闭第二个活动并返回到第一个活动时,将调用onresume函数,然后您可以更新onresume函数上的recyclerview。
如果使用startactivityforresults启动第二个“活动”,则在关闭第二个“活动”后,将指向第一个“活动”中的onactivityresult函数。您甚至可以将一些数据从第二个活动传递回第一个活动,然后更新recyclerview。
如果您希望在两个屏幕都可见时更新recyclerview,我建议使用片段。一个包含recyclerview的片段和另一个包含第二个视图的片段。然后可以在第一个片段中创建接口,并在第二个片段上实现接口。使用这种方法,您可以实时更新recyclerview。
还有一些其他的想法,比如使用firebase来更新和检索数据。使用firebase,您可以在第二个屏幕上更新数据,第一个屏幕将自动获取这些数据。
希望这些想法有用。