我将文本设置为Textview有点麻烦,请检查以下情况
因此,我有一个片段,片段中有产品名称,价格和两个RecyclerView
(1个肖像,1个水平),然后在项目上单击水平RecyclerView,它将尝试获取该特定项目的价格并且在服务器响应时,我想将该值设置为分片的价格TextView
,但是当我显式更新值时,它返回空指针,因此我尝试在所有代码下都对我不起作用。
在片段中创建用于设置文本的方法,然后从AsyncTask
调用该方法(问题:未加载UI,这就是抛出空指针的原因)
使用Layooutinfleter
。
protected void onPostExecute(String s) {
super.onPostExecute(s);
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.fragment_product, null );
TextView tv = view.findViewById(R.id.productPricePopUp);
tv.setText(s);
System.out.println("Rohit " + tv.getText());
}
我不知道何时设置文本能正常工作,并显示我设置的文本,但不更新TextView。
是否有将文本设置为TextView的解决方案? 请帮助我找到解决方案。
水平RecyclerView位于人像RecyclerView(嵌套)中
Product.java(片段)
public class Product extends SuperBottomSheetFragment {
public TextView name,des,price;
private OnFragmentInteractionListener mListener;
public int id = 0;
public Context context;
public String server_response = null;
public int ArrayID = 0;
public RecyclerView variationRecycler;
public List<variationModel> data = new ArrayList<>();
public variationAdapter adapter;
@Override
public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view1 = inflater.inflate(R.layout.fragment_product, container, false);
final MainActivity activity;
activity = (MainActivity)getActivity();
assert activity != null;
server_response = activity.server_response;
name = view1.findViewById(R.id.productNamePopUp);
des = view1.findViewById(R.id.productDesPopUp);
price = view1.findViewById(R.id.productPricePopUp);
variationRecycler = view1.findViewById(R.id.variationRecycler);
variationRecycler.setNestedScrollingEnabled(true);
variationRecycler.setLayoutManager(new LinearLayoutManager(view1.getContext().getApplicationContext()));
variationRecycler.setHasFixedSize(true);
variationRecycler.setItemAnimator(new DefaultItemAnimator());
ArrayID = this.getArguments().getInt("id");
new ExecuteOnBack(context).execute();
return view1;
}
class ExecuteOnBack extends AsyncTask<Void,Void,Void>{
String url = null;
public Context context;
public ProgressDialog p;
@Override
protected void onPreExecute() {
super.onPreExecute();
p = new ProgressDialog(context);
p.setMessage("Please Wait...");
p.setCancelable(false);
p.show();
}
public ExecuteOnBack(Context context){
this.context = context;
}
@Override
protected Void doInBackground(Void... voids) {
try {
JSONArray array = new JSONArray(server_response);
JSONObject object1 = array.getJSONObject(ArrayID);
name.setText(object1.getString("name"));
des.setText(object1.getString("short_description"));
price.setText("Rs." + object1.getString("price"));
System.out.println("Rohit" + object1.getJSONArray("attributes").getJSONObject(0).getJSONArray("options"));
for(int k=0;k<object1.getJSONArray("attributes").length();k++){
ArrayList<optionModel> listdata = new ArrayList<optionModel>();
JSONArray jArray = object1.getJSONArray("attributes").getJSONObject(k).getJSONArray("options");
//JSONArray IDArray = object1.getJSONArray("attributes").getJSONObject(k).getJSONArray("ids");
if (jArray != null) {
for (int i=0;i<jArray.length();i++){
listdata.add(new optionModel(jArray.getString(i),174));
}
}
data.add(new variationModel(object1.getJSONArray("attributes").getJSONObject(k).getString("name"),object1.getJSONArray("attributes").getJSONObject(k).getInt("id"),listdata));
}
System.out.println("Rohit" + data.get(0).getName());
adapter = new variationAdapter(data);
variationRecycler.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
p.dismiss();
}
}
}
variationAdapter.java(Portrait Recyclerview)
class variationHolder extends RecyclerView.ViewHolder{
public TextView name;
public TextView id;
public ArrayList<String> array = new ArrayList<>();
public RecyclerView horizontal_variation;
public variationHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.variationNameFirst);
horizontal_variation = itemView.findViewById(R.id.horizontal_variation);
horizontal_variation.setLayoutManager(new LinearLayoutManager(itemView.getContext(), LinearLayout.HORIZONTAL,false));
horizontal_variation.setHasFixedSize(true);
horizontal_variation.setItemAnimator(new DefaultItemAnimator());
// id = itemView.findViewById(R.id.variationId);
}
}
public class variationAdapter extends RecyclerView.Adapter<variationHolder> {
public List<variationModel> ListOf;
public variationAdapter(List<variationModel> listOf) {
ListOf = listOf;
}
@NonNull
@Override
public variationHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.portrait_variation, parent, false);
return new variationHolder(view);
}
@Override
public void onBindViewHolder(@NonNull variationHolder holder, int position) {
holder.name.setText(ListOf.get(position).getName());
horizontalAdapter adapter = new horizontalAdapter(this.ListOf.get(position).getData());
holder.horizontal_variation.setAdapter(adapter);
System.out.println("Rohit" + ListOf.get(position).getName());
}
@Override
public int getItemCount() {
return this.ListOf.size();
}
}
horizontalAdapter.java(水平适配器)
class horizontalHolder extends RecyclerView.ViewHolder{
public TextView name,price;
public MaterialCardView variationCard;
public horizontalHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.variationName);
variationCard = itemView.findViewById(R.id.variationCard);
price = itemView.findViewById(R.id.productPricePopUp);
}
static class ExecuteBack extends AsyncTask<String,String,String>{
String server_response;
Context context;
View v;
public ExecuteBack(Context context,View v) {
this.context = context;
this.v = v;
}
@Override
protected String doInBackground(String... url1) {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(url1[0]);
urlConnection = (HttpURLConnection) url
.openConnection();
int responseCode = urlConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
server_response = readStream(urlConnection.getInputStream());
final JSONObject arr = new JSONObject(server_response);
return arr.getString("price");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.fragment_product, null );
TextView tv = view.findViewById(R.id.productPricePopUp);
tv.setText(s);
System.out.println("Rohit " + tv.getText());
}
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuffer response = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response.toString();
}
}
}
public class horizontalAdapter extends RecyclerView.Adapter<horizontalHolder> {
private ArrayList<optionModel> ListOf;
public int temp;
horizontalAdapter(ArrayList<optionModel> listOf) {
ListOf = listOf;
}
@NonNull
@Override
public horizontalHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.variation_recycler, parent, false);
return new horizontalHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final horizontalHolder holder, final int position) {
holder.name.setText(this.ListOf.get(position).getName());
holder.name.setTag(this.ListOf.get(position).getId());
if(temp ==position){
holder.variationCard.setCardBackgroundColor(Color.parseColor("#515C6F"));
holder.name.setTextColor(Color.parseColor("#FFFFFF"));
}else{
holder.variationCard.setCardBackgroundColor(Color.parseColor("#FFFFFF"));
holder.name.setTextColor(Color.parseColor("#515C6F"));
}
holder.variationCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
temp = position;
new horizontalHolder.ExecuteBack(holder.itemView.getContext(),holder.itemView).execute("url");
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return this.ListOf.size();
}
这是ui的图片
答案 0 :(得分:2)
我相信您要更新的TextView位于Fragment布局中。
假设,我将建议创建一个说UpdateTextCallback
的接口
public interface UpdateTextCallback {
public void updateText(String text);
}
将其实现为TextView
存在的Fragment类。
public class Product extends SuperBottomSheetFragment, UpdateTextCallback {
...
..
.
class ExecuteOnBack extends AsyncTask<Void,Void,Void>{
..
.
@Override
protected Void doInBackground(Void... voids) {
...
..
.
adapter = new variationAdapter(data, Product.this);
variationRecycler.setAdapter(adapter);
...
..
.
}
}
...
..
.
public void updateText(String text) {
TextView tv = findViewById(R.id.productPricePopUp);
tv.setText(s);
System.out.println("Rohit " + tv.getText());
}
}
像这样修改variantAdapter类的构造函数
public class variationAdapter extends RecyclerView.Adapter<variationHolder> {
public List<variationModel> ListOf;
private UpdateTextCallback updateTextCallback ;
public variationAdapter(List<variationModel> listOf, UpdateTextCallback updateTextCallback) {
ListOf = listOf;
this.updateTextCallback = updateTextCallback;
}
...
..
.
@Override
public void onBindViewHolder(@NonNull variationHolder holder, int position) {
holder.name.setText(ListOf.get(position).getName());
horizontalAdapter adapter = new horizontalAdapter(this.ListOf.get(position).getData(), updateTextCallback);
holder.horizontal_variation.setAdapter(adapter);
System.out.println("Rohit" + ListOf.get(position).getName());
}
...
..
.
}
修改horizontalAdapter类的构造函数,并像这样更新文本
...
..
.
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
updateTextCallback.updateText(s);
}
...
..
.
public class horizontalAdapter extends RecyclerView.Adapter<horizontalHolder> {
private ArrayList<optionModel> ListOf;
public int temp;
private UpdateTextCallback updateTextCallback ;
horizontalAdapter(ArrayList<optionModel> listOf, UpdateTextCallback updateTextCallback) {
ListOf = listOf;
this.updateTextCallback = updateTextCallback;
}
...
..
.
}
即使TextView
不在Fragment布局中,我也希望您对如何从另一个类更新文本有个好主意。