这是我的第一个活动列表。
我需要在第一个项目中创建另一个列表活动,并在任何项目中创建另一个列表活动。
在第二个列表中,我需要在不重复代码的情况下完成这些操作,并且我可以在哪里更改每个列表的数据
Toolbar mToolbar;
ListView mListView;
String[] countryNames = {"Australia", "Brazil", "China", "France", "Germany", "India", "Ireland", "Italy"
, "Mexico", "Poland", "Russia", "Spain", "US","Ahmed"};
int[] countryFlags = {R.drawable.flag_australia,
R.drawable.flag_brazil,
R.drawable.flag_china,
R.drawable.flag_france,
R.drawable.flag_germany,
R.drawable.flag_india,
R.drawable.flag_ireland,
R.drawable.flag_italy,
R.drawable.flag_maxico,
R.drawable.flag_poland,
R.drawable.flag_russia,
R.drawable.flag_spain,
R.drawable.flag_us,
R.drawable.ana};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitle(getResources().getString(R.string.app_name));
mListView = (ListView) findViewById(R.id.listview);
MyAdapter myAdapter = new MyAdapter(MainActivity.this, countryNames, countryFlags);
mListView.setAdapter(myAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent mIntent = new Intent(MainActivity.this, DetailActivity.class);
mIntent.putExtra("countryName", countryNames[i]);
mIntent.putExtra("countryFlag", countryFlags[i]);
startActivity(mIntent);
}
});
}
}
这是我的Adpater代码
public class MyAdapter extends ArrayAdapter<String> {
String[] names;
int[] flags;
Context mContext;
public MyAdapter(Context context, String[] countryNames, int[] countryFlags)
{
super(context, R.layout.listview_item);
this.names = countryNames;
this.flags = countryFlags;
this.mContext = context;
}
@Override
public int getCount() {
return names.length;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder = new ViewHolder();
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.listview_item, parent, false);
mViewHolder.mFlag = (ImageView) convertView.findViewById(R.id.imageView);
mViewHolder.mName = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mFlag.setImageResource(flags[position]);
mViewHolder.mName.setText(names[position]);
return convertView;
}
static class ViewHolder {
ImageView mFlag;
TextView mName;
}
}
答案 0 :(得分:0)
此处列出的第二个活动是详细活动...
而且你只有两个细节(名称和标志),而不是列表......
所以,也许以下不是你想要的
您提到使用Firebase进行登录。您可以将列表存储在Firebase中并查询它们,这是一回事。
无论您是否在Firebase中拥有数据,我仍然建议您创建一个可以序列化的类。 (虽然研究Parcelable
是一个更好的选择)。
如果您需要&#34; 3项&#34;就像你说的,这就是你所做的。
public class Country implements Serializable {
String name;
int flag;
public Country(String name, int flag) {
this.name = name;
this.flag = flag;
}
}
将列表存储在您可以从不同位置访问的位置。
根据需要添加更多参数。
public List<Country> countries = new ArrayList<>(Arrays.asList(
new Country("Australia", R.drawable.flag_australia),
new Country("Brazil", R.drawable.flag_brazil)
));
您可能想要制作此static
,但请尽量不要。
修复您的适配器以接受该类而不是两个(或更多)数组。
public class MyAdapter extends ArrayAdapter<Country> {
public MyAdapter(Context context, List<Country> countries) {
super(context, R.layout.listview_item, countries);
this.mContext = context;
}
// TODO: Update getView too
然后你应该可以用它来启动另一个Activity
MyAdapter myAdapter = new MyAdapter(MainActivity.this, countries);
mListView.setAdapter(myAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Country country = myAdapter.getItem(i);
Intent mIntent = new Intent(MainActivity.this, DetailActivity.class);
mIntent.putExtra("country", country);
startActivity(mIntent);
}
});
在下一个活动中,您可以使用
Country country = null;
Intent extras = getIntent().getExtras();
if (extras != null) {
country = (Country) extras.getSerializable("country");
}
if (country != null) {
nameTextView.setText(country.name);
flagImageView.setImageResource(country.flag);
}