首页 > 解决方案 > How to get the data from .txt files in Android

问题描述

I am making an app where I have uploaded text files in Firebase Storage and now I want to retrieve those files and display the contents of those txt files in a text view inside a recycler view. I am able to get the uri of those text files but how do I display the contents of those files.

Here is the code I have done so far

public class RecyclerViewAdapter3 extends RecyclerView.Adapter<RecyclerViewAdapter3.ViewHolder> {

Context context3;
List<ImageUploadInfo> MainImageUploadInfoList;


public RecyclerViewAdapter3(Context context, List<ImageUploadInfo> TempList) {

    this.MainImageUploadInfoList = TempList;

    this.context3 = context;
}

public RecyclerViewAdapter3.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items3, parent, false);

    ViewHolder viewHolder = new ViewHolder(view);

    return viewHolder;
}

@Override
public int getItemCount() {

    return MainImageUploadInfoList.size();
}

@Override
public void onBindViewHolder(final RecyclerViewAdapter3.ViewHolder holder, int position) {
    ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);

    Uri uri = Uri.parse(UploadInfo.getImageURL());
    Log.d("TAGGG" , ""+uri);

    holder.userText.setText(uri.toString());
    holder.imageNameTextView.setText(UploadInfo.getImageName());
}

class ViewHolder extends RecyclerView.ViewHolder {
    public TextView imageNameTextView ;
    public TextView userText;


    public ViewHolder(View itemView) {
        super(itemView);

        userText = (TextView) itemView.findViewById(R.id.textView13);

        imageNameTextView = (TextView) itemView.findViewById(R.id.caption);
    }
}
}

In this code I am setting the uri in the textview. How do I set the content of the text file into the textview?

Please help

标签: javaandroidfirebasefirebase-storage

解决方案


将文件更改为 JSON 格式。

{
     "data" : "your text here"
}

并使用通用方法下载数据和解析 json(我建议改造)。


推荐阅读