大家好。
不幸的是,我对Java没有太多经验,我遇到了一个可能非常基本的问题。我正在填写列表视图,当我单击一个列表项之一时,我想将字符串发送到另一个意图。但是字符串的值始终与它正确显示在列表视图中。我想这与每次出现新项目时都会被覆盖的字符串有关。
我希望零件[3]被发送到OnItemClick中的另一个功能,我该如何完成?
这是代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
ViewHolder holder;
final String uuidbeacon;
final String pid;
uuidbeacon = arrayL.get(position).getProximityUuid().toString();
minorbeacon = arrayL.get(position).getMinor();
rangebeacon = arrayL.get(position).getAccuracy();
parts = fetchTums(uuidbeacon).split("::");
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
}
else{
if(parts[0].equals("1")) {
holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoring, null));
holder.beacon_image.setImageDrawable(getTumImage("http://127.0.0.1/uploads/face/" + parts[4]));
holder.beacon_uuid.setText(parts[1]);
holder.beacon_txpower.setText(parts[3]);
if(parts[2].equals("1"))
{
holder.beacon_row.setBackgroundColor(Color.parseColor("#B6B6B6"));
holder.image_lock.setVisibility(View.VISIBLE);
}
}
if(parts[0].equals("0"))
{
holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoringe, null));
}
}
pid = parts[3];
if (arrayL.get(position).getProximityUuid() != null)
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id )
{
//list.setItemChecked(position, true);
//view.setBackgroundColor(Color.BLUE);
toastThis(pid);
if(parts[2].equals("1")){
pin(pid);
}
else{
if(pid != null){
openPage(parts[3], "", parts[1]);
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
您必须使用从onItemClick
接收到的position
选择该值才能单击确切的项目。在您的情况下,您必须在arrayL
中以索引(位置)获得值。
String item = arrayL[position];
我会尝试在适配器外实现点击侦听器。例如,在片段内。
类似的东西:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
YourAdapter adapter = new YourAdapter(getActivity(), mItems);
ListView list = (ListView) view.findViewById(R.id.your_list_id);
list.setAdapter(adapter);
list.setOnItemClickListener(onItemClicked());
}
private AdapterView.OnItemClickListener onItemClicked() {
return new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
YourItem item = (YourItem) parent.getItemAtPosition(i);
// your magic happens here
}
};
}