【发布时间】: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