我想设置两个活动,每个活动具有一个listview。为此,我已经为其设计设计了自己的xml文件,因此listview具有不同的textview和图像。但是我想在两个列表视图上使用相同的适配器。
那是我的carnes活动的java文件。
public class Carnes extends AppCompatActivity {
private ListView lvItems;
private Adapter adaptador;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carnes);
lvItems = (ListView) findViewById(R.id.lv_carnes);
adaptador = (Adapter) new Adaptador(this, GetArrayItems());
lvItems.setAdapter((ListAdapter) adaptador);
}
private ArrayList<Entidad> GetArrayItems() {
ArrayList<Entidad> listItems = new ArrayList<>();
listItems.add(new Entidad(R.drawable.calabacines_3, "Lomo", " 5 min.", 5, R.drawable.color_carnes));
listItems.add(new Entidad(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Pollo", " 20 min.", 4, R.drawable.color_carnes));
listItems.add(new Entidad(R.drawable.tomate, "Pavo", " 1 min.", 3, R.drawable.color_carnes));
return listItems;
}
}
这是我的适配器:
public class Adaptador extends BaseAdapter {
private Context context;
private ArrayList<Entidad> listItems;
public Adaptador(Context context, ArrayList<Entidad> listItems) {
this.context = context;
this.listItems = listItems;
}
@Override
public int getCount() {
return listItems.size();
}
@Override
public Object getItem(int position) {
return listItems.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Entidad Item = (Entidad) getItem(position);
convertView = LayoutInflater.from(context).inflate(R.layout.elemento_lista, null);
ImageView imgFoto = (ImageView) convertView.findViewById(R.id.imgFoto);
TextView tvTitulo = (TextView) convertView.findViewById(R.id.tvTitulo);
TextView tvTiempo = (TextView) convertView.findViewById(R.id.tvTiempo);
RatingBar calificacion = (RatingBar) convertView.findViewById(R.id.ratingBarVerd);
ImageView colorfondo = (ImageView) convertView.findViewById(R.id.colorfondo);
imgFoto.setImageResource(Item.getImgFoto());
tvTitulo.setText(Item.getTitulo());
tvTiempo.setText(Item.getTiempo());
calificacion.setRating(Item.getRating());
colorfondo.setImageResource(Item.getColorfondo());
return convertView;
}
}
我将下面使用相同适配器的其他活动留给您(记住这是一个不同的列表视图):
public class Verduras extends AppCompatActivity {
private ListView lvItems;
private Adapter adaptador;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verduras);
lvItems = (ListView) findViewById(R.id.lv_verduras);
adaptador = (Adapter) new Adaptador(this, GetArrayItems());
lvItems.setAdapter((ListAdapter) adaptador);
}
private ArrayList<Entidad> GetArrayItems() {
ArrayList<Entidad> listItems = new ArrayList<>();
listItems.add(new Entidad(R.drawable.calabacines_3, "Calabacines", " 10 min.", 4, R.drawable.color_verduras));
listItems.add(new Entidad(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Patatas", " 15 min.", 2, R.drawable.color_verduras));
listItems.add(new Entidad(R.drawable.tomate, "Tomates", " 2 min.", 5, R.drawable.color_verduras));
return listItems;
}
}
这就是我用来定义变量的内容:
public class Entidad {
private int imgFoto;
private String titulo;
private String tiempo;
private int ratingBar;
private int colorfondo;
public Entidad(int imgFoto, String titulo, String tiempo, int ratingBar, int colorfondo) {
this.imgFoto = imgFoto;
this.titulo = titulo;
this.tiempo = tiempo;
this.ratingBar = ratingBar;
this.colorfondo = colorfondo;
}
public int getImgFoto() {
return imgFoto;
}
public String getTitulo() {
return titulo;
}
public String getTiempo() {
return tiempo;
}
public int getRating() { return ratingBar; }
public int getColorfondo() {
return colorfondo;
}
}
发生的事情是该代码看起来还可以,但是当您运行该应用程序时,它突然停止了,而没有告诉您发生了什么事情。
感谢您的帮助,如果您需要其他任何帮助,请告诉我。
这就是logcat上显示的内容。
05-29 16:08:38.173 2247-2247/com.test.platos4 E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x7f060059
at android.content.res.Resources.getValue(Resources.java:1013)
at android.support.v7.widget.ResourcesWrapper.getValue(ResourcesWrapper.java:209)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:86)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:94)
at com.test.platos4.Adaptador.getView(Adaptador.java:62)
at android.widget.AbsListView.obtainView(AbsListView.java:2271)
at android.widget.ListView.makeAndAddView(ListView.java:1769)
at android.widget.ListView.fillDown(ListView.java:672)
at android.widget.ListView.fillFromTop(ListView.java:733)
at android.widget.ListView.layoutChildren(ListView.java:1622)
at android.widget.AbsListView.onLayout(AbsListView.java:2106)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1868)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1689)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
05-29 16:08:38.183 1379-1571/system_process W/ActivityManager: Force finishing activity com.test.platos4/.Carnes
05-29 16:08:38.323 1379-1571/system_process D/dalvikvm: GC_FOR_ALLOC freed 658K, 12% free 22447K/25415K, paused 19ms, total 19ms
05-29 16:08:38.323 1379-1571/system_process W/ActivityManager: Force finishing activity com.test.platos4/.Dishes
05-29 16:08:38.413 1121-1621/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1114112
05-29 16:08:38.853 1379-1392/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{a78e7c78 com.test.platos4/.Carnes}
05-29 16:08:39.403 1121-1456/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1114112
05-29 16:08:39.663 1121-1456/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1114112
05-29 16:08:48.043 1379-1392/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
Activity idle timeout for ActivityRecord{a78e7c78 com.test.platos4/.Carnes}
05-29 16:08:48.863 1379-1392/system_process W/ActivityManager: Activity idle timeout for ActivityRecord{a7ce95d0 com.test.platos4/.Menu}
05-29 16:08:58.043 1379-1392/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{a7ce7888 com.test.platos4/.Dishes}
Activity destroy timeout for ActivityRecord{a78e7c78 com.test.platos4/.Carnes}
05-29 16:09:00.013 1440-1440/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xb8af20d0: ver 2 0
答案 0 :(得分:0)
在您的代码中,如果您未使用相同的适配器,您将在两个Activities
中实例化一个新适配器,例如:new Adaptador(...)
这里是tutorial,关于将BaseAdapter与ListView一起使用
以下是本教程的摘要,显示了getView()
应该如何工作:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// inflate the layout for each list row
if (convertView == null) {
convertView = LayoutInflater.from(context).
inflate(R.layout.layout_list_view_row_items, parent, false);
}
// get current item to be displayed
Item currentItem = (Item) getItem(position);
// get the TextView for item name and item description
TextView textViewItemName = (TextView)
convertView.findViewById(R.id.text_view_item_name);
TextView textViewItemDescription = (TextView)
convertView.findViewById(R.id.text_view_item_description);
//sets the text for item name and item description from the current item object
textViewItemName.setText(currentItem.getItemName());
textViewItemDescription.setText(currentItem.getItemDescription());
// returns the view for the current row
return convertView;
}
}
要优化列表视图的性能,应使用新的行布局 仅当convertView == null时才膨胀。这是因为适配器的 每当listview需要显示新行时,都会调用getView方法 屏幕上。 convertView在此过程中被回收。因此, 当convertView == null时,行布局应该只膨胀一次, 并且它的内容应该在随后的getView调用中更新 每次通话增加新行的成本很高。
我也建议使用RecyclerView insted of ListView。创建RecyclerView
是为了解决ListView
曾经/曾经存在的许多问题。