gpt4 book ai didi

java - 使用 Android AdapterView.OnItemClick 监听器重定向到新的空白 Activity

转载 作者:行者123 更新时间:2023-11-30 02:23:20 25 4
gpt4 key购买 nike

嘿,所以我按照教程构建了一个 ListView 。一切都已填充,想要单击其中一个项目并将其重定向到另一个空白 Activity 。 ListView 中的所有项目都是在我的仪表板类中创建和填充的:

    import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class Dashboard extends ActionBarActivity {

private List<DashboardItems> myDash = new ArrayList<DashboardItems>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);

populateDashboardItemList();
populateDashboardListView();
dashboardOnclick();

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_dashboard, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.homescreen:
homescreenItem();
return true;
case R.id.dashboard:
dashboardItem();
return true;
case R.id.about:
aboutItem();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

private void homescreenItem(){
startActivity(new Intent(Dashboard.this, Home.class));
}

private void dashboardItem(){
startActivity(new Intent(Dashboard.this, Dashboard.class));
}

private void aboutItem(){
new AlertDialog.Builder(this)
.setTitle("About")
.setMessage("Welcome to Save Me! An interactive and intuitive way to protect yourself during emergency situations and keep your location privacy. Made for a Dissertation and Developed by Ankhit Sharma")
.setNeutralButton("OK" , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
}).show();
}

private void populateDashboardItemList() {
myDash.add(new DashboardItems("Profile" , "Use this to create/edit your profile" , R.drawable.profile));
myDash.add(new DashboardItems("Location" , "Use this to set/view your true/dummy location" , R.drawable.location));
myDash.add(new DashboardItems("Emergency Contacts" , "Use this to select/view your emergency contacts" , R.drawable.contacts));
}

private void populateDashboardListView() {
ArrayAdapter<DashboardItems> adapter = new myDashboardAdapter();
ListView list = (ListView) findViewById(R.id.dashboardListview);
list.setAdapter(adapter);
}

private void dashboardOnclick() {
ListView list = (ListView) findViewById(R.id.dashboardListview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {

DashboardItems clickedDash = myDash.get(position);
String message = "You clicked position" + position
+ "which is" + clickedDash.getItemName();
Toast.makeText(Dashboard.this, message, Toast.LENGTH_LONG).show();
}
});
}

private class myDashboardAdapter extends ArrayAdapter<DashboardItems>{

public myDashboardAdapter(){
super(Dashboard.this, R.layout.dashboarditemslayout, myDash);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
//This makes sure we have a view to work with
View itemView = convertView;
if (itemView == null){
itemView = getLayoutInflater().inflate(R.layout.dashboarditemslayout, parent, false);
}

//Find DashboardItem to work with

DashboardItems currentDashboardItems = myDash.get(position);

//Image Icon
ImageView imageView = (ImageView)itemView.findViewById(R.id.dashboardicon);
imageView.setImageResource(currentDashboardItems.getItemID());

//Profile
TextView profView = (TextView)itemView.findViewById(R.id.dashtextprofile);
profView.setText(currentDashboardItems.getItemName());

//Description
TextView descView = (TextView)itemView.findViewById(R.id.dashtextprofiledescription);
descView.setText(currentDashboardItems.getItemDescription());


return itemView;
}


}
}

现在,在 OnItemClick 函数中,我尝试了不同的组合以使其引用另一个类(我的 UserProfile.java),但它不起作用。当前函数单击它并显示项目的位置和各自的名称以及一条消息。我想知道您是否可以告诉我启动适配器 View Intent 的正确方法,因为它对我来说都是全新的?

UserProfile.java 类是空白 Activity 。

附言。 OnItemClick 与 OnClick 除了 item 是针对项目而 onClick 是整个区域之外的主要区别是什么?

最佳答案

private void dashboardOnclick() {
ListView list = (ListView) findViewById(R.id.dashboardListview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {

DashboardItems clickedDash = myDash.get(position);
String message = "You clicked position" + position
+ "which is" + clickedDash.getItemName();
Toast.makeText(Dashboard.this, message, Toast.LENGTH_LONG).show();
Intent intent;
// i do not know how your dashitems are declared
// but the idea here is you have already gotten the clicked item so what you do is compare them
// with the available item you have in your dasboarditem like this
if(clickedDash ==DashboardItems.Profile){ // assuming DashboardItems.Profile is your profile for profile item
intent = new Intent(Dashboard.this,UserProfile.class); // you place your intent here in the onitemclick method, in the if equal profile condition
}else if(clickedDash==DashboardItems.Location){ // you can continue with the others, also here
// assuming location item is declared in DashboardItems as location
intent = new Intent(Dashboard.this, UserLocation.class);
}else{
// put the 3rd item
}
// so it is triggered when you click an item
startActivity(intent); // then you start it this way
// that's all you need to do
}
});
}

希望这就是你想要的

关于java - 使用 Android AdapterView.OnItemClick 监听器重定向到新的空白 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28160772/

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