私はListView
アンドロイドで取り組んでいます。私は一般的な方法を使用してAndroidに記入する方法を知っていますListView
。しかし、以下のようなクラスがあり、2つTextView
のaButton
とaが含まれているとしProgressBar
ます。宣言しRelativeLayout
、このレイアウトにすべてのビューを正常に追加しました。(を使用してテストしsetContentView(new ListViewItem(this).getLayout());
ましたが、基本的には問題ありません)。
public class ListViewItem {
public ListViewItem(Context context) {
this.M_speedTextView = new TextView(context);
this.M_nameTextView = new TextView(context);
this.M_progressBar = new ProgressBar(context, null,
android.R.attr.progressBarStyleHorizontal);
this.M_activityButton = new Button(context);
this.M_layout = new RelativeLayout(context);
this.M_speedTextView.setId(new Random().nextInt());
this.M_nameTextView.setId(new Random().nextInt());
this.M_progressBar.setId(new Random().nextInt());
this.M_activityButton.setId(new Random().nextInt());
this.M_layout.setId(new Random().nextInt());
}
public void setSpeed(int speed) {
this.M_speedTextView.setText(Integer.toString(speed));
}
public void setName(String name) {
this.M_nameTextView.setText(name);
}
public void setProgress(int progress) {
this.M_progressBar.setProgress(progress);
}
public void setButtonText(String text) {
this.M_activityButton.setText(text);
}
public RelativeLayout getLayout() {
// Layout setting
this.M_layout.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, 50));
this.M_layout.setBackgroundResource(android.R.color.transparent);
// Name Text setting
LayoutParams nameTextParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
nameTextParams.setMargins(5, 10, 0, 0);
nameTextParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
nameTextParams.addRule(RelativeLayout.LEFT_OF,
this.M_speedTextView.getId());
this.M_nameTextView.setTextSize(15);
this.M_nameTextView.setText("TextView");
this.M_nameTextView.setSingleLine();
this.M_nameTextView.setEllipsize(TruncateAt.END);
this.M_layout.addView(this.M_nameTextView, nameTextParams);
// Activity Button setting
LayoutParams activityParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
activityParams.setMargins(0, 5, 5, 0);
activityParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
activityParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
this.M_activityButton.setText("Start");
this.M_layout.addView(this.M_activityButton, activityParams);
// ProgressBar setting
LayoutParams progressParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
progressParams.addRule(RelativeLayout.ALIGN_BOTTOM,
this.M_activityButton.getId());
progressParams.addRule(RelativeLayout.ALIGN_LEFT,
this.M_nameTextView.getId());
progressParams.addRule(RelativeLayout.BELOW,
this.M_nameTextView.getId());
progressParams.addRule(RelativeLayout.LEFT_OF,
this.M_activityButton.getId());
this.M_layout.addView(this.M_progressBar, progressParams);
// Speed TextView setting
LayoutParams speedParam = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
speedParam.addRule(RelativeLayout.ALIGN_BASELINE,
this.M_nameTextView.getId());
speedParam.addRule(RelativeLayout.ALIGN_BOTTOM,
this.M_nameTextView.getId());
speedParam.addRule(RelativeLayout.LEFT_OF,
this.M_activityButton.getId());
this.M_speedTextView.setText("0 KiB/s");
this.M_speedTextView.setTextSize(15);
this.M_layout.addView(this.M_speedTextView, speedParam);
return this.M_layout;
}
TextView M_speedTextView;
TextView M_nameTextView;
ProgressBar M_progressBar;
Button M_activityButton;
RelativeLayout M_layout;
}
ここで、の配列を作成し、ListViewItem
これらの配列アイテムをListViewに追加したいと思います。getLayout()
( ListViewItemクラスのメソッドを使用して、これらすべてのビューを含むレイアウトを取得できます)。
これらのビューを動的に追加したいことに注意してください。それを記入するための一般的な方法を使用していません。誰かが私にこれに到達する方法を教えてもらえますか?
EDIT一般的な方法:xmlファイルをlistViewItemとして宣言し、そのsubItemを膨らませて使用し、getView
メソッドで返します。
私が現在使用しているアダプタクラスとLogCatエラーを編集します:
public class ListViewAdapter extends BaseAdapter {
private ArrayList<ListViewItem> M_items;
public ListViewAdapter(ArrayList<ListViewItem> items) {
this.M_items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return this.M_items.get(position).getLayout();
}
@Override
public int getCount() {
return this.M_items.size();
}
@Override
public ListViewItem getItem(int position) {
return this.M_items.get(position);
}
@Override
public long getItemId(int position) {
return this.M_items.get(position).getId();
}
}
LogCatエラー:
09-03 03:56:23.359: E/AndroidRuntime(9415): FATAL EXCEPTION: main
09-03 03:56:23.359: E/AndroidRuntime(9415): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.ListView.measureScrapChild(ListView.java:1189)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.ListView.measureHeightOfChildren(ListView.java:1272)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.ListView.onMeasure(ListView.java:1181)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.View.measure(View.java:8313)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.View.measure(View.java:8313)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.View.measure(View.java:8313)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.View.measure(View.java:8313)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.View.measure(View.java:8313)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.os.Looper.loop(Looper.java:130)
09-03 03:56:23.359: E/AndroidRuntime(9415): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-03 03:56:23.359: E/AndroidRuntime(9415): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 03:56:23.359: E/AndroidRuntime(9415): at java.lang.reflect.Method.invoke(Method.java:507)
09-03 03:56:23.359: E/AndroidRuntime(9415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-03 03:56:23.359: E/AndroidRuntime(9415): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-03 03:56:23.359: E/AndroidRuntime(9415): at dalvik.system.NativeStart.main(Native Method)
前もって感謝します :)