我的项目我需要在两个不同的活动中显示相同的arrayList。在MainActivity中,我只需要显示具有isConsumido atribute = false的项目。在TelaCadastrados活动中,我需要显示整个arrayList。我是怎么做到的?
package com.example.breno.seenme;
public class CustomAdapter extends ArrayAdapter<ItemCultural> {
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public CustomAdapter(Context context, int resource, List<ItemCultural> items) {
super(context, resource, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(this.getItem(position).isConsumido()){
// I Think the code to hide elements with isConsumido = true
}else{
}
return v;
}
}
在我的MainActivity适配器实例中。
CustomAdapter myAdapter = new CustomAdapter(this, android.R.layout.simple_list_item_1, CustomAdapter.VIEW_TYPE_MAINACTIVITY);
此customAdapter如何识别我的列表?
我的Custon适配器:
package com.example.breno.seenme;
public class CustomAdapter extends ArrayAdapter<ItemCultural> {
public static final int VIEW_TYPE_MAINACTIVITY = 124; // or some random int.
public static final int VIEW_TYPE_TELACADAS = 125; // or some random int.
private int viewType;
List<ItemCultural> lista;
public CustomAdapter(Context context, int textViewResourceId, int viewType) {
super(context, textViewResourceId);
this.viewType = viewType;
}
public CustomAdapter(Context context, int resource, List<ItemCultural> items, int viewType) {
super(context, resource, items);
this.viewType = viewType;
this.lista = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (viewType == VIEW_TYPE_MAINACTIVITY) {
// DO your logic for mainActivity version of the list.
// I Think the code to hide elements with isConsumido = true
if (!lista.get(position).isConsumido()) {
//showThisView(v);
v.setVisibility(View.GONE);
} else {
v.setVisibility(View.VISIBLE);
}
} else if (viewType == VIEW_TYPE_TELACADAS) {
// DO your logic for Telacadas version of the list
} else {
// UNSUPPORTED VIEW TYPE.
}
return v;
}
}
答案 0 :(得分:1)
While constructing the adapter, pass which type of View you want to render, the MainActivity
version of the list or the TelaCadastradosActivity
version of the list.
So you modify your adapter to this
package com.example.breno.seenme;
public class CustomAdapter extends ArrayAdapter<ItemCultural> {
public static final int VIEW_TYPE_MAINACTIVITY = 124; // or some random int.
public static final int VIEW_TYPE_TELACADAS = 125; // or some random int.
private int viewType;
List<ItemCultural> lista;
private final LayoutInflater mInflater;
private int resource;
public CustomAdapter(Context context, int textViewResourceId, int viewType) {
super(context, textViewResourceId);
this.viewType = viewType;
mInflater = LayoutInflater.from(context);
}
public CustomAdapter(Context context, int resource, List<ItemCultural> items, int viewType) {
super(context, resource, items);
this.viewType = viewType;
this.lista = items;
mInflater = LayoutInflater.from(context);
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = mInflater.inflate(resource, parent, false);
} else {
v = convertView;
}
if (viewType == VIEW_TYPE_MAINACTIVITY) {
// DO your logic for mainActivity version of the list.
// I Think the code to hide elements with isConsumido = true
if (!lista.get(position).isConsumido()) {
//showThisView(v);
v.setVisibility(View.GONE);
} else {
v.setVisibility(View.VISIBLE);
((TextView)v).setText(lista.get(position).toString());
}
} else if (viewType == VIEW_TYPE_TELACADAS) {
// DO your logic for Telacadas version of the list
} else {
// UNSUPPORTED VIEW TYPE.
}
return v;
}
}
And in your MainActivity
you instantiate your adapter like this
final CustomAdapter arrayAdapterOrdenado = new CustomAdapter(this, android.R.layout.simple_list_item_1, regraDeNegocioSingleton.getListaDeItensSingleton().getListaDeItensCulturais(). CustomAdapter.VIEW_TYPE_MAINACTIVITY);
For TelaCadasActivity you pass in the TelaCadas view type
final CustomAdapter arrayAdapterOrdenado = new CustomAdapter(this, android.R.layout.simple_list_item_1, regraDeNegocioSingleton.getListaDeItensSingleton().getListaDeItensCulturais(). CustomAdapter.VIEW_TYPE_TELACADAS);
答案 1 :(得分:0)
You have 2 options. In your MainActivity, you could pass your adapter a copy of your data with only the isConsumido == false entries. Alternatively, you could add some logic to your adapter to skip over the items where isConsumido == true. You would have to update the getCount() and getView() methods to take into account the items to skip over.
It's the usual trade off of memory (more memory in the first solution) versus complexity (the 2nd solution is more complex, but more memory efficient as it uses a single list).