gpt4 book ai didi

Android BaseAdapter : position not really the position?

转载 作者:太空狗 更新时间:2023-10-29 13:43:58 29 4
gpt4 key购买 nike

当我使用 BaseAdapter 的 ListView 离开屏幕时,每一行不再保持连续的位置。除了这个我不知道还能怎么解释。

如果我的 BA/LV 在屏幕上显示 4 个项目,并且我添加了一个显示每一行的 TextView 的 View ,它会显示 0、1、2、3 作为行号(这是正确的)。但是,一旦我将列表向下滚动到底部的 4 项(第 5-8 项),它就会显示为 4、5、0、1?为什么?

编辑:我确实发现如果我改变

rv = (RowView)convertView;

rv =  new RowView(mContext,(cursor.getString(2)),
(cursor.getString(5)),
cursor.getString(cursor.getColumnIndex(DBHelper.KEY_IMAGEFILENAME)),
cursor.getString(cursor.getColumnIndex(DBHelper.KEY_CITY)),position);

它可以工作,但它不会重复使用代码。所以,我想我走在正确的轨道上。我确实尝试了一些方便的方法,但这对我没有太大帮助,因为我需要在构造函数启动之前设置这些值。我是否需要创建一个新方法并在最后触发它?比如addRow方法?这也会导致它滚动得非常慢。

          @Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
//setContentView(R.layout.findlist);
//getListView().setEmptyView(findViewById(R.id.empty));
mDbHelper = new DBHelper(this);
mDbHelper.open();
cursor = mDbHelper.fetchAllLocations();
startManagingCursor(cursor);
mAdapter = new myListAdapter(this);
setListAdapter(mAdapter);

}
public class myListAdapter extends BaseAdapter {
public String testing;
public myListAdapter(Context c) {
mContext = c;
// TODO Auto-generated constructor stub
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return cursor.getCount();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


cursor.moveToPosition(position);
RowView rv;


if (convertView == null) {
rv = new RowView(mContext,(cursor.getString(2)),
(cursor.getString(5)),
cursor.getString(cursor.getColumnIndex(DBHelper.KEY_IMAGEFILENAME)),
cursor.getString(cursor.getColumnIndex(DBHelper.KEY_CITY)),position);
} else {
rv = (RowView) convertView;
try {
// I KNOW THIS SECTION IS NOT RIGHT, BUT I HAVE BEEN MESSING IN HERE
rv.setAddress(cursor.getString(2));
rv.setCity(cursor.getString(5));
rv.setFocusable(true);
rv.setClickable(true); }
catch (Exception e) {
rv = (RowView) convertView;
rv.setAddress(cursor.getString(2));
rv.setCity(cursor.getString(5));
rv.setFocusable(true);
rv.setClickable(true);
Toast mToast;
mToast = Toast.makeText(FindList.this, "Error :" + e.toString() ,
Toast.LENGTH_LONG);
mToast.show();
}
}

return rv;
}

public void addItems() {

//String[] from = new String[] { DBHelper.KEY_BUSINESSNAME, DBHelper.KEY_ADDRESS, DBHelper.KEY_CITY, DBHelper.KEY_GPSLONG, DBHelper.KEY_GPSLAT, DBHelper.KEY_IMAGEFILENAME + ""};
// create array of values of widgits
//to = new int[] { R.id.businessname, R.id.address, R.id.city, R.id.gpslong, R.id.gpslat, R.id.preview};
// Now create an array adapter and set it to display using our row from notes_row.xml


}



}


private class RowView extends LinearLayout {
private TextView mAddress;
private TextView mCity;
public ImageView mArrow;
public ImageView mPicture;
public String mPathName;
public String mDateTime;
public RowView(Context context, String title, String words, String pathName, String city, int position) {
super(context);

this.setOrientation(HORIZONTAL);
this.setVerticalGravity(16); //CENTER_VERTICAL

// Here we build the child views in code. They could also have
// been specified in an XML file.

//DISPLAY DELETE BUTTON
Button mButton = new Button(context);
mButton.setFocusable(false);
mButton.setId(position);
mButton.setBackgroundResource(R.drawable.delete3);
addView(mButton, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

TextView mTitle;
mTitle = new TextView(context);
mTitle.setText(Integer.toString(position));
addView(mTitle, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

this.setOrientation(HORIZONTAL);

try {
Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(cursor.getString(cursor.getColumnIndex(DBHelper.KEY_IMAGEFILENAME))),100, 100, true);
mPicture = new ImageView(context);
mPicture.setImageBitmap(bm);
} catch (Exception e) {
mPicture = new ImageView(context);
mPicture.setImageResource(R.drawable.noimage);
}

addView(mPicture, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

mArrow = new ImageView(context);
mArrow.setBackgroundResource(R.drawable.arrowleft3);
addView(mArrow, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

currentPosition = position;
Button button = (Button)findViewById(position);
button.setOnClickListener(mCorkyListener);

}

最佳答案

当使用 convertView 时,您会得到一个以前使用过但不再显示的 View 。您应该注意用当前项目的值重置其所有属性。否则,您在 RowView 构造函数中设置的所有值将与您第一次创建该 View 时赋予它的值保持一致。

为了更好地理解 ListViews,您必须了解它只使用一组有限的 RowViews,只是足以填充显示的数量,还有一些将在必须显示它们之前填充您的数据。

关于Android BaseAdapter : position not really the position?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/552247/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com