2013-01-07 87 views
0

我正在使用json解析器將圖像URL和描述傳遞到我的列表視圖。現在我設法加載圖像,但我如何更改列表視圖的文本?目前它只顯示項目0,項目1等等..我如何將描述傳遞給lazyadapter?無法設置列表視圖文字

主要活動:

public class MainActivity extends Activity { 

    // CREATING JSON PARSER OBJECT 
    JSONParser jParser = new JSONParser(); 
    JSONArray guide = null; 
    ListView list; 
    LazyAdapter adapter; 
    String[] mImageIds; 
    ArrayList<String> guideList =new ArrayList<String>(); 
    ArrayList<String> descriptionList =new ArrayList<String>(); 

    // GUIDE URL 
    private static String url_guide = "http://58.185.41.178/magazine_android/get_guide.txt"; 
    private static final String TAG_GUIDES = "guides";   //the parent node of my JSON 
    private static final String TAG_DESCRIPTION = "description";   
    private static final String TAG_IMAGE = "image";   


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

    // LOADING Guide IN BACKGROUND THREAD 
     new LoadGuide().execute(); 

     list=(ListView)findViewById(R.id.list); 
     adapter=new LazyAdapter(this,guideList); 
     list.setAdapter(adapter); 

     Button b=(Button)findViewById(R.id.button1); 
     b.setOnClickListener(listener); 
    } 

    @Override 
    public void onDestroy() 
    { 
     list.setAdapter(null); 
     super.onDestroy(); 
    } 

    public OnClickListener listener=new OnClickListener(){ 
     @Override 
     public void onClick(View arg0) { 
      adapter.imageLoader.clearCache(); 
      adapter.notifyDataSetChanged(); 
     } 
    }; 

    /** 
    * Background Async Task to Load all product by making HTTP Request 
    * */ 
    class LoadGuide extends AsyncTask<String, String, String> { 

     /** 
     * getting All videos from url 
     * */ 
     protected String doInBackground(String... args) { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      // getting JSON string from URL 
      JSONObject json = jParser.makeHttpRequest(url_guide, "GET", params); 

      // CHECKING OF JSON RESPONSE 
      Log.d("All guide: ", json.toString()); 

      try { 
       guide = json.getJSONArray(TAG_GUIDES); 
       for (int i = 0; i < guide.length(); i++) { 
        JSONObject c = guide.getJSONObject(i); 

        //String title = c.getString(TAG_DESCRIPTION); 
        String image = c.getString(TAG_IMAGE); 
        String description = c.getString(TAG_DESCRIPTION); 

        guideList.add(image); 
        descriptionList.add(description); 
        System.out.println(guideList); 
        }   


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

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 

      // UPDATING UI FROM BACKGROUND THREAD 
      runOnUiThread(new Runnable() { 
       public void run() { 

        /** 
        * Updating parsed JSON data into ListView 
        * */ 
        adapter.notifyDataSetChanged(); 
       } 
      }); 


     } 

    } 


    } 

圖片適配器:

public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<String> data; 
    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<String> guideList) { 
     activity = a; 
     data=guideList; 
     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.item, null); 

     TextView text=(TextView)vi.findViewById(R.id.text);; 
     ImageView image=(ImageView)vi.findViewById(R.id.image); 
     text.setText("item "+position); 
     imageLoader.DisplayImage(data.get(position), image); 
     return vi; 
    } 
} 
+0

在'onPostExceute'你不需要'runInUIThread'這個方法總是在UI線程中運行。 – Sunny

+0

@Sunny好,謝謝你告訴我。注意:) – user1234555

回答

1

在您的適配器,你可以將這個TextView中說: 「項目1」, 「項目2」 等專。你需要做的是添加在適配器構造你的descriptionList

public LazyAdapter(Activity a, ArrayList<String> guideList, ArrayList<String> descriptionList) { 

,然後做

text.setText(descriptionList.get(position)); 
+0

謝謝。我已經解決了我的問題,但是另一個,當我做「descriptionList.add(description);」當我嘗試使用system.out.println時,它會在方括號和列表視圖中顯示「[Download Magazine]」,它也會用括號顯示它。我如何刪除它們? – user1234555

0

當你的活動呼籲adapter.notifyDataSetChanged(),迫使每一個項目的重新戰平列表。這將觸發調用getView()方法的適配器。所以,你的邏輯屬於getView()方法:

text.setText(descriptionList.get(position)); 
0

使用單一的Hashmap爲guidelist和descriptionlist,然後傳遞到lazyadapter構造。使用getview()方法中hashmap的描述部分來設置文本。

0

@ user1933630

descriptionList.add(description.subString(1,description.length()-1); 
+0

這是爲了從描述中刪除[]。我沒有50位代表在那裏發表評論。所以一個新的答案。 – SKK