public void onListItemClick(ListView listview,View itemView,int position,long id) {
ListView lv=(ListView)findViewById(R.id.drinks_lv);
Intent intent = new Intent(DrinkCategoryActivity.this, DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_DRINKNO,(int)id);
startActivity(intent);
}
DetailActivity.java
public class DetailActivity extends AppCompatActivity {
public static final String EXTRA_DRINKNO = "drinkNo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
int drinkNo = (int) getIntent().getExtras().get(EXTRA_DRINKNO);
Drink drink = Drink.drinks[drinkNo];
ImageView photo =(ImageView)findViewById(R.id.photo);
photo.setImageResource(drink.getImageResourceId());
photo.setContentDescription(drink.getName());
TextView name=(TextView)findViewById(R.id.name);
name.setText(drink.getName());
TextView description=(TextView)findViewById(R.id.description);
description.setText(drink.getDescription());
}
}
代码的第一部分是来自Head First Android Development-2015的DrinkCategoryActivity类。它应该导航到DetailActivity类但没有任何反应。列表视图在点击drinks的列表选项时没有做任何事情。它没有显示任何错误但是第三个活动(没有启动DetailActivity,它应该显示图像,名称和描述。
答案 0 :(得分:0)
这是如何在listview中实现点击项目
ListView listView=(ListView)findViewById(R.id.drinks_lv);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(DrinkCategoryActivity.this, DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_DRINKNO,id);
startActivity(intent);}
});
并在DetailActivity中:
//if the type id is "int" then:
int drinkNo = this.getIntent().getExtras().getInt(EXTRA_DRINKNO);
//if it's long then use getLong
答案 1 :(得分:0)
// Declare `-` operator overload function
func -(lhs: CGPoint, rhs: CGPoint) -> CGPoint {
return CGPoint(x: lhs.x - rhs.x, y: lhs.y - rhs.y)
}
// TEST
let point1 = CGPoint(x: 10, y: 10)
let point2 = CGPoint(x: 5, y: 5)
print(point1 - point2) //prints (5.0, 5.0)
里加布局
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener
{
final String numbers = {"1","2","3"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.riga, numbers);
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adattatore, final View componente, int position, long id )
{
Toast.makeText(getApplicationContext(), numbers[position], Toast.LENGTH_LONG).show();
}
}
Activity_main布局
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:id="@+id/textView"
android:textSize="36sp" />
这是我的旧应用程序来接管使用实现AdapterView.OnItemClickListener
实现的数字