OnItemclick 丢失字符串值
OnItemclick loosing value of string
大家好。
不幸的是,我在 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;
}
我会尝试在您的适配器之外实现点击侦听器。例如,在 Fragment 中。
像这样:
@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
}
};
}
您必须使用从 onItemClick
收到的 position
选择值才能获得准确的项目点击。在您的情况下,您必须像下面这样在 arrayL
中获取索引(位置)处的值。
String item = arrayL[position];
大家好。
不幸的是,我在 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;
}
我会尝试在您的适配器之外实现点击侦听器。例如,在 Fragment 中。
像这样:
@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
}
};
}
您必须使用从 onItemClick
收到的 position
选择值才能获得准确的项目点击。在您的情况下,您必须像下面这样在 arrayL
中获取索引(位置)处的值。
String item = arrayL[position];