- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,所以我按照教程构建了一个 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/
我刚刚开始使用 android,我遵循了关于在此 website 上自定义 ListView 布局的布局教程。 。 由于所有列表都是使用数组填充的,并且都共享相同的 id -->(R.id.ListV
我有一个 ListView,其中包含使用 JSON 检索的项目列表,并且我想在单击项目时显示 ProgressDialog。 listNews.setOnItemClickListener(new O
我错过了一些非常重要的东西,但我不太明白是什么。有人可以帮忙吗?这可能是我错过的一些非常愚蠢的事情,但我无法启动我的 onItemClick。 创建时.... @Override public
public void populate(ArrayList array, ArrayList array2) { ListView showList = (ListView)
我正在使用 ArrayAdapter 来填充 ListView。选择和项目后,它会显示一个确认 Y/N 对话框。如果用户的选择是否定的,那么他应该能够选择另一个显示相同对话框的项目。等等。 这是我的代
我的 OnClickListener 似乎从 ListView 中选择了错误的项目。 Listener 总是选择列表中的第一项。这是我的 Activity,其中 OnClickListener pub
因此,我将 jfeinstein10 库用于我的 SlidingMenu。它工作正常,但当用户点击其中一个菜单选项时,我无法切换菜单。 我有滑动菜单所在的 MainActivty 和一个名为 Samp
所以我现在有点困惑。我使用的是带有扩展 BaseAdapter 的内部类的 simpleCursorAdapter,这样我就可以在 ListView 中每 x 个项目显示一个广告。现在,当用户单击 L
我正在使用 arrayadapter 在我的 ListView 中显示图片和标题以及描述 lv1.setAdapter(new ArrayAdapter(this,android.R.layout.s
我有一个 ListView,它包含来自已解析 JSON 的数据。所以这是我的示例代码: ArrayList> myList = new ArrayList>(); /** * par
使用 Firebase。我正在尝试填充 ImageView 、使用 Firebase 的 Textview 和自定义 ListView 。当用户从列表中选择一个项目时,它应该将 Firebase 项目
我正在使用 import android.support.v4.app.ListFragment 创建一个 ListFragment。在 fragment 本身(而不是父 Activity )中,我想
这可能真的很长,我很抱歉读完这一切的可怜人。 目标:我想创建一个 Pokemon Pokedex 应用程序(是的,我知道很幼稚)。我认为这对于构建第一个应用程序非常有用,因为有大量数据需要处理。对于那
每次我运行这个,我都会得到错误。我不知道怎么了?我只想通过索引从 array 中获取字符串并将其放在 URL 上并在另一个 Activity 中运行它。 包信息.androidhive.customl
我在获取数据库条目的值并将其传递给 Intent 时遇到问题。在下面的代码中,onItemClick 正在传递整个数据库的数据,而不是单击的项目。我怎样才能指定只传递点击的项目的数据? Firebas
情况是当我选择其中一个列表项时。它将在下一个 Activity 中显示该项目的详细信息。 我尝试使用 onItemClick 监听器从选定的 ListItem 获取值。我在点击该项目时遇到错误 [FA
我正在开发一个 Android 项目,其中我必须在列表中显示一些图像。为此,我使用适配器。运行代码时我没有看到任何错误,但没有显示图像。另外,我在其中设置了一个 onClickListener 和一个
[背景信息:我是一名新手,正在学习在 Android Studio 中制作“待办事项列表”应用程序。基本上,当用户单击“待办事项”列表中的任何项目时,我会弹出一个对话框,用户可以选择两个按钮来“删除或
我有一个 ListView ,当您单击列表中的某个项目时,我会用一些其他数据重新填充列表。我遇到的问题是在 onItemClick 中传递一个字符串,以便我可以在那里使用它。我怎样才能做到这一点?
我正在尝试从 OnItemClick 中扩充菜单。我查看了 Android 开发者网站,并能够使用下面的第一个代码来充气 toast 。然而,当我尝试用菜单充气机替换它时,它不起作用。你能帮我解决这个
我是一名优秀的程序员,十分优秀!