gpt4 book ai didi

android - 如何确保在解析数组时不返回重复的 JSON 数据?

转载 作者:行者123 更新时间:2023-11-29 00:56:50 24 4
gpt4 key购买 nike

几天前我问了一个关于解析 JSON 数组的问题:

How do you parse a JSON Array w/o a defined array?

我正在下载 11 个项目的列表(在 RecyclerView LinearLayoutManager 的 Activity 中以垂直布局显示)。出于某种原因,正在下载两个相同的列表。我仔细检查了在 Postman 中测试 Url 的 JSON 数据,没有重复值。此外,API 没有分页参数。

致主持人。我在这里找到了一些关于 JSON 中重复值的话题。同样,我的没有重复值。提前谢谢你。

Remove Duplicate objects from JSON Array

remove duplicate values from json data

上述线程中的 JSONUtils 类:

public class JSONUtils
{
/**
* Tag for the log messages
*/
private static final String LOG_TAG = JSONUtils.class.getSimpleName();

private static final String KEY_LINE_ID = "id";
private static final String KEY_LINE_NAME = "name";


public JSONUtils()
{
}

public static Lines extractFeatureFromJson (String linesJSON)
{
// If the JSON string is empty or null, then return early.
if (TextUtils.isEmpty(linesJSON)) {
return null;
}

Lines line = null;
try
{
// Create a JSONObject from the JSON file
JSONObject jsonObject = new JSONObject(linesJSON);

String id = "";
if (jsonObject.has("id"))
{
id = jsonObject.optString(KEY_LINE_ID);
}


String name = "";
if (jsonObject.has("name"))
{
name= jsonObject.optString(KEY_LINE_NAME);
}

line = new Lines(id, name);
}
catch (JSONException e)
{
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing lines JSON results", e);

}
// Return the list of lines
return line;
}
}

RecyclerViewAdapter 类:

public class LinesAdapter extends RecyclerView.Adapter<LinesAdapter.LinesAdapterViewHolder>
{
private static final String TAG = LinesAdapter.class.getSimpleName();

private ArrayList<Lines> linesList = new ArrayList<Lines>();
private Context context;
private LinesAdapterOnClickHandler mLineClickHandler;

/**
* The interface that receives onClick messages.
*/
public interface LinesAdapterOnClickHandler
{
void onClick(Lines textLineClick);
}

/**
* Creates a Lines Adapter.
*
* @param lineClickHandler The on-click handler for this adapter. This single handler is called
* * when an item is clicked.
*/
public LinesAdapter(LinesAdapterOnClickHandler lineClickHandler, ArrayList<Lines> linesList, Context context)
{
mLineClickHandler = lineClickHandler;
this.linesList = linesList;
this.context = context;
}

/**
* Cache of the children views for a line list item.
*/
public class LinesAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
@BindView(R.id.line_name)
public TextView lineName;

public LinesAdapterViewHolder(View view)
{
super(view);
ButterKnife.bind(this, view);
view.setOnClickListener(this);
}

/**
* This gets called by the child views during a click.
*
* @param v The View that was clicked
*/
@Override
public void onClick(View v)
{
int adapterPosition = getAdapterPosition();
Lines textLineClick = linesList.get(adapterPosition);
mLineClickHandler.onClick(textLineClick);
}
}

@Override
public LinesAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.line_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
return new LinesAdapterViewHolder(view);
}

/**
* Cache of the children views for a line list item.
*/
@Override
public void onBindViewHolder(LinesAdapterViewHolder holder, int position)
{
//Binding data
final Lines lineView = linesList.get(position);

holder.lineName.setText(lineView.getLineName());
}

@Override
public int getItemCount()
{
return linesList.size();
}

public void setLinesList(ArrayList<Lines> mLinesList)
{
this.linesList.addAll(mLinesList);
notifyDataSetChanged();
}
}

最佳答案

这个方法看起来很可疑:

public void setLinesList(ArrayList<Lines> mLinesList)
{
this.linesList.addAll(mLinesList);
notifyDataSetChanged();
}

它的名称类似于“setter”,但它实际上不是设置 行,而是添加 行。如果您的代码使用相同的参数调用了两次,您最终会得到重复项。

这里有两种写这个方法的方法,这样它每次都会覆盖列表:

public void setLinesList(ArrayList<Lines> mLinesList)
{
this.linesList.clear();
this.linesList.addAll(mLinesList);
notifyDataSetChanged();
}
public void setLinesList(ArrayList<Lines> mLinesList)
{
this.linesList = new ArrayList<>(mLinesList);
notifyDataSetChanged();
}

关于android - 如何确保在解析数组时不返回重复的 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54077106/

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