我想在Android编程上编写自定义ListView代码 这是我的RowObject类:
public class RowObject {
public int icon;
public String title;
public RowObject() {
super();
}
public RowObject(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}
}
My RowAdapter类:
public class RowAdapter extends ArrayAdapter<RowObject> {
Context _context;
int _layoutResourceId;
RowObject[] _arrayData;
public RowAdapter(Context context, int layoutResourceId,
RowObject[] arrayData) {
super(context, layoutResourceId, arrayData);
this._context = context;
this._layoutResourceId = layoutResourceId;
this._arrayData = arrayData;
}
public View getView(int position, View convertView, ViewGroup parent) {
View _row = convertView;
RowObjectViewHolder _viewHolder = null;
if (_row == null) {
LayoutInflater _inflater = ((Activity) _context)
.getLayoutInflater();
_row = _inflater.inflate(_layoutResourceId, parent, false);
_viewHolder = new RowObjectViewHolder();
// _viewHolder._imgIcon = (ImageView) _row.findViewById(R.id.imgIcon);
_viewHolder._txtTitle = (TextView) _row.findViewById(R.id.txtTitle);
_viewHolder._cbxCheck = (CheckBox) _row.findViewById(R.id.cbxCheck);
_row.setTag(_viewHolder);
} else {
_viewHolder = (RowObjectViewHolder) _row.getTag();
}
RowObject _objRow = _arrayData[position];
_viewHolder._txtTitle.setText(_objRow.title);
// _viewHolder._imgIcon.setImageResource(_objRow.icon);
_viewHolder._cbxCheck.setChecked(false);
return _row;
}
static class RowObjectViewHolder {
//ImageView _imgIcon;
TextView _txtTitle;
CheckBox _cbxCheck;
}
}
在MainActivity类中,我调用RowAdaper类。没关系,我有一个自定义的ListView:
public class MainActivity extends Activity {
// Alt + Shift + S: override method
private ListView _listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RowObject _arrayRowObject[] = new RowObject[] {
new RowObject(R.drawable.facebook, "Facebook"),
new RowObject(R.drawable.skype, "Skype") };
RowAdapter adapter = new RowAdapter(this, R.layout.listview_item_row,
_arrayRowObject);
_listView = (ListView) findViewById(R.id.listView);
View header = (View) getLayoutInflater().inflate(
R.layout.listview_header_row, null);
_listView.addHeaderView(header);
_listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
但是,当我创建一个新的Activity(Main1Activity)时,我希望在第一次运行时调用Main1Activity。
在MainActivity类中,我有:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
因为第一次调用setContentView(R.layout.activity_main1);
Main1Activity
但是,当我插入一些代码(调用适配器)时,我没有看到ListView。什么问题?
public class Main1Activity extends Activity {
private ListView _listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
RowObject _arrayRowObject[] = new RowObject[] {
new RowObject(R.drawable.facebook, "Facebook"),
new RowObject(R.drawable.skype, "Skype") };
RowAdapter adapter = new RowAdapter(this.getBaseContext(), R.layout.listview_item_row,
_arrayRowObject);
_listView = (ListView) findViewById(R.id.listView);
View header = (View) getLayoutInflater().inflate(
R.layout.listview_header_row, null);
_listView.addHeaderView(header);
_listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main1, menu);
return true;
}
}
我想我在这一行有问题:
RowAdapter adapter = new RowAdapter(this.getBaseContext(), R.layout.listview_item_row,
_arrayRowObject);
你可以帮我解决这个问题吗?谢谢!
答案 0 :(得分:0)
如果您希望在启动应用时调用Main1Activity
,那么仅更改setContentView
中的布局是不够的。你需要在android manifest
文件中指定你的启动器活动。
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
您需要使用此意图过滤器。
例如:
在应用启动时启动MainActivity
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main1Activity"
android:label="@string/app_name" >
</activity>
</application>
或者,如果您要启动Main1Activity
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Main1Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
希望很清楚!