2015-10-09 29 views
0

嗨,我正在開發一個Android應用程序,我顯示評論相關的ticket.but在服務器端數據庫所有評論都存儲在HTML格式。當我發送請求它我得到的所有HTML code.But我設法通過我得到的HTML響應從服務器,但圖像標記不工作

comm.setText(Html.fromHtml(cmnt[position])); 

轉換HTML代碼以正確的格式,但問題是,如果在評論圖像標籤,然後它會顯示小方塊代替的image.i想要忠實地展示那個形象。 請幫我

Single_Ticket.java

private class showcomment extends AsyncTask<String,String,String> 
{ 
    ProgressDialog pdialog; 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     Log.v("TAG", "In onPreExecute of the loading comnt."); 



    } 
    protected String doInBackground(String... args) { 

      Log.v("TAG", "In doInBackground of the loading comment. "); 

      List<NameValuePair> param = new ArrayList<NameValuePair>(); 
      param.add(new BasicNameValuePair("tktid", id)); 
      ServiceHandler sh = new ServiceHandler(); 


      String jsonStr = sh.makeServiceCall(URL_COMMENT, ServiceHandler.POST, param); 
      Log.d("Response: ", "> " + jsonStr); 
      if (jsonStr != null) 

      { 
       try { 

        contacts = new JSONArray(jsonStr); 
        int a=contacts.length(); 
        Log.v(TAG, ".................." + a); 
        if(a > 0) { 
         cmnt = new String[contacts.length()]; 
         cmnt_time = new String[contacts.length()]; 
         cmnt_by = new String[contacts.length()]; 
         for (int i = (a-1),j=0; i >= 0&& j < contacts.length(); i--,j++) { 

          JSONObject c = contacts.getJSONObject(j); 
          Log.v(TAG, "" + i); 
          String aa = c.getString("cmnt"); 
          String bb = c.getString("cmnt_time"); 
          String cc = c.getString("cmnt_by"); 

          Log.v(TAG,""+aa); 
          cmnt[i] = aa; 
          cmnt_time[i]=bb; 
          cmnt_by[i]=cc; 
          Log.v(TAG,""+cmnt[i]); 
         } 


         } 
       } catch (JSONException e) { 
        System.out.print("hiiiiiiiiiiii"); 
        e.printStackTrace(); 
       } 
      } 

      return null; 
     } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     if(cmnt!=null && cmnt.length > 0) { 
      C_adapter adapter = new C_adapter(Single_Ticket.this, cmnt,cmnt_by,cmnt_time); 
      cmntview.setAdapter(adapter); 
     } 

    } 
} 

C_adapter.java

public class C_adapter extends ArrayAdapter { 

Context context; 
String[] cmnt; 
String[] cmnt_by; 
String[] cmnt_time; 
LayoutInflater inflater; 


public C_adapter(Single_Ticket context, String[] cmnt,String[] cmnt_by,String[] cmnt_time) { 
    super(context, R.id.cmnt_list,cmnt); 
    this.context=context; 
    this.cmnt=cmnt; 
    this.cmnt_by=cmnt_by; 
    this.cmnt_time=cmnt_time; 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.cmnt_list, null); 

    } 
    TextView comm = (TextView) convertView.findViewById(R.id.cmnt); 
    TextView cmntby = (TextView) convertView.findViewById(R.id.cmntby); 

    comm.setText(Html.fromHtml(cmnt[position])); 
    if(cmnt_by[position].equalsIgnoreCase("Lalit Patil")|| cmnt_by[position].equalsIgnoreCase("Sarvesh Sonawane")) 
    { 
     //cmntby.setBackgroundResource(R.color.orange_red); 
     cmntby.setText(cmnt_time[position]+" - "+cmnt_by[position]); 

    } 
    cmntby.setText(cmnt_time[position]+" - "+cmnt_by[position]); 

    return convertView; 
} 



    } 

我從服務器響應這樣

{"cmnt":"<img src=\"http:\/\/i.imgur.com\/JHcdBVj.jpg\" width=\"448\"><br>","cmnt_time":"Wed,07 Oct 2015 05:41pm","cmnt_by":"test test"} 
+0

請幫助我.. –

+0

圖片代碼不會TextView的工作。您應該使用WebView而不是TextView。 –

回答

0

廣場ImageView的TextView在您的相同位置layout.xml文件。

然後檢查你的數據,你從服務器獲取內容,如果數據有IMG標籤然後被設置到圖像視圖中可見,並設置位圖圖像視圖否則設置可見文本視圖

我已經爲你做了一個功能。我使用Picasso在圖像視圖中設置圖像,使用畢加索在中設置圖像,我們可以在圖像視圖中僅用1行設置圖像。

public void setImage() { 
     try { 
      String response = "{\"cmnt\":\"<img src=\\\"http:\\/\\/i.imgur.com\\/JHcdBVj.jpg\\\" width=\\\"448\\\"><br>\",\"cmnt_time\":\"Wed,07 Oct 2015 05:41pm\",\"cmnt_by\":\"test test\"}"; 

      JSONObject jsonresponse = new JSONObject(response); 
      String cmnt = jsonresponse.getString("cmnt"); 
      if (cmnt.contains("<img")) { 
       textView.setVisibility(View.GONE); 
       imageView.setVisibility(View.VISIBLE); 
       Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>"); 
       Matcher m = p.matcher(cmnt); 
       if (m.find()) { 
        cmnt = m.group(1); 
       } 
       Picasso.with(this) 
         .load(cmnt) 
         .into(imageView); 
      } else { 
       textView.setVisibility(View.VISIBLE); 
       imageView.setVisibility(View.GONE); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
相關問題