我正在创建一个Android应用,其中第二个活动显示2个列表视图,自定义以显示图像,两个标签和每行一个按钮。它是一款游戏,它展示了在发送和接收时分离的持续挑战。 我已经阅读了一些教程,所有这些教程都告诉我创建一个XML文件来设计行,一个用于挑战的类,另一个用于数组适配器,最后将它应用于onCreate on my activity。 但是,当我调试项目时,应用程序在访问包含两个列表视图的活动时崩溃。这是我的代码,其中一小部分是西班牙语,希望你不要介意:
挑战课(Partida.java):
package com.example.tuneit;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.content.Intent;
import android.app.*;
import android.widget.ImageButton;
public class Partida {
private String partner;
private String kind;
private String song;
private String date;
private String genre;
public Partida(String partner, String kind, String song, String date, String genre) {
//super();?
this.partner = partner;
this.kind = kind;
this.song = song;
this.date = date;
this.genre = genre;
}
public String getPartner() {
return partner;
}
public void setPartner(String nameText) {
this.partner = nameText;
}
public String getKind() {
return kind;
}
public void setKind(String nameText) {
this.kind = nameText;
}
public String getSong() {
return song;
}
public void setSong(String nameText) {
this.song = nameText;
}
public String getDate() {
return date;
}
public void setDate(String nameText) {
this.date = nameText;
}
public String getGenre() {
return genre;
}
public void setGenre(String nameText) {
this.genre = nameText;
}
}
Challenges的特殊数组适配器类(PartidasAdap.java):
package com.example.tuneit;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class PartidasAdap extends ArrayAdapter<Partida>{
private int resource;
private LayoutInflater inflater;
private Context context;
public PartidasAdap(Context ctx, int resourceId,List<Partida> objects) {
super(ctx, resourceId, objects);
resource = resourceId;
inflater = LayoutInflater.from( ctx );
this.context=ctx;
}
public View getView ( int position, View convertView, ViewGroup parent ) {
convertView = ( RelativeLayout ) inflater.inflate( resource, null );
Partida P = getItem( position );
TextView PlayerName = (TextView) convertView.findViewById(R.id.PlayerName);
PlayerName.setText(P.getPartner());
TextView fecha = (TextView) convertView.findViewById(R.id.fecha);
fecha.setText(P.getDate());
ImageView imgenero = (ImageView) convertView.findViewById(R.id.genreIma);
String uri = "drawable/" + P.getGenre();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable image = context.getResources().getDrawable(imageResource);
imgenero.setImageDrawable(image);
return convertView;
}
}
显示列表视图的活动(menu.java)。忽略不相关的代码:
package com.example.tuneit;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.content.Context;
import android.content.Intent;
import android.app.*;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class menu extends ActionBarActivity {
private Context ctx;
private ListView rec;
private ListView env;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
rec = (ListView) findViewById(R.id.list);
env = (ListView) findViewById(R.id.list2);
ctx=this;
gotoReceived(findViewById(R.id.list));
ActionBar ab = getSupportActionBar();
ab.hide();
List<Partida> recibidas = new ArrayList<Partida>();
List<Partida> enviadas = new ArrayList<Partida>();
recibidas.add(new Partida("exampleUser", "rec", "Feliz cumpleaños", "13-5-2002", "other"));
enviadas.add(new Partida("exapleuser", "env", "Feliz cumpleaños", "13-5-2007", "other"));
rec.setAdapter( new PartidasAdap(ctx, R.layout.rows, recibidas) );
env.setAdapter( new PartidasAdap(ctx, R.layout.rows, enviadas) );
}
当我将每行标记为注释时,应用程序没有崩溃,除了menu.java中自生成的注释,但是自定义列表视图都没有出现,所以问题显而易见那里。 我怀疑它与PartidasAdap类中的getView方法永远不会被调用的事实有关,但是我在几个教程中检查了这个并且它们都没有使用它,所以我尊重它。 任何帮助将不胜感激,提前谢谢。
编辑:几周后,我修复了我的电脑,可以调试你的建议,但是他们没有工作。但是,我现在在pastebin中有错误日志,请参阅http://pastebin.com/a0sYx6uk答案 0 :(得分:0)
在getView中创建一个单独的xml文件作为列表和代码的行,如下所示
LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.your_row_for_items,parent,false);
答案 1 :(得分:0)
我认为问题出现在你的inflater中,使用它:
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.rows, parent, false);
不需要投射视图。
答案 2 :(得分:0)
我会以这种方式编写你的适配器类..我不确定你的方式也可能是正确的但这种方式对我来说很好
public class PartidasAdap extends ArrayAdapter<Partida>{
private int resource;
private LayoutInflater inflater;
private Context context;
List<Partida> objects;
public PartidasAdap(Context ctx, int resourceId,List<Partida> objects) {
super(ctx, resourceId, objects);
resource = resourceId;
inflater = LayoutInflater.from( ctx );
this.context=ctx;
this.objects=objects;
}
public View getView ( int position, View convertView, ViewGroup parent ) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.your_row_for_items, parent,false);
Partida P = objects.get(position);
TextView PlayerName = (TextView) convertView.findViewById(R.id.PlayerName);
PlayerName.setText(P.getPartner());
TextView fecha = (TextView) convertView.findViewById(R.id.fecha);
fecha.setText(P.getDate());
ImageView imgenero = (ImageView) convertView.findViewById(R.id.genreIma);
String uri = "drawable/" + P.getGenre();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable image = context.getResources().getDrawable(imageResource);
imgenero.setImageDrawable(image);
return convertView;
}
}