我想创建一个ListView
点击一行,它会带你到一个页面的名称和其他细节。我花了很多时间试图使它工作,但目前我所拥有的只是点击该行将您带到一个空白页面。我搜索了很多问题,但没有一个人在新页面上添加标题!这里是我的列表视图活动: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);
}
}
和屏幕2是:
<?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(位置)),现在说空的每一行。例如。在ThirdScreenActivity上:
TextView t = ((TextView)findViewById(R.id.textviewPosition));
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(String.valueOf(position));
是奥雷克是正确的,只需调用Intent i = new Intent(SecondScreenActivity.this,ThirdScreenActivity.class); i.putExtra(“position”,position); startActivity(i);一旦。也看到你的screen2.xml是不是空白,或者是你想加载的这个xml fil –
什么颜色的空白页? –
白色(我很确定是我设置的screen2背景) – stackUnderflow