マルチカラムリストのnullpointerexception
-
28-10-2019 - |
質問
私はすでに同様の質問から参照を試しましたが、適切な解決策はありません。
Webページからデータを取得し、4列の行を持つ行で構成される形式で表示しようとしています。
Webページに存在するデータ:
SBIN ;1916.00;1886.85;1.54@LT ;1315.50;1310.30;0.40@TCS ;1180.00;1178.00;0.17@AXISBANK ;1031.30;1005.95;2.52@MARUTI ;1000.35;992.35;0.81@PNB ;931.90;916.35;1.70@GAIL ;400.00;398.45;0.39@
私はそれをフォームでdiaplayしたいです
SBIN.........1916.00.....1886.85.....1.54
LT...........1315.50.....1310.30.....0.40 and so on.
私はドットを望んでいないことに注意してください、私はそれぞれの値を連続して別の列にしたいことに注意してください。
私のデータは7行で構成されています。
以下のコードを実行すると、この出力を取得します すなわち
values[0] values[1] values[2] values[3]
values[1] values[2] values[3] values[4]
values[2] values[3] values[4] values[5]
(1列目の4つのコルすべてを印刷し、次に1列目の2-4と2列目のCol1、次に2列目のCols 3-4と2列目のCol 1-2などを印刷します...)
readwebpageasynctask.java
public class ReadWebpageAsyncTask extends Activity {
private EditText ed;
private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed = (EditText) findViewById(R.id.ed);
lv = (ListView) findViewById(R.id.list);
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://abc.com/default.aspx?id=G" });
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
int sub = result.lastIndexOf('@', result.length() - 1);
String s1 = result.substring(0, sub + 2);
String temp[];
String subarr[] = new String[100];
;
Log.v("data = ", s1);
// String s = s1.replace(";", " - ");
final String arr[] = s1.split("@");
for (int i = 0; i < arr.length; i++) {
Log.v("arr" + i, arr[i] + " " + arr.length);
}
for (int i = 0; i < arr.length - 1; i++)
{
temp = arr[i].split(";");
subarr[(4 * i)] = temp[0];
subarr[(4 * i) + 1] = temp[1];
subarr[(4 * i) + 2] = temp[2];
subarr[(4 * i) + 3] = temp[3];
}
lv.setAdapter(new MyAdapter(ReadWebpageAsyncTask.this, subarr));
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/ed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search">
</EditText>
<ListView android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
myadapter.java
public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private String item1, item2, item3, item0;
int x = 0, i = 1, y = 1;
public MyAdapter(Context context, String[] values) {
super(context, R.layout.row, values);
this.context = context;
this.values = values;
}
@Override
public String getItem(int position) {
return values[position];
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row, parent, false);
TextView tv1 = (TextView) rowView.findViewById(R.id.col1);
TextView tv2 = (TextView) rowView.findViewById(R.id.col2);
TextView tv3 = (TextView) rowView.findViewById(R.id.col3);
TextView tv4 = (TextView) rowView.findViewById(R.id.col4);
if (y < 8) {
item0 = getItem(position);
Log.v("pos = ", "" + position);
item1 = getItem(position + 1);
item2 = getItem(position + 2);
item3 = getItem(position + 3);
tv1.setText(item0);
tv2.setText(item1);
tv3.setText(item2);
tv4.setText(item3);
} else {
Log.v("y= ", "" + y);
}
return rowView;
}
}
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView android:id="@+id/col1"
android:layout_width="150dip"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/col2"
android:layout_width="70dip"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/col3"
android:layout_width="70dip"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/col4"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
任意のヘルプが評価されます
解決
ListViewを使用してみてください
TableLayoutの代わりにXMLにListViewを追加し、新しいXMLにテーブルにコンテンツを配置し、ArrayAdapterを拡張してアダプターを作成し、このアダプターをListViewに設定します。
他のヒント
XMLファイルに基づいて追加されているため、Javaコードにビューを追加しないでください。あなたのJavaコードがあなたがXMLでしたことを複製しているように私には見えます...それは非常に不安定です...
Javaコードを削除して使用できない理由がわかりません setContentView
定義したXMLレイアウトを使用するには。
幅を0に設定し、すべてのTestViewsに重みを1として追加すると、行のすべてのアイテムにスペースを均等に割り当てます。
textView1.setWidth(0);
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));