如何根据用户的选择选择(多个按钮,内部活动a)在新活动(活动a)中添加项目数到列表视图?

时间:2017-04-25 12:41:44

标签: java android listview

我已经使用文本视图定义了一些按钮(在活动a中),并且无论何时点击任何按钮,它都会打开我放置了列表视图的活动(活动b)。我想要的是根据我在活动a中按下的按钮在列表视图中加载一些列表。比如按钮我希望列表视图加载4项如item1,item2,item3,item4和按钮b我希望列表视图加载5项如itemV,itemW,itemX,item4,itemZ。

这是我的活动

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView a, b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        a = (TextView)findViewById(R.id.a);
        b = (TextView)findViewById(R.id.b);

        a.setOnClickListener(this);
        b.setOnClickListener(this);
    }

@Override
    public void onClick(View v) {
        switch (v.getId()){
           case R.id.a:
                Intent sub1 = new Intent(this, SubCategory.class);
                this.startActivity(sub1);
                break;
            case R.id.b:
                Intent sub2 = new Intent(this, SubCategory.class);
                this.startActivity(sub2);
                break;
        }
    }
}

这是我的活动b

public class SubCategory extends AppCompatActivity {

private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sub_category);

    listView = (ListView)findViewById(R.id.listview);
}

}

2 个答案:

答案 0 :(得分:0)

你需要加入额外的意图。

 String empType = getIntent().getStringExtra("emp-type","");

在新活动中

Bind<IUserService>().To<AspNetUserService>().InScope(RequestScope);

答案 1 :(得分:0)

像这样修改你的两个活动。它将根据您的需要工作。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView a, b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        a = (TextView)findViewById(R.id.a);
        b = (TextView)findViewById(R.id.b);

        a.setOnClickListener(this);
        b.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.a:
                Intent sub1 = new Intent(this, SubCategory.class);
                sub1.putExtra("BUTTON_CLICKED", "A");
                this.startActivity(sub1);
                break;
            case R.id.b:
                Intent sub2 = new Intent(this, SubCategory.class);
                sub2.putExtra("BUTTON_CLICKED", "B");
                this.startActivity(sub2);
                break;
        }
    }
}

并且:

public class SubCategory extends AppCompatActivity {

    private ListView listView;
    String buttonType;
    ArrayAdapter adapter;

    static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana"};
    static final String[] NAMES = new String[] { "Sachin", "Brett", "Shane", "Zaheer"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_category);



        Bundle bundle = getIntent().getExtras();
        if(bundle != null) {
            buttonType = bundle.getString("BUTTON_CLICKED");
        }
        if(buttonType != null && buttonType.equals("A")) {
            adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, FRUITS);
        } else if(buttonType != null && buttonType.equals("B")) {
            adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, NAMES);
        }
        listView = (ListView)findViewById(R.id.listview);
        listView.setAdapter(adapter);
    }
}