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;
    }
}
4

4 回答 4

1

在您的适配器中,您正在让 TextView 专门说“项目 1”、“项目 2”等。您需要做的是在 Adapter 构造函数中添加您的 descriptionList

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

然后做

text.setText(descriptionList.get(position));
于 2013-01-07T04:45:04.860 回答
0

当您的活动调用 adapter.notifyDataSetChanged() 时,会强制重新绘制列表中的每个项目。这将触发对适配器的 getView() 方法的调用。所以你的逻辑属于 getView() 方法:

text.setText(descriptionList.get(position));
于 2013-01-07T04:46:56.513 回答
0

对 guidelist 和 descriptionlist 使用单个 Hashmap,然后将其传递给lazyadapter 构造函数。在 getview() 方法中使用 hashmap 的描述部分来设置文本。

于 2013-01-07T04:47:17.653 回答
0

@user1933630

 descriptionList.add(description.subString(1,description.length()-1);
于 2013-01-07T05:01:19.210 回答