【问题标题】:How to Remove TextView BackGround from listview if value is null?如果值为空,如何从列表视图中删除 TextView 背景?
【发布时间】:2016-06-30 07:33:33
【问题描述】:

我正在制作简单的离线聊天应用程序,到目前为止一切正常,但如果值为 null,我想删除 textview 的背景。聊天有简单的左右组合。

  protected void showList(){
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            peoples = jsonObj.getJSONArray(TAG_RESULTS);

            for(int i=0;i<peoples.length();i++){
                JSONObject c = peoples.getJSONObject(i);
                HashMap<String,String> persons = new HashMap<String,String>();
                id = c.getString("sender_id");
                ids = c.getString("rec_id");
                if(id.equals(session_id)&&ids.equals("admin")) {
                     ss = c.getString("messages");
                    persons.put(TAG_MESSAGE, ss);
                    persons.put("usern", username + ":");

                    personList.add(persons);

                  }
                    if(id.equals("admin")&&ids.equals(session_id)) {
                    tt = c.getString("messages");
                        persons.put(TAG_ID, tt);
                        persons.put("admin","Admin:");
                        personList.add(persons);
                }



                adapter = new SimpleAdapter(
                        Messages.this, personList,
                        R.layout.activity_message,
                        new String[]{"admin",TAG_ID, "usern", TAG_MESSAGE },
                        new int[]{R.id.admin, R.id.id,R.id.name, R.id.messag});

                list.setAdapter(adapter);

            }


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

    }

    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String> {

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

                InputStream inputStream = null;
                String result = null;
                try {

                    String postReceiverUrl = "http://piresearch.in/gpsapp/emp_message.php";
                    //"http://progresscard.progresscard.in/progress_card/messages/get_messages.php";
                    // HttpClient
                    HttpClient httpClient = new DefaultHttpClient();

                    // post header
                    HttpPost httpPost = new HttpPost(postReceiverUrl);

                    // add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                    nameValuePairs.add(new BasicNameValuePair("user_id", session_id));
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpClient.execute(httpPost);
                    HttpEntity resEntity = response.getEntity();

                    inputStream = resEntity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    Log.i("tagconvertstr", "[" + result + "]");
                    System.out.println(e);
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){
                myJSON = result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }

activity_message.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >



    <TextView
        android:textSize="20dp"
        android:gravity="start"
        android:textAlignment="textStart"
        android:id="@+id/admin"
        android:text="admin"
        android:textColor="#fff"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:paddingBottom="2dip"
        android:paddingTop="6dip" />


    <TextView
        android:layout_below="@id/admin"
        android:textSize="20dp"
        android:gravity="start"
        android:textAlignment="textStart"
        android:id="@+id/id"
        android:background="@drawable/mystyle"
        android:textColor="#000"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:paddingTop="6dip" />


    <TextView
        android:id="@+id/name"
        android:textSize="20dp"
        android:textColor="#fff"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:paddingBottom="2dip"
        android:paddingTop="6dip"/>



    <TextView
        android:background="@drawable/mystyle"
        android:layout_below="@id/name"
        android:id="@+id/messag"
        android:textSize="20dp"
        android:textColor="#000"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:paddingTop="6dip"/>


</LinearLayout>

activity_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_background"
    android:orientation="vertical">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="Your Messages"
        android:textAlignment="center"
        android:background="#dedb36"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@null"
        android:divider="@null"
        android:transcriptMode="alwaysScroll"
        android:stackFromBottom="true"/>
    <LinearLayout

        android:gravity="bottom"
        android:id="@+id/llMsgCompose"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="horizontal"
        android:weightSum="3">

        <EditText

            android:id="@+id/inputMsg"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:background="@color/bg_msg_input"
            android:textColor="@color/text_msg_input"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"/>

        <Button
            android:id="@+id/btnSend"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/bg_btn_join"
            android:textColor="#fff"
            android:text="Send" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 不是删除TextView 的背景,而是在向列表添加值之前放置条件。如果值为null 或空白,则不要将其添加到列表中

标签: android android-layout listview android-xml


【解决方案1】:

最简单的解决方案是不要将该项目添加到具有空值的列表中。

【讨论】:

    【解决方案2】:

    当它没有价值时,你可以简单地隐藏它。

    textView.setVisibility(View.INVISIBLE);
    

    【讨论】:

    • 我用过这个,应用程序崩溃,值为空
    • 我认为您没有通过 findViewById() 方法初始化文本视图
    【解决方案3】:

    虽然不添加该项目可能是其他人所说的最佳做法。我认为这应该可以满足您的要求。

    textView.setBackgroundColor(android.R.color.transparent);
    

    在这里,您将背景设置为透明,从而移除所有存在的背景。

    【讨论】:

      【解决方案4】:
      textView.setBackgroundColor(
        isEmpty(textView.getText().toString()) ? android.R.color.transparent: R.color.yourColor
      );
      

      如果文本为空,则将textviewBackground 设置为透明。否则它会显示你的颜色,通过使用条件运算符。

      【讨论】:

      • 请解释你的答案。 SO不鼓励仅使用代码的答案。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 2019-06-13
      相关资源
      最近更新 更多