当一个表中没有可用数据时,我想在RecyclerView中显示(“空”)。现在,当没有可用数据时,我的当前视图将如下图所示
“ Tan Kian Guan Guan”名称保持可用的原因可能是由于用户登录后,它将在“ users”表中获取有关该用户的所有信息。
下面是我的代码。
MyHistoryList.java
public class MyHistoryList extends AppCompatActivity {
List<User> userList;
RecyclerView recyclerView;
private String approveID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_history_list);
if(getIntent() != null) {
approveID = getIntent().getStringExtra("approveType");
}
else{}
final ActionBar abar = getSupportActionBar();
View viewActionBar = getLayoutInflater().inflate(R.layout.activity_my_history_list, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
TextView tvTitle = viewActionBar.findViewById(R.id.title);
tvTitle.setText("MY HISTORY LIST");
abar.setCustomView(viewActionBar, params);
abar.setDisplayShowCustomEnabled(true);
abar.setDisplayShowTitleEnabled(false);
// abar.setDisplayHomeAsUpEnabled(true);
abar.setHomeButtonEnabled(true);
recyclerView = findViewById(R.id.recylcerView);
recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
User user = SharedPrefManager.getInstance(this).getUser();
userList = new ArrayList<>();
loadHistory(user.getBadgeid());
}
private void loadHistory(String badgeid) {
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URLs.URL_LIST+"?badgeid="+ badgeid,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject userJson = array.getJSONObject(i);
userList.add(new User(
userJson.getInt("id"),
userJson.getString("name"),
userJson.getString("badgeid"),
userJson.getString("position"),
userJson.getString("department"),
userJson.getString("factory"),
userJson.getString("reviewer"),
userJson.getString("title"),
userJson.getString("year"),
userJson.getString("month"),
userJson.getString("suggestionwill"),
userJson.getString("present"),
userJson.getString("details"),
userJson.getString("benefit"),
userJson.getString("photo"),
userJson.getString("status"),
userJson.getString("comment")
));
}
UserAdapter adapter = new UserAdapter(MyHistoryList.this, userList);
recyclerView.setAdapter(adapter);
adapter.setClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int post = recyclerView.indexOfChild(v);
User user = userList.get(post);
Intent myIntent = new Intent(MyHistoryList.this, MyHistory.class);
myIntent.putExtra("user", user);
myIntent.putExtra("approveType",approveID);
startActivity(myIntent);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
activity_my_history_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/haha"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyHistoryList">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:clickable="false"
android:focusable="false"
android:longClickable="false"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#FFFFFF" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
有人可以帮忙吗?谢谢
答案 0 :(得分:0)
尝试这样
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="@string/no_data_available" />
在您的班级文件中
@Override
public void onResponse(String response) {
if(response==null){
recyclerView.setVisibility(View.Gone)
emptyView.setVisibility(View.Visible)
}else{
try {
JSONArray array = new JSONArray(response);
//add this condition
if(array.length() == 0){
答案 1 :(得分:0)
您可以在解析数据时知道空数据,
尝试一下
编辑:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/haha"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyHistoryList">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:clickable="false"
android:focusable="false"
android:longClickable="false"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#FFFFFF" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/emptyView"
android:text="List is Empty"
android:textSize="@dimen/text_15sp"/>
</LinearLayout>
在“活动”中声明textview ID:
public class MyHistoryList extends AppCompatActivity {
List<User> userList;
TextView emptyView;
RecyclerView recyclerView;
private String approveID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_history_list);
//Declare TextView
emptyView = findiViewById(R.id.emptyView);
if(getIntent() != null) {
approveID = getIntent().getStringExtra("approveType");
}
private void loadHistory(String badgeid) {
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URLs.URL_LIST+"?badgeid="+ badgeid,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
//add this condition
if(array.length() == 0){
//display empty with your code.
emptyView.setVisibility(View.VISIBLE);
recylcerView.setVisibility(View.GONE);
}else{
emptyView.setVisibility(View.GONE);
recylcerView.setVisibility(View.VISIBLE);
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject userJson = array.getJSONObject(i);
userList.add(new User(
userJson.getInt("id"),
userJson.getString("name"),
userJson.getString("badgeid"),
userJson.getString("position"),
userJson.getString("department"),
userJson.getString("factory"),
userJson.getString("reviewer"),
userJson.getString("title"),
userJson.getString("year"),
userJson.getString("month"),
userJson.getString("suggestionwill"),
userJson.getString("present"),
userJson.getString("details"),
userJson.getString("benefit"),
userJson.getString("photo"),
userJson.getString("status"),
userJson.getString("comment")
));
}
}
UserAdapter adapter = new UserAdapter(MyHistoryList.this, userList);
recyclerView.setAdapter(adapter);
adapter.setClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int post = recyclerView.indexOfChild(v);
User user = userList.get(post);
Intent myIntent = new Intent(MyHistoryList.this, MyHistory.class);
myIntent.putExtra("user", user);
myIntent.putExtra("approveType",approveID);
startActivity(myIntent);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
这有效。尝试让我知道。编码愉快。