如何创建列表视图以从Web和文件夹中获取文本

时间:2014-08-21 13:45:54

标签: android listview

我制作的列表显示了来自数据库的文本但是项目附近的图像。我需要从Android中的drawable文件夹获取的图像。它表示来自Android的数据库和图像的listview文本。

代码是:

public class Category extends Activity {
// progress dialog

    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> categoryList;

    private static String url_books = "http://10.0.2.2/project/category.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_CATEGORY = "category";
    private static final String TAG_CATEGORY_ID = "category_id";
    private static final String TAG_CATEGORY_NAME = "category_name";
    private static final String TAG_MESSAGE = "massage";

    // category JSONArray
    JSONArray category = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category);
    Typeface font1 = Typeface.createFromAsset(getAssets(),
            "font/bnazanin.TTF");
    // Hashmap for ListView
    categoryList = new ArrayList<HashMap<String, String>>();
    new LoadCategory().execute();
     ListView imagelist=(ListView) findViewById(R.id.list_image);
     imagelist.setAdapter(new imageview(this));


}
  /**
 * Background Async Task to Load category by making HTTP Request
 * */
class LoadCategory extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Category.this);
        pDialog.setMessage("Loading products. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

    }


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

        // Check your log cat for JSON reponse
        Log.d("category: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // Getting Array of category
               category = json.getJSONArray(TAG_CATEGORY);

                for (int i = 0; i < category.length(); i++) {
                    JSONObject c = category.getJSONObject(i);

                    // Storing each json item in variable
                    String category_id = c.getString(TAG_CATEGORY_ID);
                    String category_name = c.getString(TAG_CATEGORY_NAME);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_CATEGORY_ID,category_id);
                    map.put(TAG_CATEGORY_NAME, category_name);


                    // adding HashList to ArrayList
                    categoryList.add(map);

                }
                return json.getString(TAG_MESSAGE);
            } else {
                System.out.println("no category found");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }


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

        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                  ListView lv=(ListView)findViewById(R.id.list_view);
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        Category.this,categoryList,
                        R.layout.category_item, new String[] { TAG_CATEGORY_NAME},
                        new int[] { R.id.category_name });
                // updating listview
               // setListAdapter(adapter);
                lv.setAdapter(adapter);
            }
        });

}

   }

在我的代码中,我有文字但没有图像。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

在列表项中为您的需求创建一个pojo,您可以从web或local设置pojos值,然后根据您的需要使用自定义listadapter覆盖getView方法。一个很好的解释here