我试图从服务器获取字符串(用户名)想把它放到Recycle中。为此,我使用JSON获取数据和Asynctask生成新线程。我成功从服务器获取用户名但未能将它们放入在recycleview中。
这是我的Mainactivity.java
public class MainActivity extends AppCompatActivity {
BufferedReader reader;
private String content;
RecyclerView recyclerView;
Adapter madapter;
List<String> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ApiAsyncTask().execute();
}
private class ApiAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
getData();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
recyclerView=(RecyclerView)findViewById(R.id.rv123);
recyclerView.setLayoutManager(new LinearLayoutManager());
data.add("name");
Dataadapter madapter = new Dataadapter(data);
recyclerView.setAdapter(madapter);
}
}
private void getData(){
try {
URL url = new URL("https://jsonplaceholder.typicode.com/comments");
URLConnection conn = url.openConnection();
conn.setDoOutput(false);
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + " ");
}
// Append Server Response To Content String
content = sb.toString();
Log.e("TAG", "Response is -> " + content);
JSONArray jsonArray = new JSONArray(content);
for(int i=0;i<=4;i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
Log.e("TAG", "Username is -> " + name);}
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
这是我的适配器
public class Dataadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public TextView textView;
private List<String> data;
public Dataadapter(List<String> data) {
this.data = data;
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv);
}}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View dataView = layoutInflater.inflate(R.layout.container, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(dataView);
return myViewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
textView.setText(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
}
使用2个XML文件。一个是Mainactivity.xml,另一个是Container.xml。请帮助我如何将用户名(我从服务器获取)显示到recycleview中。现在,当我运行应用程序时,它只是仅显示布局。在该布局中没有获得任何用户名。但在我的日志中,我可以看到用户名。谢谢你
答案 0 :(得分:1)
只需将对象添加到列表
即可for(int i=0;i<=4;i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
data.add(jsonObject.getString("name"));
}
LinearLayoutManager构造函数接受一个参数,一个Context。
在你的onPostExecute中,执行此操作 -
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
答案 1 :(得分:0)
尝试使用此代码,我测试了它并且它正在运行。
此处 MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
BufferedReader reader;
RecyclerView recyclerView;
Dataadapter madapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.rv123);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
madapter = new Dataadapter();
recyclerView.setAdapter(madapter);
new ApiAsyncTask().execute();
}
private class ApiAsyncTask extends AsyncTask<Void, Void, List<String>> {
@Override
protected List<String> doInBackground(Void... voids) {
return getData();
}
@Override
protected void onPostExecute(List<String> names) {
madapter.updateData(names);
}
}
private List<String> getData() {
List<String> data = new ArrayList<>();
try {
URL url = new URL("https://jsonplaceholder.typicode.com/comments");
URLConnection conn = url.openConnection();
conn.setDoOutput(false);
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line);
}
// Append Server Response To Content String
String content = sb.toString();
Log.e("TAG", "Response is -> " + content);
JSONArray jsonArray = new JSONArray(content);
for (int i = 0; i < 50; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
Log.e("TAG", "Username is -> " + name);
data.add(name);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(TAG, "getData: " + data.size());
return data;
}
}
以及 Dataadapter.java
public class Dataadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public TextView textView;
private List<String> data;
public Dataadapter() {
data = new ArrayList<>();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View dataView = layoutInflater.inflate(R.layout.container, parent, false);
return new MyViewHolder(dataView);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
textView.setText(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
public void updateData(List<String> data) {
this.data = data;
notifyDataSetChanged();
}
}
从API端点返回的数据包含500个项目,我使用50来代替加载更快。
答案 2 :(得分:0)
您不应将持有者的textView作为适配器的局部变量,并为持有者的构造函数赋值。由于ViewHolder
可以重用并解析为Adapter.onBindViewHolder()
,因此onBindViewHolder()
方法中使用的textView不是holder中的textView(未调用ViewHolder
构造函数)。您可以将textView设为MyViewHolder
的变量,并使用holder.getTextView().setText(item)
设置文字。