如何将值传递给另一个活动,其id为从对话框中单击的选项

时间:2017-04-03 06:49:04

标签: java android json android-intent alertdialog

我有一个ListView,当我点击它将调用的类别时会显示几个String,并弹出一个对话框选项菜单,其中包含英语,印地语和取消选项。当用户单击Listview中的一个类别时,将触发onClicklistener,例如" Novel"," Book"或"播放"。 我想在这里做的是,当用户点击" Book"并选择语言英语选项,我想将category_id和language_id传递给下一个activity.category_id和language_id是我从服务器响应中获得的json对象。所以我需要从listview中选择category_id并将其传递给下一个活动以及在对话框中选择的language_id。

如果有可能那么如何?在此先感谢您的帮助。

holder.imageView.setImageUrl(Config.TAG_IMAGE_URL+category.getImage(), imageLoader);
    holder.textViewName.setText(category.getName());
    category_id = category.getId();
    language_id = category.getLanguage_id();
    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            handleLanguageDialog();

            }

    });


}

private AlertDialog handleLanguageDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Select Language")
            .setItems(R.array.lang, new DialogInterface.OnClickListener()                 {
                public void onClick(DialogInterface dialog, int which) {
                    // The 'which' argument contains the index position
                    // of the selected item
                    switch(which)
                    {
                        case 0:// English


                            break;
                        case 1://Bengali

                            break;
                    }
                }
            });
    return builder.create().show();
}

categ.java

    private String id;
private String name;
private String description;
private String image;
private String parent_id;
private String language_id;
private String created;
private String modified;

@Override
public String toString() {
    return "Categ{" +
            "id='" + id + '\'' +
            ", name='" + name + '\'' +
            ", description='" + description + '\'' +
            ", image='" + image + '\'' +
            ", parent_id='" + parent_id + '\'' +
            ", language_id='" + language_id + '\'' +
            ", created='" + created + '\'' +
            ", modified='" + modified + '\'' +
            '}';
}

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public String getImage() {
    return image;
}
public void setImage(String image) {
    this.image = image;
}
public String getParent_id() {
    return parent_id;
}
public void setParent_id(String parent_id) {
    this.parent_id = parent_id;
}
public String getLanguage_id() {
    return language_id;
}
public void setLanguage_id(String language_id) {
    this.language_id = language_id;
}
public String getCreated() {
    return created;
}
public void setCreated(String created) {
    this.created = created;
}
public String getModified() {
    return modified;
}
public void setModified(String modified) {
    this.modified = modified;
}

}

3 个答案:

答案 0 :(得分:0)

在你的开关盒里面这样传递

case 0:
Intent intent = new Intent(YourCurrentActivity.this, Destination.class);
intent.putExtra("category_key", category_id);
intent.putExtra("language_key", language_id);
startActivity(intent);

答案 1 :(得分:0)

 Intent intent = new Intent(SourceActivity.this,destinationAvtivity.java);
    intent.putExtra("category_id",109);
    startActivity(theintent);

答案 2 :(得分:0)

在您的第一个活动中传递此类数据

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("CATEGORY_ID", category_id);
intent.putExtra("LANGUAGE_ID", language_id);
startActivity(intent);

现在,您的下一个活动会收到这样的数据

String cat_id = getIntent().getExtras().getString("CATEGORY_ID");
String cat_id = getIntent().getExtras().getString("LANGUAGE_ID");