过滤ListView后,我无法获得旧位置

时间:2017-12-07 02:39:58

标签: java android

过滤后我无法获取ListView的旧位置,例如我应该在过滤后得到位置3,5,8,但我得到默认的0,1,2位置..你呢?伙计们有什么想法?这是我的代码..

public class Application_applicant_list extends AppCompatActivity {
 private ListView applicant;
 static String applId;
 private ArrayAdapter adapter;
 SearchView sv;
 Bundle b;
 Intent intent;


 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_application_list);
  sv = (SearchView) findViewById(R.id.appSv);
  applicant = (ListView) findViewById(R.id.listView_allForms_from_list);
  adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1);
  applicant.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView < ? > adapterView, View view, int position, long id) {
    intent = new Intent(Application_applicant_list.this, Application_form_from_list.class);

    b = new Bundle();
    b.putInt("position", position);
    intent.putExtras(b);
    Toast.makeText(getApplicationContext(), "Position:" + position,
     Toast.LENGTH_SHORT).show();
    startActivity(intent);


   }
  });


  sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
   @Override
   public boolean onQueryTextSubmit(String query) {
    return false;
   }

   @Override
   public boolean onQueryTextChange(String newText) {
    adapter.getFilter().filter(newText);

    return false;
   }
  });



 }

这是我从数据库中检索的部分,它们属于同一个类

 @Override
 protected void onResume() {
  DBHelper db = new DBHelper(this);
  ArrayList < String > ArrApp = new ArrayList < String > ();

  for (int i = 0; i < db.getAll_Appl().size(); i++) {
   ArrApp.add(String.valueOf(db.getAll_Appl().get(i).getApplId())); // + '\t' + '\t' + '\t' + db.getAll_IOP().get(i).getId());
  }

  adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, ArrApp) {
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
    TextView item = (TextView) super.getView(position, convertView, parent);
    item.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
    item.setGravity(Gravity.CENTER);
    item.setPadding(0, 10, 0, 10);
    return item;
   }

  };
  applicant.setAdapter(adapter);
  super.onResume();
 }
}

这是我的nextActivity.java,我在那里检索位置

Bundle b2 = getIntent()。getExtras(); if(b2!= null){  int position = b2.getInt(“position”); //位置是从感应和操作页面菜单项单击  form_from_list = db.getAll_Appl()。get(position);  formAppId.setText(form_from_list.getApplId());

1 个答案:

答案 0 :(得分:0)

因为,onItemClick中返回的位置是ListView item的位置,而不是list datasource (in your case it is ArrApp)中的位置。

因此,我认为您应该获得ListView item,然后将其传递给另一个Activity而不是该位置。

applicant.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView < ? > adapterView, View view, int position, long id) {

        String clickedItemValue = (String) adapterView.getAdapter().getItem(position); // cast to String because your list datasource is a list String 
        // pass clickedItemValue to another activity
    }
});