このようなあなたの両方の活動を変更します。あなたの必要に応じて動作します。
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);
}
}
あなたが投稿したコードは不完全です。 –
@AndyDeveloper何が間違っているか教えてください...私はそれを修正します。 –