我有一个自定义适配器,当点击时,所有返回的位置都是正确的,除了第二个位置,它应该是1但是0(与第一个元素相同)。我试过使用v.getTag()而不是直接使用位置,但它仍然给我相同的结果。适配器上的一小部分背景:我的ConnectedDeviceActivity将“All”,“No Connected Device”或设备名称发送到适配器。基于单击的项目,它调用AnalysisActivity,它显示与所单击项目相关的数据的图形。这是我的代码:
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class AnalysisAdapter extends ArrayAdapter{
private List appliance = new ArrayList<>();
private List animation = new ArrayList<>();
private String[] lookupName = {"heater", "dishwasher", "charger", "fridge"};
private Context c;
public AnalysisAdapter(Context context, int resource)
{
super(context, resource);
this.c = context;
}
public void add(String newAppliance)
{
if(newAppliance.equals("All") || newAppliance.equals("No Connected Device"))
{
Log.d("Adapter", newAppliance + " added with position " + -1);
appliance.add(newAppliance);
animation.add(-1);
super.add(newAppliance);
}
else
{
int position = 0;
for (int i = 0; i < lookupName.length; i++)
{
if (lookupName[i].equals(newAppliance))
{
position = i;
i = lookupName.length;
}
}
Log.d("Adapter", newAppliance + " added with position " + position);
appliance.add(newAppliance);
animation.add(position);
super.add(newAppliance);
}
}
public boolean isAlreadyInList(String testAppliance) { return this.appliance.contains(testAppliance);}
static class RowHolder
{
TextView APPLIANCE;
ImageView ANIMATION;
}
@Override
public int getCount()
{
return this.appliance.size();
}
@Override
public Object getItem(int position)
{
return this.appliance.get(position);
}
public void remove(String removeAppliance)
{
int removePosition = appliance.indexOf(removeAppliance);
appliance.remove(removeAppliance);
animation.remove(removePosition);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
RowHolder holder;
final int currentPosition = position;
//Inflate row
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.analysis_item, parent, false);
holder = new RowHolder();
holder.APPLIANCE = (TextView) row.findViewById(R.id.analysis_item_text);
holder.ANIMATION = (ImageView) row.findViewById(R.id.animation_item_image);
holder.APPLIANCE.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < appliance.size(); i ++)
Log.d("Adapter", "All appliance in order: " + appliance.get(i));
Log.d("Adapter", appliance.get(currentPosition) + " send to analysis with position " + currentPosition );
Intent intent= new Intent(c, AnalysisActivity.class);
intent.putExtra("DEVICENAME", (String) appliance.get(currentPosition));
intent.putExtra("STARTINGACTIVITY", "ConnectedDeviceActivity");
c.startActivity(intent);
}
});
row.setTag(holder);
} else
holder = (RowHolder) row.getTag();
//Display name
String EM = (String) getItem(position);
holder.APPLIANCE.setText(EM);
int deviceID = (int) animation.get(position);
if (deviceID == 0) //heater
holder.ANIMATION.setBackgroundColor(Color.GREEN);
else if (deviceID == 1) //dishwasher
holder.ANIMATION.setBackgroundColor(Color.YELLOW);
else if (deviceID == 2) //charger
holder.ANIMATION.setBackgroundColor(Color.RED);
else if (deviceID == 3)//fridge
holder.ANIMATION.setBackgroundColor(Color.BLUE);
return row;
}
}
单击列表中的第二个元素时,日志显示以下内容(位置应为1)。结果显示我的设备列表是正确的,但点击的项目标记为0.其余的位置是正确的。也可能有必要提一下,适配器的元素是由ConnectedDeviceActivity中的侦听器添加的,它从在线数据库中获取信息。
D/DailyAdapter: All appliance in order: All
D/DailyAdapter: All appliance in order: charger
D/DailyAdapter: All appliance in order: dishwasher
D/DailyAdapter: All appliance in order: fridge
D/DailyAdapter: All appliance in order: heater
D/DailyAdapter: All send to analysis with position 0
在我的第二次尝试中,我在持有者之后添加了第一行,并使用了index而不是currentPosition。日志保持不变
holder.APPLIANCE.setTag(position);
int index = (int) v.getTag();
Log.d("Adapter", appliance.get(index) + " send to analysis with position " + index);
这是单击第3个元素时的日志(位置2):
D/Adapter: All appliance in order: All
D/Adapter: All appliance in order: charger
D/Adapter: All appliance in order: dishwasher
D/Adapter: All appliance in order: fridge
D/Adapter: All appliance in order: heater
D/Adapter: dishwasher send to analysis with position 2
答案 0 :(得分:1)
如果您必须获取列表中单击行的单击位置,则很容易获得它。改变这一行
int index = (int) v.getTag();
带
int index = position;
您正在使用此方法获取视图的位置 -
getView(int position, View convertView, ViewGroup parent)
只需将此位置用作行的位置。
此外,您需要知道 getView 方法中的 convertView 是什么,如果您检查 if(convertView == null),它何时变为null 在适配器代码中。 See the details here
答案 1 :(得分:0)
请注意,您正在此行中显示索引。
Log.d(&#34; Adapter&#34;,appliance.get(currentPosition)+&#34;发送到位置分析&#34; + index);
您的索引仅在此处更新一次
int index =(int)v.getTag();
因为当 convertView == null true 时, v.setTag()只设置一次(位置始终为0)这里)
现在,这是v.imp -
所有这些代码都保存在此
中row.setTag(保持器);
并像这样检索 -
holder =(RowHolder)row.getTag();
所以无论你点击哪一行,它总是会打印0。
你可以打印位置或 currentPosition 或更新每一行的索引。