我正在尝试创建一个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);
}
}
和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))而不是(位置),现在每行都显示null。例如。 on ThirdScreenActivity:
TextView t = ((TextView)findViewById(R.id.textviewPosition));
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(String.valueOf(position));
答案 0 :(得分:0)
首先,为什么要创建相同的意图并启动它两次? 你只需要这个:
Intent i = new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
i.putExtra("position", position);
startActivity(i);
编辑:
其次,你确定你的screen2包含可见元素吗?你应该发布你的XML
答案 1 :(得分:0)
你没有得到任何东西的原因是因为你传递了一个int。 position是一个int而不是一个额外的字符串,所以getStringExtra不会工作,因为extra是一个int。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked,
int position, long id) {
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
Intent i = new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
i.putExtra("position", strText);
startActivity(i);
}
});
答案 2 :(得分:0)
问题是你传递了一个额外的int:
i.putExtra("position", position);
并收到一个字符串:
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(position);
...所以你必须为以下几行更改这两行......
Intent intent = getIntent();
int position = intent.getExtras().getInt("position");
t.setText(String.valueOf(position));
编辑:如果你想传递一个字符串,那就这样做......
i.putExtra("position", String.valueOf(position));
...所以你必须在其他活动上收到字符串...
Intent intent = getIntent();
String position = intent.getExtras().getString("position");
t.setText(position);