0

我的应用程序有问题。

我有一个网站,我有一个排名表。我想从这张表中得到所有的用户名、积分和奖金。

我使用 Asynctask 通过 Jsoup 将 HttpRequest 发送到我的网站。

解析我的 html 字符串后,我将所有用户名、积分和奖金保存在 3 个数组中。

好的,现在我想创建一个动态表,并在 main_activity.xml 中使用 TableLayout

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myTableLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
</TableLayout>

这是应用程序代码:

package it.android.mypackage;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tv;
    TableLayout chartTable;
    String[] users;
    String[] points;
    String[] bonuses;

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

        chartTable = (TableLayout) findViewById(R.id.myTableLayout);

        String pathURL = "http://mywebsite.com/chart.aspx";

        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(pathURL);

    }

    class DownloadWebPageTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            String url = urls[0];
            Document doc = null;

            try {
                doc = Jsoup.connect(url).get();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            response = doc.toString();
            return response;
        }

        @Override
        protected void onPostExecute(String result) {
            Document doc = null;

            doc = Jsoup.parse(result);

            Elements listUsers = doc
                    .select("tr[style=border-bottom: 2px solid black;] td:eq(2)");
            Elements listPoints = doc
                    .select("tr[style=border-bottom: 2px solid black;] td:eq(3)");
            Elements listBonuses = doc
                    .select("tr[style=border-bottom: 2px solid black;] td:eq(4)");


                users = (listUsers.text()).split(" ");
                points = (listPoints.text()).split(" ");
                bonuses = (listBonuses.text()).split(" ");

            int i;
            for (i = 0; i < 100; i++) {

                TableRow tr = new TableRow(MainActivity.this);
                tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                TextView positiontv = new TextView(MainActivity.this);
                positiontv.setText(i+1);
                positiontv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                TextView usernametv = new TextView(MainActivity.this);
                usernametv.setText(users[i]);
                usernametv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


                TextView pointstv = new TextView(MainActivity.this);
                pointstv.setText(points[i]);
                pointstv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


                TextView bonusestv = new TextView(MainActivity.this);
                bonusestv.setText(bonuses[i]);
                bonusestv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                tr.addView(positiontv);
                tr.addView(usernametv);
                tr.addView(pointstv);
                tr.addView(bonusestv);

                chartTable.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            }

        }
    }
}

当我调试我的应用程序时,我有一个异常,在 LogCat 中我可以看到:

09-06 14:58:21.230: W/ResourceType(29801): No package identifier when getting value for resource number 0x00000001

我有这个错误

positiontv.setText(i+1);

以及 setText() 方法中的其他三个 TextView。

usernametv.setText(users[i]);
pointstv.setText(points[i]);
bonusestv.setText(bonuses[i]);

我不明白问题出在哪里。

谁来帮帮我?

4

1 回答 1

0

通过替换解决:

positiontv.setText("" + i+1);
usernametv.setText("" + users[i]);
pointstv.setText("" + points[i]);
bonusestv.setText("" + bonuses[i]);

:)

于 2012-09-07T18:42:31.887 回答