我正在尝试创建一个ListView
单击行的位置,它会将您带到包含名称和其他详细信息的页面。我花了很多天试图让它工作,但目前我所拥有的只是单击该行会将您带到一个空白页面。我搜索了很多问题,但没有一个为新页面添加标题!这是我的列表视图活动:
package com.example.cookbook;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View;
public class SecondScreenActivity extends Activity
{
ArrayList<String> RecipeList;
public void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
setContentView(R.layout.main);
// Get the reference of ListViewRecipes
ListView RecipeListView=(ListView)findViewById(R.id.mainListView);
RecipeList = new ArrayList<String>();
getRecipeNames();
// Create The Adapter with passing ArrayList as 3rd parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, RecipeList);
// Set The Adapter
RecipeListView.setAdapter(arrayAdapter);
// register onClickListener to handle click events on each item
RecipeListView.setOnItemClickListener(new OnItemClickListener()
{
// argument position gives the index of item which is clicked
public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
{
Intent i=new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
i.putExtra("position", position);
startActivity(i);
}
});
}
void getRecipeNames()
{
RecipeList.add("Recipe1");
RecipeList.add("Recipe2");
RecipeList.add("Recipe3");
RecipeList.add("Recipe4");
RecipeList.add("Recipe5");
RecipeList.add("Recipe6");
RecipeList.add("Recipe7");
RecipeList.add("Recipe8");
RecipeList.add("Recipe9");
RecipeList.add("Recipe10");
}
}
这是我的新页面活动:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ThirdScreenActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
TextView t = ((TextView)findViewById(R.id.textviewPosition));
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(position);
}
}
和 screen2 是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextView
android:id="@+id/textviewPosition"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
我在 eclipse 或 logcat 中没有收到任何错误消息,当我单击时它只是显示一个空白页面!谢谢您的帮助
编辑:尝试 (String.valueOf(position)) 而不是 (position) 现在它对每一行都说 null 。例如。在 ThirdScreenActivity 上:
TextView t = ((TextView)findViewById(R.id.textviewPosition));
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(String.valueOf(position));