java - 如何将点击哪一行的变量传递给我的 Intent ?

标签 java android android-intent

<分区>

我正在使用 Java 和 Android Studio 创建我的第一个 Android 应用。我有一个主类和布局(在适配器的帮助下)显示章节编号的 ListView 。我有一个 clicklistener,当单击一行时,将加载一个新 Activity 。但是,我想做的是捕获一个变量,该变量记录单击了哪一行,以便我可以在新 Activity 中加载相关内容。做这个的最好方式是什么?

主要 Activity :

public class MainActivity extends AppCompatActivity {
    String[] nameArray = {"Chapter 1","Chapter 2","Chapter 3","Chapter 4","Chapter 5","Chapter 6", "Chapter 7", "Chapter 8", "Chapter 9", "Chapter 10", "Chapter 11", "Chapter 12", "Chapter 13", "Chapter 14", "Chapter 15", "Chapter 16", "Chapter 17", "Chapter 18", "Chapter 19", "Chapter 20", "Chapter 21" }; //List of chapters to display
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ChapterlistAdapter whatever = new ChapterlistAdapter(this, nameArray);
        listView = (ListView) findViewById(R.id.chapterListview);
        listView.setAdapter(whatever);

        // Make the row respond when clicked
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
                Log.d("dev","Its clicked" );
                goToChapter();
            }

        });
    }

    /** Called when the user taps the Send button */
    public void goToChapter() {
        Intent intent = new Intent(this, CardView.class);
        intent.putExtra(EXTRA_MESSAGE, "chapter clicked");
        startActivity(intent);
    }

章节列表适配器:

public class ChapterlistAdapter extends ArrayAdapter {
    //to reference the Activity
    private final Activity context; //Stores which activity the list listview is on?
    //to store the list of countries
    private final String[] chapterArray;

    public ChapterlistAdapter(Activity context, String[] nameArrayParam){

        super(context,R.layout.chapter_listview_row , nameArrayParam);
        this.context=context;
        this.chapterArray = nameArrayParam;
    }

    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.chapter_listview_row, null,true);

        //this code gets references to objects in the listview_row.xml file
        TextView nameTextField = (TextView) rowView.findViewById(R.id.chapterListviewRowText);

        //this code sets the values of the objects to values from the arrays
        nameTextField.setText(chapterArray[position]);


        return rowView;

    };
}

我的想法是在 putExtra 方法中传递一个变量——但是我不确定如何捕获该变量。我可以从 MainActivity 获取它吗?还是我必须在适配器类中执行此操作?

最终目标是了解点击了哪个章节,以便显示该章节的内容。

最佳答案

通过位置获取被点击的item,如:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //get here using position  
        String selected = (String) parent.getItemAtPosition(position);                     
    }
});

关于java - 如何将点击哪一行的变量传递给我的 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48233695/

相关文章:

java - Google App Engine - 低级数据存储 API 标志?

java - Android 堆增长 - 我应该担心吗?

java - 使用 Extras 将信息从 Activity 传递到 Intent 并返回

android - 使用 Android 联系人应用程序编辑由自定义 ContentProvider 添加的原始联系人

java - 创建 Magento 站点的 native Android 应用程序

java - 在独立应用程序的 hibernate 状态下配置 sessionFactory

java - Spring Boot OAuth2 `AbstractTokenGranter.validateGrantType()` 方法中发生了什么?

java - 使用Java,尝试显示带有图像的可滚动jbutton列表,但显示文本而不是按钮

Android DownloadManager 如何在完成下载前将文件标记为临时文件

android - MediaStore.Video.Thumbnails.getThumbnail() 返回 null