可敬的 Gurus Bellow 代码通过单击列表视图过滤的项目返回错误的位置和数据ArrayAdapter
,任何建议都将受到高度赞赏。
Listview
可以通过 setOnItemClickListener 上的波纹管代码轻松完美地过滤它返回错误的位置。任何建议都将受到高度赞赏。
public class MyActivity extends AppCompatActivity {
---------Variables------
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
Intent i = getIntent();
name2=i.getStringExtra("chiled");
postion = i.getIntExtra("id",0);
chiled= i.getStringExtra("name").replace(" ", "_");
//-------------------------Action Bar-------
ActionBar mActionBar = getSupportActionBar();
if (mActionBar != null) {
----Action BarCode---------
}
adapter = new ArrayAdapter<String>(MyActivity.this,
R.layout.listview_item,R.id.text);
//################# FireBaseStarts
mProgressDialog = new ProgressDialog(MyActivity.this);
mProgressDialog.setTitle("Loading List...");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
FirebaseDatabase database = FirebaseDatabase.getInstance();
myRef = database.getReference(chiled);
listview = (ListView) findViewById(R.id.listview);
queryRef = myRef.orderByChild("order");
queryRef.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
long b = dataSnapshot.getChildrenCount();
imageUrls = new String[(int) b];
int i = 0;
for (DataSnapshot ddataSnapshot : dataSnapshot.getChildren()) {
Object value = ddataSnapshot.getValue();
adapter.add(((Map) value).get("name").toString());
imageUrls[i] = ((Map) value).get("url").toString();
i++;
}
listview.setAdapter(adapter);
mProgressDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("Database Error is: ", "loadPost:onCancelled", databaseError.toException());
Toast.makeText(MyActivity.this, "Failed to load post.",
Toast.LENGTH_SHORT).show();
}
});
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(MyActivity.this,
SecondActivity.class);
i.putExtra("name", adapter.getItem(position));
i.putExtra("id", position);
startActivity(i);
}
});
}
//---------------Search Starts
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
final MenuItem item=menu.findItem(R.id.inputSearch);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setQueryHint("Search Naghma...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
// listview.setAdapter(adapter);
return true;
}
}