0

我正在尝试创建一个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));
4

3 回答 3

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);
}
});
于 2013-06-23T16:19:08.347 回答
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);
于 2013-06-23T17:00:57.900 回答
0

首先,您为什么要创建相同的意图并启动两次?
你只需要这个:

Intent i = new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
i.putExtra("position", position);
startActivity(i);

编辑:

其次,您确定您的 screen2 包含可见元素吗?您应该发布您的 XML

于 2013-06-23T12:56:58.397 回答