gpt4 book ai didi

java - 当我使用实际手机时,RecyclerView 不显示项目 View ,但在模拟器上工作正常

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:05 25 4
gpt4 key购买 nike

我的 recyclerView 没有显示项目 View ,也不知道为什么,我能够从服务器获取数据,但 recyclerView 只是没有使用 ui 填充我的数据。

这是我的适配器类,它将 itemviews 与数据绑定(bind)在一起:

public class AdapterBooking extends           RecyclerView.Adapter<RecyclerView.ViewHolder>  {

private Context context;
private LayoutInflater inflater;
List<Booking> data= Collections.emptyList();
Booking current ;

public AdapterBooking(Context context,List<Booking> data) {

this.context = context;
this.inflater= LayoutInflater.from(context);
this.data = data;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.containerbookings, parent,false);
MyHolder holder=new MyHolder(view);
return holder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyHolder myHolder = (MyHolder) holder;
current = data.get(position);

myHolder.txtAccommoName.setText(current.AccommoName);
myHolder.txtBookingDate.setText("Booking Date: " + current.BookingDate);
myHolder.txtBookingExpiry.setText("Booking Expiry: " + current.BookingDuration);
myHolder.txtBookngStatus.setText("Status " + current.AccredStatus);
myHolder.txtBookngStatus.setTextColor( ContextCompat.getColor(context, R.color.colorAccent));

Glide.with(context).load(R.drawable.the_yard_room)
.placeholder(R.drawable.ic_img_error)
.error(R.drawable.ic_img_error)
.into(myHolder.img);
}

@Override
public int getItemCount() {
return data.size();
}

class MyHolder extends RecyclerView.ViewHolder {


TextView txtAccommoName;
ImageView img;
TextView txtBookingDate;
TextView txtBookingExpiry;
TextView txtBookngStatus;
BootstrapButton btnCancel;

public MyHolder(View itemView) {
super( itemView );

txtAccommoName = (TextView)itemView.findViewById(R.id.textBookedAccommoName);
img= (ImageView) itemView.findViewById(R.id.imageBookedAccomo);
txtBookingDate = (TextView)itemView.findViewById(R.id.txtBookingDate);
txtBookingExpiry = (TextView)itemView.findViewById( R.id. txtBookingExpiry);
txtBookngStatus = (TextView)itemView.findViewById( R.id.txtBookingStatus );
btnCancel = (BootstrapButton)itemView.findViewById( R.id. btnCancelBookings);

}

这是我的 Activity 类,它从服务器获取数据:

public class BookingList extends AppCompatActivity {
List<Booking> data;
Booking booking;
RecyclerView recyclerViewBooking;
AdapterBooking mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_booking_list );

new Connect().execute();
}


private class Connect extends AsyncTask<String,String,String>
{
ProgressDialog load;
HttpURLConnection connection = null;
URL url = null;
String http = null;

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(BookingList.this);
int studID = settings.getInt("STUDENT_ID_BOOKING",0 );

public Connect() {

load = new ProgressDialog(BookingList.this);

}


@Override
protected void onPreExecute() {
super.onPreExecute();

load.setMessage("Loading...");
load.setCancelable(false);
load.show();

}

@Override
protected String doInBackground(String... params) {

http = "http://192.168.42.197:5432/WCF/BookingServices.svc/getAllBookingsByStud/"+studID;
try {
url = new URL(http);

} catch (MalformedURLException e) {
e.printStackTrace();
}

try {
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(15000);
connection.setConnectTimeout(10000);
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "Application/json");
connection.setDoInput(true);
} catch (IOException e) {
e.printStackTrace();
}

try {
int code = connection.getResponseCode();

System.out.println("Response: " + code);
if(code ==HttpURLConnection.HTTP_OK)
{
InputStream input = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line = null;

while((line=reader.readLine()) != null)
{
result.append(line);
}
System.out.println(result.toString());

return (result.toString());

}else {
return("Unsuccessful");
}

} catch (IOException e) {
e.printStackTrace();
return e.toString();
}finally {
connection.disconnect();
}


}

@Override
protected void onPostExecute(String result) {

load.dismiss();
load.dismiss();
data= new ArrayList<>();
try {

JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length();i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONObject object = jsonObject.getJSONObject("Accommodation");
JSONObject objectStudent = jsonObject.getJSONObject("Student");

booking = new Booking();

booking.Name = objectStudent.getString("Name");
booking.Surname = objectStudent.getString("Surname");
booking.StudentNumber = objectStudent.getInt("StudentNumber");
booking.AccommoName = object.getString("AccommoName");
booking.AccredStatus = object.getString("AccredStatus");
booking.BookingDate = jsonObject.getString("BookingDate");
booking.BookingDuration = jsonObject.getString("BookingDuration");
booking.BookingStatus = jsonObject.getString( "BookingStatus" );

data.add(booking);
}

recyclerViewBooking = (RecyclerView)findViewById(R.id.listOfbooks);
mAdapter = new AdapterBooking(BookingList.this,data);
recyclerViewBooking.setAdapter(mAdapter);
recyclerViewBooking.setLayoutManager(new LinearLayoutManager(BookingList.this));

} catch (Exception e) {
e.printStackTrace();
Toast.makeText(BookingList.this, e.toString(), Toast.LENGTH_LONG).show();
}


}
}

}

最佳答案

检查您的布局,确保您的 RecyclerView 不是 ScrollView 的子级。如果您需要,可以使用 NestedScrollView。

如果你设置断点,你的 onBindViewHolder 会起作用吗?

您可以尝试的另一件事:

RecyclerView有一个方法:setHasStableIds(boolean)。

@Override
public void setHasStableIds(boolean hasStableIds) {
return false;
}

另外,我认为上下文是错误的:

Activity 中:

private Activity getActivity {
return this;
}

在异步任务中:

mAdapter = new AdapterBooking(getActivity().getApplicationContext(),data);

希望这些信息对您有所帮助。

祝你好运。

关于java - 当我使用实际手机时,RecyclerView 不显示项目 View ,但在模拟器上工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com