我有一个活动4的列表,我扩展了BaseAdapter,getview看起来像这样......
public View getView(int position, View convertView, ViewGroup parent) {
View row=null;
for(int i=0;i<10;)
{
row = convertView;
if(row==null)
{
LayoutInflater inflater = mContext.getLayoutInflater();
row = inflater.inflate(R.layout.parsed,null);
}
TextView id = (TextView)row.findViewById(R.id.id);
id.setText(idvector.elementAt(position));
TextView photo = (TextView)row.findViewById(R.id.photo);
photo.setText(photovector.elementAt(position));
TextView subcategory = (TextView)row.findViewById(R.id.subcategory);
subcategory.setText(subcategoryvector.elementAt(position));
TextView name = (TextView)row.findViewById(R.id.name);
name.setText(namevector.elementAt(position));
}
return(row);
}
}
现在我的问题是列表没有给出向量的所有值,也没有滚动..如何使我的列表可滚动???
答案 0 :(得分:1)
getView
方法用于扩充,填充和返回单个列表行。
您需要做的就是:
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the individual row from the 'parsed' layout
View row = mInflater.inflate(R.layout.parsed);
// Set the properties on the inflated row
TextView id = (TextView) row.findViewById(R.id.id);
id.setText(idvector.elementAt(position));
TextView photo = (TextView) row.findViewById(R.id.photo);
photo.setText(photovector.elementAt(position));
TextView subcategory = (TextView) row.findViewById(R.id.subcategory);
subcategory.setText(subcategoryvector.elementAt(position));
TextView name = (TextView) row.findViewById(R.id.name);
name.setText(namevector.elementAt(position));
return row;
}
Ramps所说的是,不是创建你的BaseAdapter
并传入四个单独的向量,你可以创建一个Photo
个对象的Vector(或List)(或者不管你是什么)代表),每个都有id
,photo
,subcategory
和name
成员变量。即使事情更加面向对象! :)
列表不滚动;只有当列表中的项目数超过可用的屏幕空间时,它才会滚动。如果列表中只显示四个项目,则不会滚动任何内容。
您还应该检查一下如何调用BaseAdapter
超级方法(可能是您正在扩展ArrayAdapter
):您是否正在传递其中一个数据向量或其他数据结构?如果基本适配器没有您想要呈现的正确信息,则不会呈现它! :)
答案 1 :(得分:0)
我不确定,但似乎你没有把物品放到适配器上。在您的代码snipet中,您正在从不同的向量(idvector,subcategoryvector等)中检索元素。可能你应该创建一个代表项目类型的类,保存你需要的所有东西 - id,subcategory,photo)并将该项添加到你的适配器。
请检查适配器的大小。
还有一件事。似乎不需要代码中的“for”循环。
问候!
好的,这是一些例子。 RowData是我的项目的类。如您所见,我的自定义适配器扩展了ArrayAdapter&lt; RowData&gt;我可以通过调用adapter.add(rowInstance)来放置新项目。
public class CustomList extends ListActivity {
private LayoutInflater mInflater;
private Vector data;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector();
RowData rd = new RowData("item1", "description1");
data.add(rd);
rd = new RowData("item2", "description2");
data.add(rd);
rd = new RowData("item2", "description3");
data.add(rd);
CustomAdapter adapter = new CustomAdapter(this, R.layout.custom_row,R.id.item, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
RowData row = adapter.getItem(position);
Builder builder = new AlertDialog.Builder(this);
builder.setTitle(row.mItem);
builder.setMessage(row.mDescription + " -> " + position );
builder.setPositiveButton("ok", null);
builder.show();
}
/**
* Data type used for custom adapter. Single item of the adapter.
*/
private class RowData {
protected String mItem;
protected String mDescription;
RowData(String item, String description){
mItem = item;
mDescription = description;
}
@Override
public String toString() {
return mItem + " " + mDescription;
}
}
private class CustomAdapter extends ArrayAdapter < RowData > {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
//widgets displayed by each item in your list
TextView item = null;
TextView description = null;
//data from your adapter
RowData rowData= getItem(position);
//we want to reuse already constructed row views...
if(null == convertView){
convertView = mInflater.inflate(R.layout.custom_row, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
//
holder = (ViewHolder) convertView.getTag();
item = holder.getItem();
item.setText(rowData.mItem);
description = holder.getDescription();
description.setText(rowData.mDescription);
return convertView;
}
}
/**
* Wrapper for row data.
*
*/
private class ViewHolder {
private View mRow;
private TextView description = null;
private TextView item = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView getDescription() {
if(null == description){
description = (TextView) mRow.findViewById(R.id.description);
}
return description;
}
public TextView getItem() {
if(null == item){
item = (TextView) mRow.findViewById(R.id.item);
}
return item;
}
}
答案 2 :(得分:0)
public int getCount()
{
return idvector.size();
}
Thanx很多Ramps ......我要实施你所展示的程序......
答案 3 :(得分:-2)
您的意思是,如果您的ListView内容大于您显示的区域,您希望它滚动吗?如果是这样,请在要滚动的内容周围添加ScrollView。
// inside of the parent layout(most likely a Linear Layout)
<ScrollView android:layout_height="wrap_content"
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="vertical">
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="@id/android:list"
android:layout_width="wrap_content"
android:layout_height="380dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="130dip" />
</RelativeLayout>
</ScrollView>