如何在Android Studio中遍历ListView并获取其中一个项目的位置,而无需点击它。

huangapple go评论67阅读模式
英文:

How to go through a listview and get the position of a item in it WITHOUT click on it Android Studio

问题

Sure, here's the translation of the provided content:

我想要获取列表视图中项目的位置并在 if 语句中使用它

现在我正在使用 for 循环遍历列表视图当我找到特定的产品 ID 等于相同的产品if 语句我希望能够使用该位置来更新我的列表

for (int i = 0; i < adapterItensDoCarrinho.getCount(); i++) {
    Log.d("ENTROU", "ENTROU");
    Log.d("ID", String.valueOf(lsvCarrinhoCompras.getId()));
    if (produtoSelecionado.getId() == adapterItensDoCarrinho.getItemId(i)) {
        Log.d("ENTROU 2", "ENTROU 2");
        int novaQte = itemDoCarrinho.getQtdeSelecionada();
        novaQte++;
        itemDoCarrinho.setNome(produtoSelecionado.getNome());
        itemDoCarrinho.setQtdeSelecionada(novaQte);
        itemDoCarrinho.setPrecoItemVenda(produtoSelecionado.getPrecoProduto());
        adapterItensDoCarrinho.updateItemCarrinho(lsvCarrinhoCompras.getId(), itemDoCarrinho);
        break;
    }
    else {
        itemDoCarrinho.setNome(produtoSelecionado.getNome());
        itemDoCarrinho.setQtdeSelecionada(qtdeProduto);
        itemDoCarrinho.setPrecoItemVenda(produtoSelecionado.getPrecoProduto());
        adapterItensDoCarrinho.addItemCarrinho(itemDoCarrinho);
    }
}

希望您能理解!

英文:

I want to get the position of a item in the list view and use it in a if.

Now i am going through the listview using a for loop, and i want when i find the specfic Product Id equal to the Same Product (if clause), and when i find it i use that position to update my list

for (int i = 0; i &lt; adapterItensDoCarrinho.getCount(); i++) {
	Log.d(&quot;ENTROU&quot;, &quot;ENTROU&quot;);
	Log.d(&quot;ID&quot;, String.valueOf(lsvCarrinhoCompras.getId()));
	if (produtoSelecionado.getId() == adapterItensDoCarrinho.getItemId(i)) {
		Log.d(&quot;ENTROU 2&quot;, &quot;ENTROU 2&quot;);
		int novaQte = itemDoCarrinho.getQtdeSelecionada();
		novaQte++;
		itemDoCarrinho.setNome(produtoSelecionado.getNome());
		itemDoCarrinho.setQtdeSelecionada(novaQte);
		itemDoCarrinho.setPrecoItemVenda(produtoSelecionado.getPrecoProduto());
		adapterItensDoCarrinho.updateItemCarrinho(lsvCarrinhoCompras.getId(), itemDoCarrinho);
		break;
	}
	else {
		itemDoCarrinho.setNome(produtoSelecionado.getNome());
		itemDoCarrinho.setQtdeSelecionada(qtdeProduto);
		itemDoCarrinho.setPrecoItemVenda(produtoSelecionado.getPrecoProduto());
		adapterItensDoCarrinho.addItemCarrinho(itemDoCarrinho);
	}
}

Hope you understand it!

答案1

得分: 2

你的适配器应该处理这种行为。在适配器类中编写一个公共方法,允许您传入所选的产品 ID。无论何时在您的 Activity/Fragment/其他内容中更改所选的产品 ID,在适配器中调用该公共方法以更新产品 ID 值。然后,在适配器的 getView() 方法中,如果 ID 匹配,执行您想要为具有该 ID 的产品发生的任何特殊行为。

英文:

Your Adapter should be the one to handle this behavior. Write a public method in the Adapter class which allows you to pass in the selected product ID. Whenever the selected product ID changes in your Activity/Fragment/whatever, call the public method in the Adapter to update the product ID value. Then, in the getView() method of the Adapter, if the ID matches, perform whatever special behavior you want to have happen for the product with that ID.

huangapple
  • 本文由 发表于 2020年9月1日 08:54:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63679937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定