我在XML中定义了简单的自定义列表视图布局。有时在编辑此listview的内容之后以及调用方法listviewAdapter.notifyDataSetChanged()之后;我的listview被扭曲了:
在此快照中,列表视图中的第一个条目应该像其他条目一样居中。这发生在listview中的随机位置...... 这里是listview的XML定义...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Black"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".MainActivity" >
<View
android:id="@+id/remelis1"
android:layout_width="wrap_content"
android:layout_height="3dp"
android:layout_alignLeft="@+id/Gaminys"
android:layout_alignParentTop="true"
android:background="@drawable/remelis" />
<TextView
android:id="@+id/Gaminys"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/remelis1"
android:layout_centerHorizontal="true"
android:gravity="center"
android:padding="@dimen/dp2"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/Orange" />
<TextView
android:id="@+id/plotis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Gaminys"
android:layout_below="@+id/Gaminys"
android:gravity="right"
android:paddingBottom="@dimen/dp2"
android:paddingLeft="@dimen/dp2"
android:paddingRight="@dimen/dp2"
android:paddingTop="@dimen/dp2"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/Orange" />
编辑: 我以fallowing方式定义listView项目和适配器:
String[] gaminiai = new String []{ "040 319 00", "040 342 00", "040 419 00", " 040 565 00", "040 566 00", "040 792 00",
"040 847 00", " 041 082 00", "041 283 00", "041 284 00", "041 400 00", "041 800 00"};
double [] plotis = new double [] {160.0 , 190.5, 185.3 , 90.0, 252.0, 190.0, 302.3,
140.8, 55.5, 100.0 , 110.5 , 125.0 };
for (int i=0; i<=11; i++){
arrayWidith.add(new Plotis(gaminiai[i], plotis[i]));
}
widithAdapter = new myAdapter(this, R.layout.my_list_view, arrayWidith);
myListView.setAdapter(widithAdapter);
列表视图的监听器:
myListView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
arrayWidith.remove(pos);
widithAdapter.notifyDataSetChanged();
return true;
}
});
“Plotis”是我的Class representin对象,有两个字段 - string和double。 myAdapter基本上是从网上的一些教程中复制/粘贴的,它的定义如下:
public class myAdapter extends ArrayAdapter {
Context mContext;
int layoutResourceId;
ArrayList<Plotis> mData = null;
public myAdapter(Context mContext, int layoutResourceId, ArrayList<Plotis> data) {
super(mContext, layoutResourceId,
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
mData = data;
}
@Override
public View getView(int position, View convertView, ViewGroup
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
}
Plotis plotis = mData.get(position);
// get the TextView and then set the text (item name) and tag (item ID) values
TextView textViewItem = (TextView) convertView.findViewById(R.id.Gaminys);
textViewItem.setText(plotis.getStringGaminys());
textViewItem = (TextView) convertView.findViewById(R.id.plotis);
textViewItem.setText(plotis.getStringPlotis());
return convertView;
}
}
我做错了什么? 感谢。