public class SearchRecipe extends AppCompatActivity {
ArrayList<Recipe> lst=new ArrayList<Recipe>();
private ListView listView;
ProgressDialog pg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_recipe);
pg = ProgressDialog.show(this, "Loading", "please wait", true);
getDataBase3();
}
public void getDataBase3() {
myFirebaseRef.child("Recipes").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Recipe recipe = snapshot.getValue(Recipe.class);
lst.add(recipe);
}
init();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(SearchRecipe.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
}
private void init() {
listView= (ListView) findViewById(R.id.listview_searchRecipe);
UsersAdapter adapter = new UsersAdapter(this, lst);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(SearchRecipe.this,ShowRecipe.class);
intent.putExtra("recipeName",lst.get(position).getRecipeName());
startActivity(intent);
finish();
}
});
pg.dismiss();
}
public class UsersAdapter extends ArrayAdapter<Recipe> {
public UsersAdapter(android.content.Context context, ArrayList<Recipe> names) {
super(context, 0, names);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Recipe user = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.recipe_row_layout, parent, false);
TextView tvname= (TextView) convertView.findViewById(R.id.recipeTV2_row);
TextView tvlike= (TextView) convertView.findViewById(R.id.likes_row);
TextView tvdislike= (TextView) convertView.findViewById(R.id.dislike_row);
tvname.setText(user.getRecipeName());
tvlike.setText(""+user.getLikes());
tvdislike.setText(""+user.getDislikes());
}
return convertView;
}
}
填充lst作品 - 有价值和正确的。 当列表到达位置8时,它开始替换ListView中的名称(如果列表中的数字8是“dog”,数字1是“cat”,它将显示数字8,数字1是“cat”,但是当你点击1时它会把它带到1,当你点击8时它会把你带到正确的8!)
所以问题在于ListView的呈现 - 它显示错误的值,当你点击它们时它会带你到正确的位置。
谢谢你们。
答案 0 :(得分:0)
不推荐使用ListView,只需使用RecyclerView,它更轻松,更容易使用(如果你仍然遇到问题 - 然后发布)
Google的RecyclerView示例:https://developer.android.com/training/material/lists-cards.html
可搜索和可扩展的recycerview示例(在对话框容器中,这个想法是相同的):https://github.com/WithoutCaps/DialogsCheatSheet