为什么我的列表视图也同时显示按钮和编辑文本?

时间:2019-05-30 16:22:52

标签: java android android-layout

我的列表视图显示所有组件。 但是我只想显示消息。

这是它的图片...

Loooks very messy The pic

TextView(消息的来源)和Button在下一条消息的下方重复进行。如果有可能我可以在没有textview的情况下实现该功能,那我认为会更好。网址(googlespreadsheet)。

这是我的脚本。谢谢!

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Message">


    <TextView
        android:id="@+id/mesaj"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="7dp"
        android:layout_marginEnd="42dp"
        android:textColor="@android:color/white"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/listviewmes" />
    <EditText
        android:id="@+id/mesajtext"
        android:layout_width="298dp"
        android:layout_height="46dp"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="16dp"
        android:hint="Mesajiniz..."
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/sendbutton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mesaj"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/sendbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="7dp"
        app:layout_constraintBaseline_toBaselineOf="@+id/mesajtext"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/mesajtext"
        app:layout_constraintTop_toTopOf="parent"/>



    <ListView
        android:id="@+id/listviewmes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bck"
        android:theme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </ListView>

</android.support.constraint.ConstraintLayout>




public class Message extends AppCompatActivity {
FirebaseUser fuser;
FirebaseDatabase database;
FirebaseAuth mAuth;
EditText et;
TextView tv;
ProgressDialog pd;
ListView lview;
ListAdapter adaptery;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message);
    database = FirebaseDatabase.getInstance();
    mAuth = FirebaseAuth.getInstance();

    Button sendbtn;
    Toolbar toolbar;


    sendbtn = findViewById(R.id.sendbutton);
    //tv = findViewById(R.id.mesaj);
    et = findViewById(R.id.mesajtext);
    lview = findViewById(R.id.listviewmes);
    getItems();



    sendbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addItemToSheet();
            et.getText().clear();

        }
    });
}`

`private void addItemToSheet() {
    final String mesaj = et.getText().toString();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, "MY URL SEND TO GOOGLE SHEET",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {


                }
            },

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

                }
            }
    ) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> parmas = new HashMap<>();
            parmas.put("action", "addItem");
            parmas.put("mesaj", mesaj);

            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 queue = Volley.newRequestQueue(this);

    queue.add(stringRequest);
}


public void sendMessage(View v) {
    FirebaseUser user = mAuth.getCurrentUser();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference();
    UUID uuid = UUID.randomUUID();
    String stuuid = uuid.toString();
    String mesaj = et.getText().toString();


}

private void getItems() {

    pd = ProgressDialog.show(this, "Loading", "please wait", false, true);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, "MY URL MESSAGES GET FROM GOOGLE SHEET",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    parseItems(response);

                }

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

                }
            }
    );
    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 queue = Volley.newRequestQueue(this);

    queue.add(stringRequest);
}
private void parseItems(String jsonResposnce) {


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



    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 mesaj = jo.getString("mesaj");


            HashMap<String, String> item = new HashMap<>();
            item.put("mesaj", mesaj);

            list.add(item);


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


    `adaptery = new SimpleAdapter(this,list,R.layout.activity_message,
            new String[]{"mesaj"},new int[]{R.id.mesaj, android.R.layout.simple_list_item_1,list});


    lview.setAdapter(adaptery);
    pd.dismiss();
}}

1 个答案:

答案 0 :(得分:0)

用户使用RecyclerView而不是ListView(ListView不会释放内存,但RecyclerView是一个工作得更好的ListView),然后在适配器类中,有一个名为MyViewHolder的类,该类从RecyclerView.ViewHolder扩展 在此类中,您将所有视图都放在要设置为RecyclerView的布局中 在Adapetr类中,您有一个名为onBindViewHolder的函数,并且具有MyViewHolder的输入,您可以在此处隐藏按钮

public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i)
        {
             if(condition)
                myViewHolder.button.setVisibilty(View.GONE); 
        }

这是myViewHolder

public class MyViewHolder extends RecyclerView.ViewHolder
{
    TextView txt;
    Button btn;
    public MyViewHolder(View itemView)
    {
        super(itemView);
        txt= itemView.findViewById(R.id.txt);
        btn= itemView.findViewById(R.id.btn)
    }
}

我不知道为什么每一行代码中都有一个按钮和一个EditText,但是如果EditText是URL和用于确认操作(从URL加载数据)的按钮,则不需要在其中添加这些内容列表布局!您应该在活动上方添加一个按钮和一个EditText,并在其下方设置一个RecyclerView