在 ArrayList 适配器中检索元素
Retrieving element in an ArrayList Adapter
我创建了一个 listview
,它有一个图像和一个按钮 - 我正在使用 ArrayAdapter
查看 listview
中的项目。
当我点击按钮时,我想获得所点击项目的详细信息。
所以我尝试了以下方法:
pd = productList.get(position);
其中 productList
是 ArrayList<ProductDetails> productList;
getproductdetailsbutton
是 productListadapter extends ArrayAdapter<ProductDetails>
中的一个按钮
holder.getproductdetailsbutton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
String productID = pd.getProductID();
String productName = pd. getProductName();
Log.d("Value", " Product Details " + productID + " " + productName);
}
});
当我单击第一项的按钮时,我会在日志中获得显示在屏幕上的最后一项的详细信息。
如何获取该位置点击项目的详细信息?
谢谢!
更新:getVIEW 代码:
@Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
Typeface tf = Typeface.createFromAsset(contextValue.getAssets(), fontPath);
Typeface tf2 = Typeface.createFromAsset(contextValue.getAssets(), fontPath2);
ViewHolder holder = null;
if (convertView == null)
{
convertView = vi.inflate(R.layout.product_details_items, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.productimage);
holder.productgetdetails = (Button) convertView.findViewById(R.id.productgetdetails);
holder.productgetdetails.setTag(position);
holder.productgetdetails.setTypeface(tf2);
convertView.setTag(holder);
ProductDetails pd = productList.get(position);
if (pinexist.equalsIgnoreCase(contextValue.getString(R.string.pinexistvalue)))
{
Log.d("Value"," - ID " + pd.getProductID());
if (logdb.checkproductDetailsExists(pd.getProductID()) == 0)
{
holder.productgetdetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.e("Value", " = FINAL" + productList.get(position).getProductID());
}
});
}
else
{
holder.productgetdetails.setText(contextValue.getString(R.string.nodata));
}
}
else
{
//DO NOTHING
}
}
else
{
holder = (ViewHolder) convertView.getTag();
}
ProductDetails pd = productList.get(position);
Glide.with(getContext().getApplicationContext())
.load(pd.getProduct_image())
.placeholder(R.drawable.placeholder)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(holder.image);
return convertView;
}
我猜(实际上我有信心),你一定是将 pd
声明为 global
变量,它会根据最后显示的项目进行更新,尝试将 pd
移动到 getView()
并将其标记为 final
以便您可以在 onclick
中使用它,就像在
中一样
public void getView(){
final ProductDetails pd = productList.get(position);
// your code for click
}
展开布局以在适配器中查看时
你可以在按钮
上设置OnClickListener
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.list_layout, null,true);
final Product pd = pdList.get(position);
//....
Button btn= (Button) = rowView.findViewById(R.id.getproductdetailsbutton);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
String productID = pd.getProductID();
String productName = pd.getProductName();
Log.d("Value", " Product Details " + productID + " " + productName);
}
});
//..
return rowView;
};
或者你可以处理 ListView OnItemClickListener
我创建了一个 listview
,它有一个图像和一个按钮 - 我正在使用 ArrayAdapter
查看 listview
中的项目。
当我点击按钮时,我想获得所点击项目的详细信息。
所以我尝试了以下方法:
pd = productList.get(position);
其中 productList
是 ArrayList<ProductDetails> productList;
getproductdetailsbutton
是 productListadapter extends ArrayAdapter<ProductDetails>
holder.getproductdetailsbutton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
String productID = pd.getProductID();
String productName = pd. getProductName();
Log.d("Value", " Product Details " + productID + " " + productName);
}
});
当我单击第一项的按钮时,我会在日志中获得显示在屏幕上的最后一项的详细信息。
如何获取该位置点击项目的详细信息?
谢谢!
更新:getVIEW 代码:
@Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
Typeface tf = Typeface.createFromAsset(contextValue.getAssets(), fontPath);
Typeface tf2 = Typeface.createFromAsset(contextValue.getAssets(), fontPath2);
ViewHolder holder = null;
if (convertView == null)
{
convertView = vi.inflate(R.layout.product_details_items, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.productimage);
holder.productgetdetails = (Button) convertView.findViewById(R.id.productgetdetails);
holder.productgetdetails.setTag(position);
holder.productgetdetails.setTypeface(tf2);
convertView.setTag(holder);
ProductDetails pd = productList.get(position);
if (pinexist.equalsIgnoreCase(contextValue.getString(R.string.pinexistvalue)))
{
Log.d("Value"," - ID " + pd.getProductID());
if (logdb.checkproductDetailsExists(pd.getProductID()) == 0)
{
holder.productgetdetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.e("Value", " = FINAL" + productList.get(position).getProductID());
}
});
}
else
{
holder.productgetdetails.setText(contextValue.getString(R.string.nodata));
}
}
else
{
//DO NOTHING
}
}
else
{
holder = (ViewHolder) convertView.getTag();
}
ProductDetails pd = productList.get(position);
Glide.with(getContext().getApplicationContext())
.load(pd.getProduct_image())
.placeholder(R.drawable.placeholder)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(holder.image);
return convertView;
}
我猜(实际上我有信心),你一定是将 pd
声明为 global
变量,它会根据最后显示的项目进行更新,尝试将 pd
移动到 getView()
并将其标记为 final
以便您可以在 onclick
中使用它,就像在
public void getView(){
final ProductDetails pd = productList.get(position);
// your code for click
}
展开布局以在适配器中查看时 你可以在按钮
上设置OnClickListener
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.list_layout, null,true);
final Product pd = pdList.get(position);
//....
Button btn= (Button) = rowView.findViewById(R.id.getproductdetailsbutton);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
String productID = pd.getProductID();
String productName = pd.getProductName();
Log.d("Value", " Product Details " + productID + " " + productName);
}
});
//..
return rowView;
};
或者你可以处理 ListView OnItemClickListener