android – AppCompatActivity中的setListAdapter

我有程序功能listArray扩展AppCompatActivity但我的代码错误

我的代码

NewsActivity

public class NewsActivity extends AppCompatActivity {

Toolbar toolbar;
// Progress Dialog
private ProgressDialog pDialog;

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

ArrayList<HashMap<String,String>> productsList;

// url to get all products list
private static String url_all_products = "http://localhost/update.PHP";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "updatedzc";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_VERSION = "version";
private static final String TAG_DESC = "description";

// products JSONArray
JSONArray products = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news);

    // Hashmap for ListView
    productsList = new ArrayList<HashMap<String,String>>();

    // Loading products in Background Thread
    new LoadAllProducts().execute();
}
// Response from Edit Product Activity
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    // if result code 100
    if (resultCode == 100) {
        // if result code 100 is received
        // means user edited/deleted product
        // reload this screen again
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }
}

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

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

    /**
     * getting All products 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_all_products,"GET",params);

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

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

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
                    String version = c.getString(TAG_VERSION);
                    String description = c.getString(TAG_DESC);

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

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID,id);
                    map.put(TAG_NAME,name);
                    map.put(TAG_VERSION,version);
                    map.put(TAG_DESC,description);

                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                Toast.makeText(getApplicationContext(),"doesn't have news Now",Toast.LENGTH_SHORT).show();
            }
        } 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() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                listadapter adapter = new SimpleAdapter(
                        NewsActivity.this,productsList,R.layout.list_item,new String[] { TAG_PID,TAG_NAME,TAG_VERSION,TAG_DESC},new int[] { R.id.pid,R.id.name,R.id.timestamp,R.id.txtStatusMsg });
                // updating listview
                setlistadapter(adapter);
            }
        });
    }
}
}

得到错误

setlistadapter(adapter);

像这样的错误

Error:(182,21) error: cannot find symbol method setlistadapter(listadapter)

有人可以帮帮我吗?

解决方法

如果您的活动中包含以下ListView
<ListView android:id="@+id/mainListView"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_below="@id/scan_content"/>

添加类似的东西

public ListView mainListView;

到NewsActivity和替换

setlistadapter(adapter);

mainListView = (ListView) findViewById(R.id.mainListView);  
mainListView.setAdapter(adapter);

除非我误解了这个问题,否则这应该有效.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...