我试图做一个ListView,当我调用函数actualizarDisplay()时它可以刷新。 我在日志猫中看到了一条消息,我怀疑这条消息:
10-06 12:24:02.524: I/dalvikvm(6911): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
10-06 12:24:02.524: W/dalvikvm(6911): VFY: unable to resolve virtual method 561: Landroid/content/res/TypedArray;.getType (I)I
10-06 12:24:02.524: D/dalvikvm(6911): VFY: replacing opcode 0x6e at 0x0002
10-06 12:24:02.688: D/AbsListView(6911): Get MotionRecognitionManager
10-06 12:24:02.712: D/AbsListView(6911): onVisibilityChanged() is called, visibility : 4
10-06 12:24:02.712: D/AbsListView(6911): unregisterIRListener() is called
10-06 12:24:02.712: D/AbsListView(6911): onVisibilityChanged() is called, visibility : 0
10-06 12:24:03.055: D/AbsListView(6911): unregisterIRListener() is called
我的问题是,当我按下屏幕时,事件onClickListener没有被修改。
这是我的主要活动:
public class Rescate extends ActionBarActivity {
ArrayList<Ficha> listaFichas = new ArrayList<Ficha>();
MyCustomAdapter dataAdapter = null;
Ficha fichaAux = new Ficha();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rescate);
Bundle b = this.getIntent().getExtras();
int tipo=b.getInt("lugar");
switch(tipo) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
fichaAux.setNumInv("1");
fichaAux.setIden("cuadro");
fichaAux.setUbi("sacrsitía");
fichaAux.setNumHom(2);
fichaAux.setMat("Plástico");
fichaAux.setEstado(0);
fichaAux.setPrioridad(1);
listaFichas.add(fichaAux);
fichaAux = new Ficha();
fichaAux.setNumInv("2");
fichaAux.setIden("cuadro");
fichaAux.setUbi("sacrsitía");
fichaAux.setNumHom(2);
fichaAux.setMat("Plástico");
fichaAux.setEstado(0);
fichaAux.setPrioridad(2);
listaFichas.add(fichaAux);
actualizarDisplay();
break;
default:
setContentView(R.layout.activity_rescate);
}
}
这是我的函数actualizarDisplay,它实现了lsiteners:
public void actualizarDisplay()
{
dataAdapter = new MyCustomAdapter(this,R.layout.listadofichas, listaFichas);
ListView listView = (ListView) findViewById(R.id.listaObras);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Intent passIntent = new Intent();
passIntent.setClass(Rescate.this, VistaFicha.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("num",listaFichas.get(arg2).getNumInv() );
bundle.putCharSequence("ubicacion",listaFichas.get(arg2).getUbi() );
passIntent.putExtras(bundle);
startActivity(passIntent);
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3)
{
final String[] opciones = new String[] { "Si", "No"};
final AlertDialog.Builder dialogo = new AlertDialog.Builder(Rescate.this);
dialogo.setCancelable(true);
dialogo.setTitle("¿Desea establecer que la obra ha sido rescatada?");
dialogo.setItems(opciones, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0)
{
listaFichas.get(arg2).setEstado(1);
actualizarDisplay();
}
else
{
listaFichas.get(arg2).setEstado(0);
actualizarDisplay();
}
}
});
dialogo.setCancelable(false);
dialogo.create();
dialogo.show();
return false;
}
});
listView.setAdapter(dataAdapter);
}
这是我处理ListView的函数:
public class FichaAdapter extends BaseAdapter
{
private ArrayList<Ficha> fichas;
public FichaAdapter(ArrayList<Ficha> fichas)
{
this.fichas = fichas;
//Cada vez que cambiamos los elementos debemos noficarlo
notifyDataSetChanged();
}
public int getCount()
{
return fichas.size();
}
public Object getItem(int position)
{
return fichas.get(position);
}
public long getItemId(int position)
{
return 0;
}
public View getView(int position, View convertView, ViewGroup parent)
{
FichaView view;
if (convertView == null) //NO existe, creamos uno
view = new FichaView(parent.getContext());
else //Existe, reutilizamos
view = (FichaView) convertView;
view.setFicha(fichas.get(position));
return view;
}
}
private class MyCustomAdapter extends ArrayAdapter<Ficha>
{
private ArrayList<Ficha> fichaList;
public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<Ficha> fichaList)
{
super(context, textViewResourceId,fichaList);
this.fichaList = new ArrayList<Ficha>();
this.fichaList.addAll(fichaList);
}
private class ViewHolder
{
TextView num,iden,ubi,hombres,material;
ImageView foto;
RelativeLayout fondo;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listadofichas, null);
holder = new ViewHolder();
holder.num = (TextView) convertView.findViewById(R.id.numInvResCon);
holder.iden = (TextView) convertView.findViewById(R.id.idenRescCont);
holder.ubi = (TextView) convertView.findViewById(R.id.ubiResCon);
holder.hombres = (TextView) convertView.findViewById(R.id.numHomResCon);
holder.material = (TextView) convertView.findViewById(R.id.matRescCon);
holder.foto = (ImageView) convertView.findViewById(R.id.imaResc);
holder.fondo = (RelativeLayout) convertView.findViewById(R.id.fondoRes);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Ficha ficha = fichaList.get(position);
holder.num.setText(ficha.getNumInv());
holder.iden.setText(ficha.getIden());
holder.ubi.setText(ficha.getUbi());
holder.hombres.setText(String.valueOf(ficha.getNumHom()));
holder.material.setText(ficha.getMat());
if(ficha.getPrioridad()==1)
{
holder.num.setTextColor(Color.RED);
holder.iden.setTextColor(Color.RED);
holder.ubi.setTextColor(Color.RED);
holder.hombres.setTextColor(Color.RED);
holder.material.setTextColor(Color.RED);
}
if(ficha.getPrioridad()==2)
{
holder.num.setTextColor(Color.parseColor("#FF8000"));
holder.iden.setTextColor(Color.parseColor("#FF8000"));
holder.ubi.setTextColor(Color.parseColor("#FF8000"));
holder.hombres.setTextColor(Color.parseColor("#FF8000"));
holder.material.setTextColor(Color.parseColor("#FF8000"));
}
if(ficha.getPrioridad()==3)
{
holder.num.setTextColor(Color.GREEN);
holder.iden.setTextColor(Color.GREEN);
holder.ubi.setTextColor(Color.GREEN);
holder.hombres.setTextColor(Color.GREEN);
holder.material.setTextColor(Color.GREEN);
}
if(ficha.getEstado()== 1)
{
holder.fondo.setBackgroundColor(Color.GREEN);
holder.num.setTextColor(Color.WHITE);
holder.iden.setTextColor(Color.WHITE);
holder.ubi.setTextColor(Color.WHITE);
holder.hombres.setTextColor(Color.WHITE);
holder.material.setTextColor(Color.WHITE);
}
return convertView;
}
}
我认为这个问题是日志猫说的可见性:0 我在这一行有一个警告:
convertView = vi.inflate(R.layout.listadofichas, null);
我认为问题出在这里。
我写的布局:
listadofichas.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fondoRes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:contentDescription="@string/par" >
<ImageView
android:id="@+id/imaResc"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="40dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="1dp"
android:contentDescription="@string/vacia"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/numInv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imaResc"
android:layout_marginLeft="100dp"
android:layout_toRightOf="@+id/imaResc"
android:text="@string/numInvResc"
android:textColor="#000000"
android:textSize="@dimen/letrasPequenasAumen" />
<TextView
android:id="@+id/idenResc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/numInv"
android:layout_below="@+id/numInv"
android:layout_marginTop="12dp"
android:text="@string/idenResc"
android:textColor="#000000"
android:textSize="@dimen/letrasPequenasAumen" />
<TextView
android:id="@+id/ubiResc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/idenResc"
android:layout_below="@+id/idenResc"
android:layout_marginTop="12dp"
android:text="@string/ubiResc"
android:textColor="#000000"
android:textSize="@dimen/letrasPequenasAumen" />
<TextView
android:id="@+id/numHomResc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ubiResc"
android:layout_below="@+id/ubiResc"
android:layout_marginTop="12dp"
android:text="@string/numHomResc"
android:textColor="#000000"
android:textSize="@dimen/letrasPequenasAumen" />
<TextView
android:id="@+id/matResc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/numHomResc"
android:layout_below="@+id/numHomResc"
android:layout_marginTop="12dp"
android:text="@string/matResc"
android:textColor="#000000"
android:textSize="@dimen/letrasPequenasAumen" />
<TextView
android:id="@+id/numInvResCon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/idenResc"
android:layout_marginLeft="84dp"
android:layout_toRightOf="@+id/numInv"
android:text="@string/vacia"
android:textSize="@dimen/letrasPequenasAumen" />
<TextView
android:id="@+id/idenRescCont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/idenResc"
android:layout_alignLeft="@+id/numInvResCon"
android:text="@string/vacia"
android:textSize="@dimen/letrasPequenasAumen" />
<TextView
android:id="@+id/ubiResCon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/numHomResc"
android:layout_alignLeft="@+id/idenRescCont"
android:text="@string/vacia"
android:textSize="@dimen/letrasPequenasAumen" />
<TextView
android:id="@+id/numHomResCon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/matResc"
android:layout_alignLeft="@+id/ubiResCon"
android:text="@string/vacia"
android:textSize="@dimen/letrasPequenasAumen" />
<TextView
android:id="@+id/matRescCon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/matResc"
android:layout_alignBottom="@+id/matResc"
android:layout_alignLeft="@+id/numHomResCon"
android:text="@string/vacia"
android:textSize="@dimen/letrasPequenasAumen" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imaResc"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:layout_toEndOf="@+id/imaResc"
android:layout_toRightOf="@+id/imaResc"
android:contentDescription="@string/guia"
android:src="@drawable/barralat" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/matResc"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:src="@drawable/barrahor" />
<Spinner
android:id="@+id/spinner2"
style="@style/mySpinnerItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_below="@+id/spinner1"
android:layout_marginTop="13dp"
android:entries="@array/equipos" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/numInvResCon"
android:layout_alignBottom="@+id/numInvResCon"
android:layout_marginLeft="203dp"
android:layout_toRightOf="@+id/numInvResCon"
android:text="@string/equiposAsig"
android:textColor="#000000"
android:textSize="@dimen/letrasPequenasAumen" />
<Spinner
android:id="@+id/spinner1"
style="@style/mySpinnerItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="13dp"
android:layout_marginLeft="13dp"
android:entries="@array/equipos" />
</RelativeLayout>
and the layout of the activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.gepv.Rescate" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<ListView
android:id="@+id/listaObras"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="60dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="20dp"
tools:listitem="@layout/listadofichas" >
</ListView>
</RelativeLayout>
</LinearLayout>
adpater效果很好,因为我可以改变颜色,但听众并不工作 任何人都可以帮助我吗?
答案 0 :(得分:1)
在你的活动的onCreate中,你有一个开关,只有当值tipo为4时才启动适配器,所以如果不是,则listview没有适配器而且什么都不做。您可以调试以查看它获得的值或至少放置行
Log.d("Rescate","tipo = "+tipo)
查看它具有的值。要查看onItemClick是否被调用,您可以插入onItem的第一行单击该行:
Log.d("Rescate","onItemClick")
当您单击列表中的项目时,查看它是否出现在日志中。
我认为错误可能与ActionBarActivity
已弃用的事实有关,您应该使用AppCompatActivity
。
如果它仍然无效,你还可以发布你使用的布局文件吗?
答案 1 :(得分:1)
所以这是我的代码与String列表项而不是Ficha。我让'Rescate.java'首先扩展AppCompatActivity
然后ActionBarActivity
,它适用于两个版本:
public class Rescate extends ActionBarActivity
{
ArrayList<String> listaFichas = new ArrayList<>();
MyCustomAdapter dataAdapter = null;
String fichaAux = "Hello";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// setting this up with Strings to keep things simple
listaFichas.add("Hello");
listaFichas.add("Hi there");
actualizarDisplay();
}
public void actualizarDisplay()
{
dataAdapter = new MyCustomAdapter(this,R.layout.listadofichas, listaFichas);
ListView listView = (ListView) findViewById(R.id.listaObras);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Intent passIntent = new Intent();
passIntent.setClass(Rescate.this, VistaFicha.class);
startActivity(passIntent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3)
{
final String[] opciones = new String[]{"Si", "No"};
final AlertDialog.Builder dialogo = new AlertDialog.Builder(Rescate.this);
dialogo.setCancelable(true);
dialogo.setTitle("¿Desea establecer que la obra ha sido rescatada?");
dialogo.setItems(opciones, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item)
{
if (item == 0)
{
actualizarDisplay();
Toast.makeText(Rescate.this, "item = 0", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(Rescate.this, "item != 0", Toast.LENGTH_SHORT).show();
}
}
});
dialogo.setCancelable(false);
dialogo.create();
dialogo.show();
return false;
}
});
listView.setAdapter(dataAdapter);
}
}
'VistaFicha.java'只是一个显示“Hello World”的空白活动。
我将adpater放在一个单独的文件'MyCustomAdapter.java'
中public class MyCustomAdapter extends ArrayAdapter
{
private ArrayList<String> fichaList;
private LayoutInflater vi;
public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<String> fichaList)
{
super(context, textViewResourceId,fichaList);
this.fichaList = new ArrayList<String>();
this.fichaList.addAll(fichaList);
// I had to change the original code because my adapter is not an inner class:
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder
{
TextView num;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
convertView = vi.inflate(R.layout.listadofichas, null);
holder = new ViewHolder();
holder.num = (TextView) convertView.findViewById(R.id.tvHallo);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
String ficha = fichaList.get(position);
holder.num.setText(ficha);
return convertView;
}
}
列表行的xml只是TextView
内的LinearLayout
(R.id.tvHallo)而我的Rescate活动只包含通常的“Hello World”和ListView
(R.id.listaObras)
希望这会有所帮助,如果有任何问题,请告诉我们。)