gpt4 book ai didi

android - 如何在 ListView 中获取选定的项目?

转载 作者:行者123 更新时间:2023-11-30 00:29:43 26 4
gpt4 key购买 nike

这是我的代码,出于某种原因,当我运行它(编辑、删除)时,我无法在 android studio 中收到警报提示。 T`//Activity 是第一个页面,负责通过 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 中提取数据,关于我应该如何解决这个问题的任何线索?

最佳答案

因为你应该添加 .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();

关于android - 如何在 ListView 中获取选定的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44662760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com