如何在ListView中获取所选项目?

时间:2017-06-20 20:49:44

标签: android listview

这是我的代码,出于某种原因,当我运行它时,我无法在android studio中收到提示(编辑,删除)。 T` //活动是第一页,负责通过firebase登录

public class mainActivity extends AppCompatActivity {
    public String username;
    public String userUid;
    FirebaseListAdapter<blogContent> adapter; //Declare connection to firebase
    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) { //Bsically the main page

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         username = getIntent().getStringExtra("username");
        userUid = getIntent().getStringExtra("userUid");
        setTitle("Welcome, " + username);
        final String user = username;
        final ListView listView = (ListView) findViewById(R.id.activityList);
        adapter = new FirebaseListAdapter<blogContent>(this, blogContent.class, R.layout.activity_blog_content, //connects to firebase
                FirebaseDatabase.getInstance().getReference(userUid)) { //Pulls user ID from firebase
            @Override
            protected void populateView(View v, blogContent model, int position) { //checks if user exists in database
                    String checking = model.getUsercheck();
                if(user.equals(checking)) { //exists

                        TextView title = (TextView) v.findViewById(R.id.title);
                        ImageView img = (ImageView) v.findViewById(R.id.img);
                        TextView des = (TextView) v.findViewById(R.id.description);

                        byte[] decodedString = Base64.decode(model.getImg(), Base64.DEFAULT);
                        Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

                        title.setText(model.getTitle());
                        des.setText(model.getDescription());
                        img.setImageBitmap(bitmap);
                        Log.d("TAG", model.getImg());

                }else{ //if doesn't exist
                    Log.d("TAG","no such user found");
                }
            }

        };
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                blogContent itemRef = adapter.getItem(position);

                CharSequence picker[] = new CharSequence[] {"Edit", "Delete"};
                AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity.this);
                builder.setTitle("Choose an Action");
                builder.setItems(picker, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch(which){
                            case 0:{
                                Toast.makeText(mainActivity.this, "0", Toast.LENGTH_SHORT).show();
                                break;
                            }
                            case 1:{
                                Toast.makeText(mainActivity.this, "1", Toast.LENGTH_SHORT).show();
                                break;
                            }
                            default:{
                                break;
                            }
                        }
                    }
                });
            }
        });
    }

    public void addActivityIntent(View view){ //get user and password and sends screen to addactivity
        Intent intent = new Intent(mainActivity.this, addActivity.class);  //declares destination
        intent.putExtra("username", username);
        intent.putExtra("userUid", userUid);
        startActivity(intent);  //sends user
    }

}

此外,似乎无法构建数组,因为我从Firebase中提取数据,我应该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

因为你应该添加.show()你可以编写像

这样的警告对话框
  new AlertDialog.Builder(mainActivity.this);
            builder.setTitle("Choose an Action");
            builder.setItems(picker, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch(which){
                        case 0:{
                            Toast.makeText(mainActivity.this, "0", Toast.LENGTH_SHORT).show();
                            break;
                        }
                        case 1:{
                            Toast.makeText(mainActivity.this, "1", Toast.LENGTH_SHORT).show();
                            break;
                        }
                        default:{
                            break;
                        }
                    }
                }
            }).show();