2014-05-20 16 views
-1

在我的列表視圖中顯示了textview,但我很難實現imageview。我想在文字前放置相同的圖標圖像。請幫忙 !!如何將ImageView實現到列表視圖行

//Fun.java

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.BasicHttpParams; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.params.HttpParams; 
import org.apache.http.protocol.HTTP; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 


import android.app.Activity; 
import android.widget.TextView; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.graphics.Color; 
import android.net.ParseException; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.Toast; 

public class Fun extends Activity{ 
    JSONArray jArray; 
    String result = null; 
    InputStream is = null; 
    StringBuilder sb = null; 
    ArrayList<String> al = new ArrayList<String>(); 
    String targetmonth; 
    int responseCode; 
    int listItemCount=0; 
    ListView listview ; 
// --> public static int [] prgmImages={R.drawable.ic}; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 
     listview = (ListView) findViewById(R.id.listView1); 

// -->  listview.setAdapter(new DataAdapter(Fun.this, prgmImages)); 
     new LoadData().execute(); 

     // listening to single list item on click 
     listview.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

       // selected item 
       String product = ((TextView) view.findViewById(R.id.targetmonth)).getText().toString(); 

       // Launching new Activity on selecting single List Item 
       Intent i = new Intent(getApplicationContext(), SingleListItem.class); 
       // sending data to new activity 
       i.putExtra("product", product); 
       startActivity(i); 

      } 
     }); 


    } 



    private class LoadData extends AsyncTask<Void, Void, Void> { 
     private ProgressDialog progressDialog; 
     @Override 
     // can use UI thread here 
     protected void onPreExecute() { 
     this.progressDialog = ProgressDialog.show(Fun.this, ""," Can you just see your roof..."); 
     } 
     @Override 
     protected void onPostExecute(final Void unused) { 
      try{ 

        listview.setAdapter(new DataAdapter(Fun.this,al.toArray(new String[al.size()]))); 


        this.progressDialog.dismiss(); 





       } 
       catch(Exception e){ 
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
       } 
     } 
     @Override 
     protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
      // HTTP post 
        try { 
         ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
         HttpClient httpclient = new DefaultHttpClient(); 
         try{ 
         HttpPost httppost = new HttpPost("http://10.0.0.1/app/ap.php"); 

         StringEntity se = new StringEntity("envelope",HTTP.UTF_8); 
         httppost.setEntity(se); 
         HttpParams httpParameters = new BasicHttpParams(); 
         // Set the timeout in milliseconds until a connection is established. 
         int timeoutConnection = 3000; 
         HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
         // Set the default socket timeout (SO_TIMEOUT) 
         // in milliseconds which is the timeout for waiting for data. 
         int timeoutSocket = 3000; 
         HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
         HttpResponse response = httpclient.execute(httppost); 
         HttpEntity entity = response.getEntity(); 
         is = entity.getContent(); 
         } 
         catch(Exception e){ 
          Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
         } 
         //buffered reader 
         try{ 
         BufferedReader reader = new BufferedReader(new InputStreamReader(
           is, "iso-8859-1"), 80); 
         sb = new StringBuilder(); 
         sb.append(reader.readLine() + "\n"); 
         String line = "0"; 
         while ((line = reader.readLine()) != null) { 
          sb.append(line + "\n"); 
         } 
         is.close(); 
         result = sb.toString(); 

         System.out.println("Magu data aagaya"+result); 
         } 
         catch(Exception e){ 
          Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
         } 
         try{ 
         jArray = new JSONArray(result); 
         JSONObject json_data = null; 
         for (int i = 0; i < jArray.length(); i++) { 
          json_data = jArray.getJSONObject(i); 
          targetmonth=json_data.getString("targetmonth"); 
          al.add(targetmonth); 
         } 
        } 
        catch(JSONException e){ 
         Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
        } 
        } catch (ParseException e) { 
        // Log.e("log_tag", "Error in http connection" + e.toString()); 
         Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
        } 
        catch (Exception e) { 
          // Log.e("log_tag", "Error in http connection" + e.toString()); 
           Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
          }  


      return null; 
     } 


    } 




} 

// DataAdapter的

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class DataAdapter extends BaseAdapter { 
    Context mContext; 
    private LayoutInflater mInflater; 
    String targetmonth; 
    String img; 
    //String targetamount; 
    String[] month; 
    //String[] year; 
    //String[] amount; 
    public DataAdapter(Context c, String[] month) { 
     this.month = month; 
     //this.year = year; 
     //this.amount = amount; 
     mContext = c; 
     mInflater = LayoutInflater.from(c); 
    } 
    public int getCount() { 
     return month.length; 
    } 
    public Object getItem(int position) { 
     return position; 
    } 
    public long getItemId(int position) { 
     return position; 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.customgrid, parent, false); 
      holder = new ViewHolder(); 
      holder.month = (TextView) convertView 
        .findViewById(R.id.targetmonth); 

      holder.img = (ImageView) convertView.findViewById(R.id.image_id); 
     // holder.amount = (TextView) convertView.findViewById(R.id.targetamount); 




      if (position == 0) { 
       convertView.setTag(holder); 
      } 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     try { 

      holder.month.setText(month[position]); 




    //  holder.img.setImageResource(img[position]); 
     // holder.amount.setText(amount[position]); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return convertView; 
    } 
    static class ViewHolder { 
     TextView month; 
     ImageView img; 
    // TextView year, amount; 
    } 

} 

