过滤器列表后我无法获得android中物品的实际位置

时间:2016-08-03 12:44:55

标签: android listview

这是我的MainActivity.java,我在item上定义onclicklistener。总是我在过滤后得到错误的项目值,如果listview中有10个项目然后过滤后如果我得到3个项目并且正确的位置是3个,6,9但我得到0,1,2。

请帮帮我。

<?php
    include_once '../db/dbconnect.php';

    $query = "SELECT * FROM property WHERE city='Sheffield' AND type='House'";
    $result = $mysqli->query($sql);
    $num_rows = mysql_num_rows($result);
    if($num_rows > 0){
       for($i=0;$i<$num_rows;$i++){
            $row=mysql_fetch_row($result);
            $location[] = $row[3].','.$row[6].','.$row[7].','.($i+1);
      }
   }

?>

这里是Adapter class

package com.focusmedica.maadiabetes;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends Activity {

private MainAdapter adapter;
private static ListView lvFiltered;
private static ArrayList<DataModel> data=new ArrayList<>();;
MyDatabase handler;
private EditText etSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lvFiltered =(ListView)findViewById(R.id.lvFiltered);
    etSearch = (EditText)findViewById(R.id.etSearch);
    etSearch.setText("");
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    handler=new MyDatabase(this);

    data=handler.getChapterDetails();
    adapter = new MainAdapter(this,data);
    lvFiltered.setAdapter(adapter);
    lvFiltered.setTextFilterEnabled(true);

    lvFiltered.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(MainActivity.this, SubActivity.class);
            intent.putExtra("position", position);
            startActivity(intent);
        }
    });

    etSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            if(cs.length()>0) {
                etSearch.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.delete, 0);
            }else{
                etSearch.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            }
            // When user changed the Text
            adapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

    etSearch.setOnTouchListener(new View.OnTouchListener() {
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP) {
                if(etSearch.getCompoundDrawables()[2]!=null){
                    if(event.getX() >= (etSearch.getRight()- etSearch.getLeft() - etSearch.getCompoundDrawables()[2].getBounds().width())) {
                        etSearch.setText("");
                    }
                }
            }
            return false;
        }
    });
  }
}

3 个答案:

答案 0 :(得分:2)

获取已过滤列表中某个项目的“旧”位置(相当于项目ID):

@Override
public long getItemId(int position) {

    int itemID;

    // orig will be null only if we haven't filtered yet:
    if (orig == null)
    {
        itemID = position;
    }
    else
    {
        itemID = orig.indexOf(dataSet.get(position));
    }
    return itemID;
}

现在将此方法与OnItemClickListener

一起使用
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(MainActivity.this, SubActivity.class);
        intent.putExtra("position", adapter.getItemId(position) );
        startActivity(intent);
    }

注意:如果您需要使用notifyDatasetChanged(),也可以覆盖此方法:

@Override
public void notifyDatasetChanged()
{
    // either this or: orig = dataSet;
    orig = null;
    super.notifyDatasetChanged();
}

答案 1 :(得分:0)

过滤&#39; ArrayList&#39;将从第0个位置添加数据。所以你将获得0,1,2的位置。你必须使用id而不是position。

答案 2 :(得分:0)

将您想要获取的数据存储在数组中并使用listview的onClick方法中相同数组的位置选择用户选择的数据。