在同一对象上调用时,setTag()和getTag()的值不同

时间:2019-03-15 13:20:56

标签: java android

我有一个带有listView的活动,该活动从SQLite数据库检索数据。我希望能够从屏幕上删除特定行,为此,我从数据库中检索了该行的唯一ID。我在日志中看到为每个条目检索正确的ID(例如,如果listView中有5个项目,则从12到16),但是getTag()总是获取项目在列表中的位置的ID(从0到16)。无论我添加了多少项目):

public class ViewListContentsActivity extends AppCompatActivity {

    MyDBHandler myDb;
    ArrayList<DataModel> itemsList;
    ListView listView;
    DataModel datamodel;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewcontents_layout);

       itemsList = new ArrayList<DataModel>();
       myDb = new MyDBHandler(this);

        final ArrayList<String> theList = new ArrayList<>();
        Cursor data = myDb.getListContents();
        int numRows = data.getCount();

        final ListadapterColumns adapterColumns = new ListadapterColumns(this, R.layout.list_adapter_view, itemsList);
        listView = (ListView) findViewById(R.id.databaseList);
        listView.setAdapter(adapterColumns);

            while(data.moveToNext()){
                //retrieve data from specific columns from database
                datamodel = new DataModel(data.getString(1), data.getString(2), data.getString(4));
                itemsList.add(datamodel);
                //get id in database
                long id = data.getLong(0);
                listView.setTag(id);
                Log.e(TAG, "my_tag" + id);
            }

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                itemsList.remove(position);
                removeItem((long) listView.getTag());
                adapterColumns.notifyDataSetChanged();
                Log.e(TAG, "delete_tag" + id);
                return false;
            }
        });
    }

这些是removeItem()和deleteData()方法:

 public void removeItem(long id) {
            myDb.deleteData(id);
}

public void deleteData(long id){
    SQLiteDatabase db = getWritableDatabase();
    String query = "DELETE FROM " + TABLE_NAME + " WHERE " +
            COLUMN_ID + "='" + id + "'";
    db.execSQL(query);

错误是此代码混乱地删除了行,我从5中选择了第3项,并删除了第2或第4项。

有人可以帮我了解我在想什么吗?为什么我的方法没有从数据库中删除我选择的确切项目?

1 个答案:

答案 0 :(得分:0)

在这里,您正在将标签设置为列表视图。这是不正确的。 您需要做的是在列表行项目中设置视图标签。检查下面的示例代码。 (在您的adapater视图中)

var i = Expression.Parameter(typeof(int), "i");
var cnt = Expression.Variable(typeof(int), "cnt");
var sum = Expression.Variable(typeof(int), "sum");

var writeLineMethod = typeof(Console).GetMethod("WriteLine", new[] { typeof(object) });

var breakLabel = Expression.Label("break");
var loop = Expression.Loop(
    Expression.Block(
        Expression.IfThen(
            Expression.GreaterThanOrEqual(cnt, i),
            Expression.Block(
                Expression.Call(writeLineMethod, Expression.Convert(sum, typeof(object))),
                Expression.Break(breakLabel))),
        Expression.AddAssign(sum, cnt),
        Expression.PostIncrementAssign(cnt)),
    breakLabel);

var block = Expression.Block(new[] { cnt, sum },
    Expression.Assign(cnt, Expression.Constant(0)),
    Expression.Assign(sum, Expression.Constant(0)),
    loop,
    sum);

var method = Expression.Lambda<Func<int, int>>(block, new[] { i }).Compile();

在您的长按侦听器中,检查以下代码。

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    // your code
    convertView.setTag(posistion);      
    return convertView;
}