使用回收视图,ftp的内容不会显示在屏幕上,直到手机被锁定并再次解锁



我想使用回收器视图显示来自ftp的内容。但是,除非我在应用程序运行时锁定和解锁手机一次,否则数据不会反映在视图中。我想在应用程序启动后立即使用回收器视图显示数据。请帮忙!

这就是我的代码的样子

主要活动.java

package com.example.nishant.ftp1;
import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.apache.commons.net.ftp.*;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
public FTPClient mFTPClient = null;
Button but;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<FileListObject> fileList = new ArrayList<>();
private RecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerAdapter(fileList, this);
recyclerView.setAdapter(adapter);
new setUpList().execute();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
Log.v("rec","in recycler");
adapter = new RecyclerAdapter(fileList, this);
recyclerView.setAdapter(adapter);
}

public class setUpList extends AsyncTask<Object, Void, Object> {
String host = "test.rebex.net", username = "demo", password = "password";
int port = 21;
@Override
protected Object doInBackground(Object[] objects) {
try {
mFTPClient = new FTPClient();
// connecting to the host
mFTPClient.connect(host,port);
// Now check the reply code, if positive mean connection success
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
Log.v("wwww","Connected");
// Login using username & password
boolean status = mFTPClient.login(username, password);
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
String toppath=null;
try {
FTPFile[] ftpDirs = mFTPClient.listFiles("/pub/example/");
for (int i = 0; i < ftpDirs.length; i++) 
{
toppath = ftpDirs[0].getName();
Log.d("CONNECT", "Directories in the ftp server are "+ ftpDirs[i].getName());
/*FileListObject fileListObject = new FileListObject(ftpDirs[i].getName());
fileList.add(fileListObject);*/
fileList.add(new FileListObject(ftpDirs[i].getName()));
// adapter.notifyDataSetChanged();
}
adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
} catch (Exception e) {
return e;
}
return null;
}
public void onPostExecute(Object res) {
if (res instanceof Exception) {
int d = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getApplicationContext(), "" + res, d);
toast.show();
}
}
}
}

回收器适配器.java

package com.example.nishant.ftp1;
/**
* Created by Nishant on 1/24/2018.
*/

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Hitayu on 18-01-2018.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private ArrayList<FileListObject> fileList = new ArrayList<>();
private Context context;
public RecyclerAdapter(ArrayList<FileListObject> fileList, Context context) {
this.fileList = fileList;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recycler_view,parent,false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final FileListObject objec = fileList.get(position);
Log.v("mmm",objec.getName());
holder.fileText.setText(Html.fromHtml("<u>"+objec.getName()+"</u>"));
/*holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(objec.getLocation()));
context.startActivity(intent);
}
});*/
}
@Override
public int getItemCount() {
return fileList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView fileText;
public LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
fileText = (TextView) itemView.findViewById(R.id.file);
linearLayout = (LinearLayout) itemView.findViewById(R.id.layout);
}
}
}

文件列表对象.java

package com.example.nishant.ftp1;
/**
* Created by Nishant on 1/24/2018.
*/
public class FileListObject {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public FileListObject(String name) {
this.name = name;
}
}

体现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nishant.ftp1">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

布局/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F6F6F6"
android:orientation="vertical"
tools:context="com.example.nishant.ftp1.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"/>
</LinearLayout>

布局/layout_recycler_view.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/file"
android:layout_width="match_parent"
android:textColor="@color/colorPrimaryDark"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>

尝试在asyncTask 的 onPostExectue 中adapter.notifyDataSetChanged();。 在 asyncTask 中,在 onPostExecute 或 onPreExecute 方法中执行与 ui 相关的操作。

下载适配器后,不会在适配器中设置数据。因此,adapter.notifyDataSetChanged()在这一点上是没有用的。 在适配器中创建一个方法来设置数据:

public void setData(ArrayList<FileListObject> fileList) {
this.fileList = fileList;
}

然后在调用adapter.notifyDataSetChanged()之前调用它。

最新更新