【问题标题】:Blank Screen on ListActivity Populated from mySQL从 mySQL 填充的 ListActivity 上的空白屏幕
【发布时间】:2012-01-07 00:55:12
【问题描述】:

这是我第一次尝试创建从远程 mySQL 数据库填充的 ListActivity。从本地 SQLite 数据库获取数据时,我的工作正常。我需要修改我的课程以从远程数据库中获取数据,所以我尝试通过遵循教程和文档来调整我的课程以做到这一点。由于我的观点和使用“getExtras”的复杂性,我无法弄清楚这一点。

我的问题是:现在有了修订,我在 LogCat 中得到了一个没有错误返回的空白列表;那么你能看出我的班级有什么问题吗?推荐?

public class QueryDisplayList extends ListActivity {

    private static final String PHP_KEY = "PHP_KEY";
    private static final String QUERY_ORDER = "QUERY_ORDER";

    JSONArray jArray;
    String result = null;
    InputStream is = null;
    StringBuilder sb = null;

    // private Object tvLabel = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        setContentView(R.layout.list_view2);

        /**
         * Get the query string from last activity and pass it to this
         * activity-----------------------------------------------------
         */
        // String p = null;
        // if (extras != null) {
        // p = extras.getString(PHP_KEY);
        // }
        loadQuery();
    }

    void loadQuery() {

        new Thread(new Runnable() {

            public void run() {

                String qO = getIntent().getStringExtra("QUERY_ORDER");
                String php = getIntent().getStringExtra("PHP_KEY");

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                // http post
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(
                            "http://10.0.2.2/App/php/" + php + qO + ".php");

                    Log.e("log_tag", "Fetched " + php + qO + ".php");

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                } catch (Exception e) {

                    Log.e("log_tag", "Error in http connection " + e.toString());
                }

                // convert response to string
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(is, "iso-8859-1"), 8);
                    sb = new StringBuilder();
                    sb.append(reader.readLine() + "\n");

                    String line = "0";
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }

                    is.close();
                    result = sb.toString();

                } catch (Exception e) {
                    Log.e("log_tag", "Error converting result " + e.toString());
                }

            }
        }).start();

        setListAdapter(new QueryAdapter(this, result));
    }

    /**
     * The Query Adaptor --------------------------------------------
     */

    private class QueryAdapter extends ArrayAdapter<String> {

        private Activity context;

        public QueryAdapter(Activity context, String result) {
            super(context, R.layout.list_view2);
            this.context = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View rowView = inflater.inflate(R.layout.list_item, null, true);

            try {
                jArray = new JSONArray(result);
                JSONObject json_data = null;

                for (int i = 0; i < jArray.length(); i++) {

                    json_data = jArray.getJSONObject(i);

                    final String tvLabel = json_data.getString("label");
                    TextView labelTxt = (TextView) convertView
                            .findViewById(R.id.label);
                    if (labelTxt != null) {
                        labelTxt.setText("(" + tvLabel + ")");
                    }

                    final String tvTitle = json_data.getString("title");
                    TextView titleTxt = (TextView) convertView
                            .findViewById(R.id.listTitle);
                    if (titleTxt != null) {
                        titleTxt.setText(tvTitle);
                    }

                    String tvDescription = json_data.getString("description");
                    TextView descriptionTxt = (TextView) convertView
                            .findViewById(R.id.caption);
                    if (descriptionTxt != null) {
                        descriptionTxt.setText(tvDescription);
                    }

                    String tvDate = json_data.getString("date");
                    TextView dateTxt = (TextView) convertView
                            .findViewById(R.id.dateAdded);
                    if (dateTxt != null) {
                        dateTxt.setText(tvDate);
                    }

                    String tvGoto = json_data.getString("gotoURL");
                    TextView gotoTxt = (TextView) convertView
                            .findViewById(R.id.dummy);
                    if (gotoTxt != null) {
                        gotoTxt.setText(tvGoto);
                    }

                    gotoTxt.setVisibility(View.GONE);
                    convertView.setTag(gotoTxt);

                    final String ni = json_data.getString("intent");

                    final ListView lv = getListView();
                    lv.setEnabled(true);
                    lv.setClickable(true);

                    /**
                     * Click Listeners --------------------------------
                     */
                    lv.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> arg0, View v,
                                int arg2, long arg3) {

                            // -- Set the domain name in the strings.xml file
                            // once
                            // the
                            // DNS is established for the website.
                            String mDomain = getResources().getString(
                                    R.string.domain);

                            String url = "";
                            url = mDomain + (String) v.getTag();

                            String intent = ni;
                            Class<?> nIntent = null;
                            try {
                                nIntent = Class
                                        .forName("com.andaerosystems.andaero.utili."
                                                + intent);
                            } catch (ClassNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            Intent i = new Intent(QueryDisplayList.this,
                                    nIntent);
                            i.putExtra("PHP_KEY", tvLabel);
                            i.putExtra("KEY_URL", url);
                            i.putExtra("KEY_SUBTITLE", tvTitle);
                            i.putExtra("KEY_LABEL", tvLabel);
                            i.putExtra("KEY_INTENT", intent);
                            i.putExtra("QUERY_ORDER", "ASC");
                            i.putExtra("KEY_YPOS", "0.0");
                            QueryDisplayList.this.startActivity(i);

                            Log.e("tag", url);
                        }
                    });
                }
            } catch (JSONException e1) {
                Toast.makeText(getBaseContext(), "No label Found",
                        Toast.LENGTH_LONG).show();
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
            return rowView;
        }
    }
}

