Android 使用recyclerview,在手机再次锁定和解锁之前,ftp的内容不会显示在屏幕上
我想使用回收器视图显示ftp中的内容。但是,除非在应用程序运行时锁定和解锁手机一次,否则数据不会反映在视图中。我想在应用程序启动后立即使用recycler视图显示数据。请帮忙 这就是我的代码的样子 MainActivity.javaAndroid 使用recyclerview,在手机再次锁定和解锁之前,ftp的内容不会显示在屏幕上,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,我想使用回收器视图显示ftp中的内容。但是,除非在应用程序运行时锁定和解锁手机一次,否则数据不会反映在视图中。我想在应用程序启动后立即使用recycler视图显示数据。请帮忙 这就是我的代码的样子 MainActivity.java package com.example.nishant.ftp1; import android.annotation.SuppressLint; import android.content.res.Configuration; import android.o
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();
}
}
}
}
显示
<?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>
布局/活动\u 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/layout\u recycler\u 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>
下载适配器后,您不会在适配器中设置数据。因此,
adapter.notifyDataSetChanged()
在这一点上是无用的。
在适配器中创建一个方法以设置数据:
public void setData(ArrayList<FileListObject> fileList) {
this.fileList = fileList;
}
public void setData(ArrayList文件列表){
this.fileList=文件列表;
}
然后在调用
adapter.notifyDataSetChanged()
之前调用它。尝试adapter.notifyDataSetChanged()。
在asyncTask中,在onPostExecute或onPreExecute方法中执行与ui相关的操作。尝试adapter.notifyDataSetChanged()代码>在asyncTask的OnPostExecutue中。它成功了,谢谢。我添加它作为答案。
<?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>
public void setData(ArrayList<FileListObject> fileList) {
this.fileList = fileList;
}