使用带有Volley Response的setText更改TextView文本

时间:2016-10-15 11:41:36

标签: android android-volley settext

我正在开发一个项目,一旦创建了片段,就需要更改textview的文本。值将来自我的数据库。我能够获取值但我不能将它传递给我的TextViews。我应该使用什么方法?我在try-catch中尝试了public class MostlyBorrowedBookLayout extends Fragment { private Button popup; private PopupWindow popupWindow; LayoutInflater layoutInflater; private RelativeLayout relativeLayout; View myView; SessionManager session; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { myView = inflater.inflate(R.layout.botd_layout, container, false); popup = (Button) myView.findViewById(R.id.btnpopup); session = new SessionManager(getActivity().getApplicationContext()); final TextView txtTitle= (TextView) myView.findViewById(R.id.txtTitle); final TextView txtAuthor= (TextView) myView.findViewById(R.id.txtAuthor); final TextView txtPublisher= (TextView) myView.findViewById(R.id.txtPublisher); final TextView txtRate = (TextView) myView.findViewById(R.id.txtRate); final TextView txtAvailability = (TextView) myView.findViewById(R.id.txtAvailability); final TextView txtBookLevel = (TextView) myView.findViewById(R.id.txtBookLexile); //ERROR HOW CAN I CALL THE VALUE FROM onResponse txtTitle.setText(bookTitle); txtAuthor.setText(bookAuthor); txtBookLevel.setText(bookLevel); txtPublisher.setText(bookPublisher); txtAvailability.setText(availability_name); // convertToString HashMap<String, Integer> user_int = session.getUserLexileNID(); Integer lexile = user_int.get(SessionManager.KEY_LEXILE); //final TextView final TextView testing = (TextView) myView.findViewById(R.id.txtBookLexile); relativeLayout = (RelativeLayout) myView.findViewById(R.id.relative); final int userLexile = Integer.parseInt(lexile.toString()); Response.Listener<String> responseListener = new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject jsonResponse = new JSONObject(response); boolean success = jsonResponse.getBoolean("success"); if (success) { String bookTitle = jsonResponse.getString("BookTitle"); String bookAuthor = jsonResponse.getString("BookAuthor"); String bookPublisher = jsonResponse.getString("BookPublisher"); int bookLevel = jsonResponse.getInt("bookLevel"); int availability_name = jsonResponse.getInt("availability_name"); //tried having the setText here but wont work }else{ Toast.makeText(getActivity(),"Mostly Borrowed Book Error", Toast.LENGTH_SHORT); } } catch (JSONException e) { e.printStackTrace(); } } }; MostlyBorrowedBookRequest mostlyBorrowedBookRequest = new MostlyBorrowedBookRequest(userLexile,responseListener); RequestQueue queue = Volley.newRequestQueue(getActivity()); queue.add(mostlyBorrowedBookRequest); popup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopUpCommentRate dialog = new PopUpCommentRate(); dialog.show(getActivity().getFragmentManager(), "my_dialog"); } }); return myView; } ,但它没有用。我非常抱歉。我只是个新手。

git branch -D <branch name>

}

0 个答案:

没有答案