【问题讨论】:

    标签: android mysql arrays listview listactivity


    【解决方案1】:

    我快速查看了您的代码。试试这个。

    void loadQuery() {
    
            new Thread(new Runnable() {
    
                public void run() {
    
                    String qO = getIntent().getStringExtra("QUERY_ORDER");
                    String php = getIntent().getStringExtra("PHP_KEY");
    
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    // http post
                    try {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(
                                "http://10.0.2.2/App/php/" + php + qO + ".php");
    
                        Log.e("log_tag", "Fetched " + php + qO + ".php");
    
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();
    
                    } catch (Exception e) {
    
                        Log.e("log_tag", "Error in http connection " + e.toString());
                    }
    
                    // convert response to string
                    try {
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(is, "iso-8859-1"), 8);
                        sb = new StringBuilder();
                        sb.append(reader.readLine() + "\n");
    
                        String line = "0";
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }
    
                        is.close();
                        result = sb.toString();
                       YourActivity.this.runOnUiThread(new Runnable(){
    
                @Override
                public void run() {
                    YourActivity.this.setListAdapter(new QueryAdapter(this, result));
                }});
    
                    } catch (Exception e) {
                        Log.e("log_tag", "Error converting result " + e.toString());
                    }
    
    
                }
            }).start();
    
    
        }
    

    【讨论】:

    • 谢谢!那是向前迈出的一步,但现在我遇到了致命的例外:主要java.lang.ClassCastException: com.andaerosystems.andaero.utili.QueryDisplayList$1$1 cannot be cast to android.content.Context at com.andaerosystems.andaero.utili.QueryDisplayList$QueryAdapter 不知道从这里做什么..?还有什么帮助吗?谢谢! @Nikola Despotoski
    • 你正在做一些错误的转换...检查抛出异常的行
    【解决方案2】:

    你有没有一步步调试并检查出变量的结果值?

    一个例子:在解析一个 json 文件时,一个错字或来自 getString() 的错误会停止进程。

    【讨论】:

      【解决方案3】:

      首先,您不会将您从 HttpPost 收到的新结果传递给您的适配器(因为适配器是在您获得响应之前创建的)。

      另外,getView 不适合这样使用。它的作用是在列表中创建一个项目。您正在做的是创建所有项目。您应该做的是解析数据并将包含解析数据的列表添加到适配器。

      看一下这个例子:http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-19
        • 1970-01-01
        • 2023-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-18
        相关资源
        最近更新 更多