我正在使用Intent和Bundle将数据传输到listactivity,我的问题是我可以使用的设计模式,我不需要在添加新数据模型时编写新的ListActivity。任何意见和建议将不胜感激。
示例代码如下:
主要活动中的:
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] list = Data.getName();
Bundle bundle = new Bundle();
bundle.putStringArray("list", list);
Intent intent = new Intent(MainActivity.this, ListActivityA.class);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
ListActivityA中的:
public class ListActivityA extends ListActivity {
public static final String KEY_LIST = "list";
String[] list;
String[] list2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
Bundle bundle = getIntent().getExtras();
list = bundle.getStringArray(KEY_LIST);
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list));
}
@Override
protected void onListItemClick(ListView listView, View view, int position, long id) {
list2 = Data.getHistory((String) listView.getItemAtPosition(position));
Bundle bundle = new Bundle();
bundle.putStringArray(KEY_LIST, list2);
Intent intent = new Intent(ListActivityA.this, ListActivityB.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
ListActivityB中的:
public class ListActivityB extends ListActivity {
public static final String KEY_LIST = "list";
String[] list;
String[] list2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
Bundle bundle = getIntent().getExtras();
list = bundle.getStringArray(KEY_LIST);
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list));
}
@Override
protected void onListItemClick(ListView listView, View view, int position, long id) {
list2 = Data.getDetail((String) listView.getItemAtPosition(position));
Bundle bundle = new Bundle();
bundle.putStringArray(KEY_LIST, list2);
Intent intent = new Intent(ListActivityB.this, ListActivityC.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
ListActivitC中的:
public class ListActivityC extends ListActivity {
public static final String KEY_LIST = "list";
String[] list;
String[] list2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
Bundle bundle = getIntent().getExtras();
list = bundle.getStringArray(KEY_LIST);
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list));
}
@Override
protected void onListItemClick(ListView listView, View view, int position, long id) {
list2 = Data.getHistory((String) listView.getItemAtPosition(position));
}
}
数据示例:
public class Data {
public static String[] getName() {
String[] data = {"Andy", "Jack", "John"};
return data;
}
public static String[] getHistory(String name) {
String[] data = new String[] {};
switch (name) {
case "Andy":
data = new String[] {"Iphone6", "Sony Z", "Iphone6", "Iphone6"};
break;
case "Jack":
data = new String[] {"Samsung S6", "Samsung S6", "Samsung S5", "Iphone6"};
break;
case "John":
data = new String[] {"Iphone6", "Samsung S6", "Samsung S6", "Samsung S6"};
break;
}
return data;
}
public static String[] getDetail(String item) {
String[] data = new String[] {};
switch (item) {
case "Iphone6":
data = new String[] {"wareHouseA 10", "wareHouseB 15", "wareHouseC 8"};
break;
case "Sony Z":
data = new String[] {"wareHouseA 4", "wareHouseB 30"};
break;
case "Samsung S6":
data = new String[] {"wareHouseA 10", "wareHouseB 5", "wareHouseC 6"};
break;
}
return data;
}
}