2014-10-09 87 views
0

我有一個工作與文本列表視圖,現在想要添加圖像與AQuery。添加ImageView與AQUERY到ListView

我已經更改了XML並添加了ImageView元素,現在想要通過AQuery添加圖片加載 直到現在,我還爲TextViews使用了一個SpecialAdapter,但我正在努力獲取圖像

到目前爲止我的代碼,我覺得我必須做些什麼與適配器現在想

aq.id(R.id.imageView).image(c.getString("URL"), false, false); 

,但究竟是什麼?

package com.schatten.p5_4; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.androidquery.AQuery; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

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

public class GetArticleList extends Activity { 
public final static String EXTRA_MESSAGE = ".MESSAGE"; 
    //JSON Node Names 
    private static final String TABLE = "Table"; 
    private static final String A_ID = "AID"; 
    private static final String A_TEXT = "ArtikelText"; 
    private static final String A_GROUP = "Fuel"; 
    private static final String A_PRICE = "Preis_qm"; 
    private static final String A_IMG_URL = "URL"; 
    private static String url = ""; 
    ListView lv; 
    TextView aid; 
    TextView articletext; 
    TextView preis_qm; 
    ImageView article_img; 
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>(); 
    JSONArray android = null; 
    private Context context; 
    private AQuery aq; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.article_list); 
    oslist = new ArrayList<HashMap<String, String>>(); 

    Intent intent = getIntent(); 
    url = GlobalDataPool.urlArticleList + intent.getStringExtra(GetArticle.EXTRA_MESSAGE); 

    new JSONParse().execute(); 
} 

private class JSONParse extends AsyncTask<String, String, JSONObject> { 
    private ProgressDialog pDialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     aid = (TextView) findViewById(R.id.tv_aid); 
     preis_qm = (TextView) findViewById(R.id.tv_preis_qm); 
     articletext = (TextView) findViewById(R.id.tv_articletext); 
     article_img = (ImageView) findViewById((R.id.list_image); 
     pDialog = new ProgressDialog(GetArticleList.this); 
     pDialog.setMessage("Getting Data ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected JSONObject doInBackground(String... args) { 
     JSONParser2 jParser = new JSONParser2(); 
     // Getting JSON from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 
     return json; 
    } 

    @Override 
    protected void onPostExecute(JSONObject json) { 
     pDialog.dismiss(); 
     try { 
      // Getting JSON Array from URL 
      android = json.getJSONArray(TABLE); 
      for (int i = 0; i < android.length(); i++) { 

       JSONObject c = android.getJSONObject(i); 
       // Storing JSON item in a Variable 
       String AID = c.getString(A_ID); 
       String ATEXT = c.getString(A_TEXT); 
       String APRICE = c.getString(A_PRICE); 
       String AIMGURL = GlobalDataPool.imageURL + c.getString("URL"); 
       // Adding value HashMap key => value 
       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put(A_ID, AID); 
       map.put(A_TEXT, ATEXT); 
       map.put(A_PRICE, APRICE); 
       map.put(A_IMG_URL, AIMGURL); 
       oslist.add(map); 
       lv = (ListView) findViewById(R.id.list); 
       SpecialAdapter adapter = new SpecialAdapter(GetArticleList.this, oslist, 
         R.layout.article_list_v, 
         new String[]{A_ID, A_TEXT, A_PRICE, A_IMG_URL}, new int[]{ 
         R.id.tv_aid, R.id.tv_articletext, R.id.tv_preis_qm, R.id.iv_url}); 


       lv.setAdapter(adapter); 


       lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
               @Override 
               public void onItemClick(AdapterView<?> parent, View view, 
                     int position, long id) { 
                //Toast.makeText(GetArticleList.this, "You Clicked at " + oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show(); 

                Intent intent = new Intent(GetArticleList.this, GetArticle.class); 
                intent.putExtra(EXTRA_MESSAGE, GlobalDataPool.urlArticle + oslist.get(+position).get("AID")); 
                startActivity(intent); 
                finish(); 
               } 
              } 
       ); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      Toast.makeText(getApplicationContext(), "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    } 
    } 
} 

更新:對不起,不知道我的當前SpecialAdapter 這裏的問題是,我想我必須要管理那裏AQuery的imagae下載。 但有人可以幫我嗎?

package com.schatten.p5_4; 

import java.util.HashMap; 
import java.util.List; 
import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.SimpleAdapter; 

public class SpecialAdapter extends SimpleAdapter { 
private int[] colors = new int[] {0x30FFFFFF, 0x30DDDDDD }; 

public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) { 
    super(context, items, resource, from, to); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = super.getView(position, convertView, parent); 
    int colorPos = position % colors.length; 
    view.setBackgroundColor(colors[colorPos]); 

    return view; 
} 
} 

回答

0

好吧,我已經使用下面的適配器修復它,它的工作原理。 將是很好的意見得到更好的編碼,我不知道如果我必須做的所有的東西在getView部分

package com.schatten.p5_4; 

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

import com.androidquery.AQuery; 

import java.util.HashMap; 
import java.util.List; 

public class SpecialAdapter extends SimpleAdapter { 
//HashMap<String, String> map = new HashMap<String, String>(); 
private int[] colors = new int[]{0x30FFFFFF, 0x30DDDDDD}; 
private List<HashMap<String, String>> data; 

public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) { 
    super(context, items, resource, from, to); 
    data = items; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = super.getView(position, convertView, parent); 
    ImageView list_image; 
    TextView aid; 
    TextView articletext; 
    TextView preis_qm; 

    if (convertView == null) { 
     LayoutInflater mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = mInflater.inflate(R.layout.article_list_v, parent, false); 
    } 

    HashMap<String, String> myitem = new HashMap<String, String>(); 
    myitem = data.get(position); 

    aid = (TextView) convertView.findViewById(R.id.tv_aid); 
    preis_qm = (TextView) convertView.findViewById(R.id.tv_preis_qm); 
    articletext = (TextView) convertView.findViewById(R.id.tv_articletext); 
    aid.setText(myitem.get("AID")); 
    articletext.setText(myitem.get("ArtikelText")); 
    preis_qm.setText(myitem.get("Preis_qm")); 

    String myUrl = myitem.get("URL"); 

    if (myUrl.toLowerCase().contains("jpg")) { 
     list_image = (ImageView) convertView.findViewById(R.id.list_image); 
     AQuery aq = new AQuery(convertView); 
     aq.id(list_image).image(myitem.get("URL"), false, false, 200, 0); 
    } 

    return convertView; 
} 

}

0

您應該在適配器的getView()方法中設置AQuery。你可以添加適配器的代碼嗎?

+0

所有我必須是你看到的定義的定義的適配器與SpecialAdapter適配器....所以我應該在那裏做什麼? – 2014-10-09 09:31:32

+0

您應該創建適配器來擴展BaseAdapter。然後在getView()方法中,您應該使用AQuery。下面是如何創建自己的適配器的示例:http://stackoverflow.com/questions/11258675/android-query-downloading-images-for-a-listview – Forin 2014-10-09 09:41:15

+0

但是沒有辦法抓住SpecialAdapter中的實際ImageView和然後在AQuery中直接處理這個請求。在此狀態下構建額外的getView聽起來非常複雜 – 2014-10-09 13:14:39