- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在尝试通过覆盖 onPostExecute 方法来填充 GridView 时遇到问题。我得到了一个力量接近adapter.add(oneMovie);
在 onPostExecute 方法中。
我的代码链接如下:
MainActivityFragment.java
public class MainActivityFragment extends Fragment {
MovieAdapter adapter;
public MainActivityFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater Inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
Inflater.inflate(R.menu.moviefragment, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
MovieDetails movie = new MovieDetails();
movie.execute();
return true;
}
return super.onOptionsItemSelected(item);
}
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String movieinfo = null;
private final String LOG_TAG = MovieDetails.class.getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SingleMovie[] movieList = {};
adapter = new MovieAdapter(getActivity(), Arrays.asList(movieList));
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
GridView gridview = (GridView) rootView.findViewById(R.id.gridView);
gridview.setAdapter(adapter);
return rootView;
}
public class MovieDetails extends AsyncTask<Void, Void, SingleMovie[]> { //Line number 84 according to log
@Override
protected void onPostExecute(SingleMovie[] singleMovie) {
if (singleMovie != null) {
adapter.clear();
for (int i = 0; i < singleMovie.length; i++) {
SingleMovie oneMovie = singleMovie[i];
Log.v(LOG_TAG, oneMovie.movieTitle + oneMovie.movieImage);
adapter.add(oneMovie); //Line number 92 according to log
}
}
super.onPostExecute(singleMovie);
}
private SingleMovie[] getmovieData(String movieInfo)
throws JSONException {
final String MDB_RESULT = "results";
final String MDB_TITLE = "title";
final String MDB_POSTER = "poster_path";
JSONObject moviejson = new JSONObject(movieInfo);
JSONArray movieArray = moviejson.getJSONArray(MDB_RESULT);
String baseURL = "http://image.tmdb.org/t/p/w185/";
SingleMovie[] movieDetails = new SingleMovie[5];
for (int i = 0; i < 5; i++) {
JSONObject currentMovie = movieArray.getJSONObject(i);
String movietitle = currentMovie.getString(MDB_TITLE);
String moviePosterendURL = currentMovie.getString(MDB_POSTER);
String moviePosterURL = baseURL + moviePosterendURL;
movieDetails[i] = new SingleMovie(moviePosterURL, movietitle);
}
return movieDetails;
}
@Override
protected SingleMovie[] doInBackground(Void... params) {
try {
URL url = new URL("http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=APPID");
String movieDbUrl = url.toString();
Log.v(LOG_TAG, movieDbUrl);
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuilder buffer = new StringBuilder();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
movieinfo = buffer.toString();
Log.v(LOG_TAG, movieinfo);
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}
try {
return getmovieData(movieinfo);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
MovieAdapter.java
public class MovieAdapter extends ArrayAdapter<SingleMovie>{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SingleMovie singleMoviecontent = getItem(position);
View rootView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
ImageView poster = (ImageView) rootView.findViewById(R.id.movie_poster_image);
Picasso.with(getContext()).load(singleMoviecontent.movieImage).into(poster);
TextView name = (TextView) rootView.findViewById(R.id.movie_name);
name.setText(singleMoviecontent.movieTitle);
return rootView;
}
public MovieAdapter(FragmentActivity context, List<SingleMovie> resource) {
super(context, 0, resource);
}
}
SingleMovie.java
public class SingleMovie {
String movieImage;
String movieTitle;
public SingleMovie(String image, String title){
this.movieImage = image;
this.movieTitle = title;
}
}
代码的另一部分工作得很好。我已经在各个部分记录了它,并且日志消息正如预期的那样。代码在 adapter.add(oneMovie) 处中断;在 postexecute 方法中。在这方面的任何帮助都会很棒。
谢谢
日志消息:
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: FATAL EXCEPTION: main
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: Process: io.github.the_dagger.movies, PID: 1870
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: java.lang.UnsupportedOperationException
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at java.util.AbstractList.add(AbstractList.java:404)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at java.util.AbstractList.add(AbstractList.java:425)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at io.github.the_dagger.movies.MainActivityFragment$MovieDetails.onPostExecute(MainActivityFragment.java:92)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at io.github.the_dagger.movies.MainActivityFragment$MovieDetails.onPostExecute(MainActivityFragment.java:84)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.os.AsyncTask.finish(AsyncTask.java:636)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.os.AsyncTask.access$500(AsyncTask.java:177)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.os.Looper.loop(Looper.java:139)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5308)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)
最佳答案
Arrays.asList
返回由数组支持的固定大小的 ArrayList
,不支持添加或删除等操作。变化
adapter = new MovieAdapter(getActivity(), Arrays.asList(movieList));
与
adapter = new MovieAdapter(getActivity(), new ArrayList<SingleMovie>());
和add
将按预期工作
关于java - 难以使用自定义适配器填充 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650133/
我有 3 个 AutoCompleteTextView,我想在它们上面注册 2 个 String[] 适配器。目前,我正在这样做: atw_from.setAdapter(new ArrayAdapt
我需要实现一个 recyclerView 来显示我对 Parse 的查询,所以我已经做到了: private class Pagination extends RecyclerView.OnScro
我对 BizTalk 相当陌生,目前我只是探索它的功能并了解不同部分(架构、编排、端口等)如何协同工作。我对其适配器有疑问: 不同的适配器是否已经随 BizTalk 服务器安装一起预装并准备好配置,或
我在 BizTalk 中测试 MQSC 适配器以与 Z/OS 主机上的队列通信时遇到问题。 测试场景:通过 Biztalk 发送消息时,我(强制)停止并启动主机 channel ,以模拟主机 IPL。
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我想用我的音频单元在iPhone上录制一条音频信号,该信号来自一条普通的3.5毫米音频电缆(例如,另一部iPhone作为声源)。 由于iPhone具有4端口耳机插孔,因此无法直接插入。 我尝试了不同种
[请参阅下面的更新] 我很难定义模式。我的同事说这是适配器模式。我不知道。我们陷入困境主要是因为我们想要正确命名我们的组件。 问题:是适配器模式吗?如果不是的话是什么?如果是其他事情,这是实现这个想法
我有点不熟悉Java KeyAdapter有效,并且使用 KeyAdapter 使用以下代码得到了意想不到的结果。当按下一个键而另一个键已按下时,就会出现此问题,无论 isKeyPressed() 是
我想知道如何通过 ORM 适配器使用 Node.js 在 MySQL 中创建多个表。我通过模型创建了一个表,即“us.js” module.exports = { identity: 'us'
我有一个 JavaFx 客户端。我正在使用一个具有 ObservableSet 作为字段的 bean 作为模型。我想将这些数据显示到 ListView 中,但我无法将我的字段类型更改为 Observa
我正在尝试在 native iOS 应用程序中实现基于表单的身份验证,但我需要在没有收到质询的情况下登录,我想打开一个表单并登录。我实现了一个包含 isCustomResponse 函数的 Chall
我正在尝试为我的迭代器和 const_iterator 类实现反向迭代器适配器,但遇到了一些麻烦。如果有人可以指导我解决这个问题,将不胜感激! 我的想法是我应该能够从我的 rbegin() 和 ren
使用 spring-integration-sftp,创建任意数量的入站 channel 适配器对象的推荐方法是什么?我的应用程序需要监视多个远程目录(1 到 n),直到运行时才知道。 最佳答案 当前
我正在尝试为我们自己的框架创建适配器。我们的框架使用自己的断言机制,因此我需要编写适配器。 适配器类非常简单,如下所示: public class AllureReportListener {
有没有什么方法可以使用命令行而不是使用 Worklight 控制台来部署 Worklight 适配器? (因为我的 worklight 服务器安装在 WAS 上,wsadmin 命令或类似的命令...
我想构建自己的自定义 log4j(网络)适配器来解决我的问题 that I posted here. 我查看了 log4j 的文档,但看不到开发人员在哪里/是否讨论如何执行此操作。 有人能给我指出正确
我使用消息驱动 channel 适配器从 weblogic JMS 队列接收作为字符串的 xml 消息,然后将此消息传递到 spring 集成 channel 以存储到数据库中,转换为不同的 xml,
有没有什么方法可以使用命令行而不是使用 Worklight 控制台来部署 Worklight 适配器? (因为我的 worklight 服务器安装在 WAS 上,wsadmin 命令或类似的命令...
我试图为 Android 制作一个聊天应用程序,所以我使用了 RecyclerView 。我的适配器有问题,我的聊天室收到的 JSON 响应显示为空白。我的代码是否遗漏了某些内容? 这是我的适配器类
如果这是重复的,我提前道歉。我对 Android 开发还是新手,并尝试寻找解决方案,但找不到有效的解决方案。 我正在创建一个待办事项应用程序并在我的适配器中收到此错误。 java.lang.NullP
我是一名优秀的程序员,十分优秀!