我想显示我从api获取的所有联系人,并使用recyclerview进行显示。但是当我想运行代码时,它只显示一个联系人,实际上它在数组上有2个联系人。
Recyclerview适配器
package com.example.ilham.dompettebal;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class contactRecyclerAdapter extends RecyclerView.Adapter<contactRecyclerAdapter.ViewHolder> {
private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
contactRecyclerAdapter(Context context,List<String> data)
{
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.contact_recyclerview, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String teman = mData.get(position);
holder.myTextView.setText(teman);
}
@Override
public int getItemCount() {
return mData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.usernameTeman);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
主要活动包括从服务器和recyclerview获取数据
package com.example.ilham.dompettebal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements contactRecyclerAdapter.ItemClickListener {
contactRecyclerAdapter adapter;
private static final String TAG = "MainActivity";
private TextView username;
public String tokenUser;
public JSONObject namaTeman;
public JSONArray friends;
private TextView usernameFriends;
ArrayList<String> usernameFriendsProfile = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tokenUser= getIntent().getStringExtra("token");
Log.d(TAG, "tokenmainactivity" + tokenUser); //untuk log pada onerror
Log.d(TAG, "tokenhalamanselanjutnya " + tokenUser);
username = (TextView) findViewById (R.id.user);
usernameFriends = (TextView)findViewById(R.id.usernameTeman);
initData();
}
@Override
public void onItemClick(View view, int position) {
Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
}
public void initData() {
//get user
AndroidNetworking.get("http://10.0.2.2:3000/users")
.addHeaders("Authorization","Bearer "+tokenUser)
.setPriority(Priority.MEDIUM)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "onResponse: " + response); //untuk log pada onresponse
try {
JSONObject obj = new JSONObject(String.valueOf(response));
String usernameAkun = obj.getString("username");
username.setText(usernameAkun);
Log.d(TAG, "namaprofil : " + username);
JSONArray friends = obj.getJSONArray("friends");
for (int i=0;i<friends.length();i++)
{
JSONObject objek = friends.getJSONObject(i);
usernameFriendsProfile.add(objek.getString("username"));
}
Log.d(TAG, "usernametemendidalem : " + usernameFriendsProfile);
// ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(MainActivity.this,R.layout.contact_recyclerview,R.id.usernameTeman,usernameFriendsProfile);
// teman.setAdapter(arrayAdapter);
RecyclerView recyclerView = findViewById(R.id.teman);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
adapter = new contactRecyclerAdapter(MainActivity.this, usernameFriendsProfile);
adapter.setClickListener(MainActivity.this);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(ANError error) {
Log.d(TAG, "onError: Failed" + error); //untuk log pada onerror
}
});
}
}
usernameFriendsProfile的值是[falah,ilham],但在显示屏上它仅显示第一个这样的索引 联系人界面-
这是active_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/addFriendButton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@+id/searchButton"
android:src="@drawable/add_friend"/>
<ImageView
android:id="@+id/searchButton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@+id/settingButton"
android:src="@drawable/search"
/>
<ImageView
android:id="@+id/settingButton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:src="@drawable/setting"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"
android:id="@+id/myprofile"
>
<ImageView
android:id="@+id/avatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/avatar" />
<TextView
android:id="@+id/user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:text="Username" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Teman"
android:layout_below="@id/myprofile"
android:layout_marginTop="20dp"
android:paddingLeft="10dp"
android:id="@+id/contact"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/contact"
android:id="@+id/teman"
/>
</RelativeLayout>
这是在联系人recycleview xml上
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="20dp"
>
<ImageView
android:id="@+id/avatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/avatar" />
<TextView
android:id="@+id/usernameTeman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:text="Username" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
RecyclerView
定义:
如果您的应用需要根据以下内容显示元素的滚动列表: 大数据集(或经常更改的数据),则应使用 如本页所述,使用RecyclerView。
UI不能显示所有元素的最常见问题可能是:
在这种情况下,您在此处使用的每一行的项目布局为
R.layout.contact_recyclerview 。建议您在XML文件中创建android:layout_height =“ wrap_content”。