我需要在列表中显示自定义项目,但在尝试了许多不同的方法来解决问题后,我仍然坚持这个错误:
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{quinto3.et37.agendadeviajes/quinto3.et37.agendadeviajes.Principal}:
> java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
> cannot be cast to android.widget.AbsListView$LayoutParams
这是主要活动
public class Principal extends Activity {
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
private ArrayAdapter<TipoDeViaje> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.agenda_principal);
ListView lista = (ListView) findViewById(R.id.lista);
View footer = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_add_button, (ViewGroup) getWindow().getDecorView().getRootView(), false);
lista.addFooterView(footer, null, false);
llenarLista();
registerForContextMenu(lista);
}
private void llenarLista() {
ListView lista = (ListView) findViewById(R.id.lista);
TipoDeViajeDAO tManager = new TipoDeViajeDAO(this);
ArrayList<TipoDeViaje> tipoList = tManager.traerTodos(this);
mAdapter = new AdapterTipoDeViaje(this,tipoList);
lista.setAdapter(mAdapter);
}
public void footerClick(View v) {
Toast.makeText(this, "pls", Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, EditTipoDeViaje.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
llenarLista();
}
这是我的自定义适配器
public class AdapterTipoDeViaje extends ArrayAdapter<TipoDeViaje> {
public AdapterTipoDeViaje(Context context, ArrayList<TipoDeViaje> tipos) {
super(context, 0, tipos);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
TipoDeViaje t = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.lista_fila, parent, false);
}
// Lookup view for data population
TextView desdeText = (TextView) convertView.findViewById(R.id.desde_text);
TextView hastaText = (TextView) convertView.findViewById(R.id.hasta_text);
// Populate the data into the template view using the data object
desdeText.setText(t.getDesde().getNombre());
hastaText.setText(t.getHasta().getNombre());
// Return the completed view to render on screen
return convertView;
}
这是我的lista_fila.xml(我想用于行的布局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/desde_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="desde"
android:textSize="30sp" />
<TextView
android:id="@+id/hasta_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hasta"
android:textSize="30sp" />
<ImageButton
android:id="@+id/boton_agregar_registro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/boton_agregar_registro"
android:src="@drawable/ic_add_viaje" />
</LinearLayout>
这是agenda_principal
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/lista"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
这是footer_add_button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="7dip" >
<ImageButton
android:id="@+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:gravity="center"
android:onClick="footerClick"
android:src="@drawable/ic_add_button"
android:contentDescription="@string/desc_footer_button"/>
</LinearLayout>
答案 0 :(得分:1)
问题在于页脚膨胀。
试试这个:
View footer = LayoutInflater.from(this).inflate(R.layout.footer_add_button, lista, false);
lista.addFooterView(footer);