gpt4 book ai didi

java - 使用 Firebase 刷新列表的最佳方法是什么

转载 作者:行者123 更新时间:2023-12-02 08:54:28 25 4
gpt4 key购买 nike

我目前在项目中更新列表的方式显然不是应用程序的标准。如何刷新列表而不清除它?

这段代码写得很好,没有任何中断;但是,一旦用户完成编辑,列表就会被清除并刷新,使用户返回到列表顶部。刷新数据时的最佳实践是什么,特别是当其他用户正在编辑数据而不中断当前用户时。

写作:

protected void addStep() {
String stepID = Database.push().getKey();
step newStep = new step(recipeID, stepID, "stepImage", "","");
Database.child(stepID).setValue(newStep);
getData();

}

适配器:

package asdasd.asd;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
* This class is the class responsible for handling the lists of steps that the user will see during the creator phase.
* It works by setting an array adapter which uses the fragment_steps layout displaying it on a list within the StepActivity
* On text listeners are on each of the fields, when the user edits one of the fields, the program waits 600ms, and then uploads the
* data to the database;
* this refreshes the list TODO change the way the list refreshes in the instance of the creator; perhaps add a delay of 1 minuite before refresh, and or if a new step has been added
* A timer is responsible for this delay. The setting of data is to a specific path; being the recipe -> step -> long/shortText field.
*/

public class stepList extends ArrayAdapter<step>{
private Activity context;
private List<step> steps;
private DatabaseReference database;
private Timer timer1,timer2;

public stepList(Activity context, List<step> steps) {
super(context, R.layout.fragment_step, steps);
this.context = context;
this.steps = steps;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

//get database
database = FirebaseDatabase.getInstance().getReference("steps");

//init Layout inflater
LayoutInflater inflater = context.getLayoutInflater();

//step
View listViewItem = inflater.inflate(R.layout.fragment_step,null,true);

//step objects
final TextView descriptionText = (TextView) listViewItem.findViewById(R.id.stepRecipeShortText);
final TextView longDescriptionText = (TextView) listViewItem.findViewById(R.id.stepRecipeLongText);
ImageView stepImage = listViewItem.findViewById(R.id.stepImage);
//init step
final step step = steps.get(position);

//get stepID
final String stepID = step.getStepID();

//Set Data
descriptionText.setText(step.getStepDescription());
longDescriptionText.setText(step.getStepLongDescription());

//TODO If user has uploaded an image, then use that, else then use default

//Add listener to descriptionText so that when a user edits the fields, it is uploaded to the same step in the database
descriptionText.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// user is typing: reset already started timer (if existing)
if (timer1 != null) {
timer1.cancel();
}
}
@Override
public void afterTextChanged(Editable s) {
timer1 = new Timer();
timer1.schedule(new TimerTask() {
@Override
public void run() {
String newDescriptionText = descriptionText.getText().toString();
addToDatabase(stepID,"stepDescription", newDescriptionText);
}
}, 600);
}
});
//Add listener to LongDescriptionText so that when a user edits the fields, it is uploaded to the same step in the database
longDescriptionText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// user is typing: reset already started timer (if existing)
if (timer2 != null) {
timer2.cancel();
}
}
@Override
public void afterTextChanged(Editable s) {
timer2 = new Timer();
timer2.schedule(new TimerTask() {
@Override
public void run() {
String newLongDescriptionText = longDescriptionText.getText().toString();
addToDatabase(stepID, "stepLongDescription", newLongDescriptionText);
}
}, 600);
}
});
return listViewItem;
}
//Add the data the user is entering to the database; there is a 600ms delay on the period between the user stopping typing and the data being updated.
private void addToDatabase(String id, String location, String text) {
database.child(id).child(location).setValue(text);
}
}

获取:

public void getData() {
//receives all the recipes and adds them to the list
Database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Clear the list
listStepsList.clear();
//Iterate through the nodes
for(DataSnapshot stepSnapshot : dataSnapshot.getChildren()){
//get recipe
step step = stepSnapshot.getValue(step.class);
//add step to list, if it is apart of the same recipe.
if(step.getRecipeID().equals(recipeID)) {
listStepsList.add(step);
}
}
//create Adapter
stepList stepAdapter = new stepList(StepActivity.this, listStepsList);
//Attatch adapter to listview
viewStepsList.setAdapter(stepAdapter);
stepAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

最佳答案

我假设只要数据库发生变化,您就会看到“大爆炸”/闪光。如果是这样,那是因为您正在更新整个列表,即使数据中只更改了单个项目。

为了改进这一点,您需要更精细地更新适配器以进行更改。为此,您可以附加一个 ChildEventListener,它会触发数据结构中较低一级的事件。因此,假设将一个新节点添加到您的列表中,您将得到一个仅包含一个新节点的 onChildAdded 调用,而不是重建整个列表来添加一个节点。然后,您将更新 listStepsList 而不是重建它,并告诉适配器有关更改的信息。

举个例子,我建议查看 FirebaseUI,如 adapters in there都使用这种模式。他们从共同的FirebaseArray class构建它观察数据库,然后使用适配器类将数组粘合到 UI。例如,这里是how FirebaseRecyclerAdapter connects the changes in the database to minimal updates to the view .

关于java - 使用 Firebase 刷新列表的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60573017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com