Кнопка перестает работать после динамической настройки вида прокрутки - PullRequest
0 голосов
/ 28 января 2019

У меня есть макет, подобный следующему:

<Relative Layout>
    <Scroll View>
        <Linear Layout>
          <Text View --1>
            '
            '
            '
          <Text View --n>
         <Linear Layout>
    <Scroll View>

    <Image View>
           // Clickable Image View using as button using onClickLister  
    <Image View>

<Relative Layout>

Я прагматично добавляю textViews с помощью следующего кода:

int length = list.size();
setContentView(R.layout.activity_chat);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);

for(int i=0; i<length;i++) {
        TextView textView1 = new TextView(this);
        textView1.setLayoutParams(new 
LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        textView1.setText(list.get(i).get("time")+">>>"+" 
  "+list.get(i).get("message"));
        textView1.setPadding(5, 10, 5, 10);// in pixels (left, top, right, 
 bottom)
        linearLayout.addView(textView1);

Где list - это arrayList, содержащий hashmap.

всякий раз, когда я обновляю прокрутку программно, битоны перестают работать.

Я работаю над проектом Android. Вот код:

    package com.probhakarsarkar.carkeymanager;

    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    import android.widget.ScrollView;
    import android.widget.TextView;
    import android.widget.LinearLayout.LayoutParams;
    import android.widget.Toast;

    import com.android.volley.DefaultRetryPolicy;
    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.RetryPolicy;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.StringRequest;
    import com.android.volley.toolbox.Volley;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;

    public class chatActivity extends AppCompatActivity {

        String staffID;
        ProgressDialog loading;
        ScrollView scrollView;
        ImageView send,update;
        EditText msg_bar;



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_chat);
            //data from dashboard
            Intent intent = getIntent();
            staffID = intent.getStringExtra("staffID");
            //data from prevActivity dashboard


             msg_bar =(EditText)findViewById(R.id.editText10);
            //load message from server
             getMessage(); //after execution of this, button underneath doesn't work
            //load message from server
            send = (ImageView)findViewById(R.id.imageView2);
            send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendMessage();
                }
            });
            update= (ImageView)findViewById(R.id.imageView4);
            update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getMessage();
                }
            });
        }

        private void getMessage() {

            loading =  ProgressDialog.show(this,"Fetching chat data!","please wait",false,true);

            StringRequest stringRequest = new StringRequest(Request.Method.GET, "Url",
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            parseItems(response);
                        }
                    },

                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }
            );

            int socketTimeOut = 50000;
            RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

            stringRequest.setRetryPolicy(policy);

            RequestQueue queue = Volley.newRequestQueue(this);
            queue.add(stringRequest);

        }


        private void parseItems(String jsonResposnce) {

            ArrayList<HashMap<String, String>> list = new ArrayList<>();

            try {
                JSONObject jobj = new JSONObject(jsonResposnce);
                JSONArray jarray = jobj.getJSONArray("items");


                for (int i = 0; i < jarray.length(); i++) {

                    JSONObject jo = jarray.getJSONObject(i);

                    String time = jo.getString("time");
                    String message = jo.getString("message");


                    HashMap<String, String> item = new HashMap<>();
                    item.put("time", time);
                    item.put("message", message);
                    list.add(item);


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


            int length = list.size();
            setContentView(R.layout.activity_chat);

            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);


            for(int i=0; i<length;i++) {
                TextView textView1 = new TextView(this);
                textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                textView1.setText(list.get(i).get("time")+">>>"+" "+list.get(i).get("message"));
                textView1.setPadding(5, 10, 5, 10);// in pixels (left, top, right, bottom)
                linearLayout.addView(textView1);
            }

             loading.dismiss();

        }

        //send message
        private void   sendMessage() {


           final  String msg = msg_bar.getText().toString().trim();

            final ProgressDialog loading = ProgressDialog.show(this, "Sending Message!", "Please wait");
            StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://script.google.com/macros/s/AKfycbxBvETCK7haDSLXu3glcS8BcZuSHx-3VxaMTRcBBjlNKIyBg2Wh/exec",
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                            loading.dismiss();
                            Toast.makeText(chatActivity.this,response,Toast.LENGTH_LONG).show();

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }
            ) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> parmas = new HashMap<>();

                    //here we pass params
                    parmas.put("action", "message");
                    parmas.put("id", staffID);
                    parmas.put("message", msg);
                    return parmas;
                }
            };


            int socketTimeOut = 50000;// u can change this .. here it is 50 seconds

            RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            stringRequest.setRetryPolicy(retryPolicy);

            RequestQueue queue2 = Volley.newRequestQueue(this);

            queue2.add(stringRequest);

        }

        //send message

    }

Я новичок в программировании, пожалуйста, помогитемне, как решить эту проблему.Я хочу закончить свой проект.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...