//Customgrid.xml用於格式化的TextView和ImageView的

<?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="fill_parent" 
    android:orientation="vertical"> 
<TableLayout android:id="@+id/TableLayout01" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent"> 
     <TableRow android:id="@+id/TableRow01" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"> 
     </TableRow> 
     <LinearLayout 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" android:id="@+id/linearLayout1" android:weightSum="1"> 

    <ImageView 
     android:id="@+id/image_id" 
     android:layout_gravity="center" 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:src="@drawable/ic" /> 


      <TextView 
       android:layout_width="wrap_content" 
       android:text="hello" 
       android:layout_height="wrap_content" 
       android:id="@+id/targetmonth" 
       android:textSize="15sp" 
       android:textStyle="italic" 
       android:paddingBottom="5sp" 
       android:paddingRight="5sp" 
       android:paddingTop="5sp" 
       android:layout_gravity="left" 
       android:layout_weight="0.32" 
       android:gravity="left|center" 
       android:paddingLeft="10sp" 
       android:textColor="#ffc844"> 

      </TextView> 
     </LinearLayout> 

</TableLayout> 
</LinearLayout> 

//主.xml - listview 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="fill_parent" 
    android:orientation="vertical"> 
<TableLayout android:id="@+id/TableLayout01" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent"> 
     <TableRow android:id="@+id/TableRow01" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"> 
     </TableRow> 
     <LinearLayout 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" android:id="@+id/linearLayout1" android:weightSum="1"> 

    <ImageView 
     android:id="@+id/image_id" 
     android:layout_gravity="center" 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:src="@drawable/ic" /> 


      <TextView 
       android:layout_width="wrap_content" 
       android:text="hello" 
       android:layout_height="wrap_content" 
       android:id="@+id/targetmonth" 
       android:textSize="15sp" 
       android:textStyle="italic" 
       android:paddingBottom="5sp" 
       android:paddingRight="5sp" 
       android:paddingTop="5sp" 
       android:layout_gravity="left" 
       android:layout_weight="0.32" 
       android:gravity="left|center" 
       android:paddingLeft="10sp" 
       android:textColor="#ffc844"> 

      </TextView> 
     </LinearLayout> 

</TableLayout> 
</LinearLayout> 
+1

我不會走線槽所有的代碼,以便請解釋你的問題是什麼,發生了什麼或沒有發生。 –

+0

看到我想在我的列表活動中看到imageview和textview活動 在Fun.java textview是從MySQL服務器中提取的,但我想要將常量imageview圖標放在textview的左側。 我該怎麼做? –

回答

0

與代碼替換您的customgrid.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="fill_parent" 
    android:orientation="vertical" > 

    <TableLayout 
     android:id="@+id/TableLayout01" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <TableRow 
      android:id="@+id/TableRow01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 
     </TableRow> 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <ImageView 
       android:id="@+id/image_id" 
       android:layout_width="48dp" 
       android:layout_height="48dp" 
       android:layout_gravity="center" 
       android:src="@drawable/ic_launcher" /> 

      <TextView 
       android:id="@+id/targetmonth" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_gravity="left" 
       android:layout_weight="1" 
       android:gravity="left|center" 
       android:paddingBottom="5sp" 
       android:paddingLeft="10sp" 
       android:paddingRight="5sp" 
       android:paddingTop="5sp" 
       android:text="hello" 
       android:textColor="#ffc844" 
       android:textSize="15sp" 
       android:textStyle="italic" > 
      </TextView> 
     </LinearLayout> 
    </TableLayout> 

</LinearLayout> 

這可能會幫助你

0
LazyAdapter.java 
package com.example.androidhive; 

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

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList&lt;HashMap&lt;String, String&gt;&gt; data; 
    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList&lt;HashMap&lt;String, String&gt;&gt; d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.list_row, null); 

     TextView title = (TextView)vi.findViewById(R.id.title); // title 
     TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name 
     TextView duration = (TextView)vi.findViewById(R.id.duration); // duration 
     ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image 

     HashMap&lt;String, String&gt; song = new HashMap&lt;String, String&gt;(); 
     song = data.get(position); 

     // Setting all values in listview 
     title.setText(song.get(CustomizedListView.KEY_TITLE)); 
     artist.setText(song.get(CustomizedListView.KEY_ARTIST)); 
     duration.setText(song.get(CustomizedListView.KEY_DURATION)); 
     imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image); 
     return vi; 
    } 
} 

使用此以下鏈接嘗試http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/