Boostr



0 votes
11 views
I am not able to get the old position of my ListView after filtering, for example I should be getting position 3,5,8 after filtering, but I'm getting the default 0,1,2 position.. Do you guys have any idea why? Here's my code..

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;
   }
  });

 }
This is the part where I retrieve from the database, they're in the same class

 @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();
 }
}
This is my nextActivity.java where I retrieve the position

Bundle b2 = getIntent().getExtras(); if (b2 != null) { int position = b2.getInt("position"); // position is from induction and operation page menu item click form_from_list = db.getAll_Appl().get(position); formAppId.setText(form_from_list.getApplId());
asked Dec 7 in HTML by Amrendra | 11 views



1 Answer

0 votes
Because, the position that return in onItemClick is the position of ListView item not the position in list datasource (in your case it is ArrApp).

Therefore, I think you should get the value of ListView item then pass it to another Activity instead of the position.

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
    }
});
answered Dec 7 by Amrendra

Related Questions



Welcome to Knowledge Boostr, where you can ask questions and receive answers from other members of the community.
This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers


23,471 questions
40,135 answers
12 comments
23,609 users