我是Android界的新手,我在使用活动方面遇到了问题,我的问题是我想使用ListView制作应用程序,以显示业务类别列表,每个业务类别都会有另一个列表与公司的名称。
我的array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="categorias">
<item >Agencias de Autos y Motos</item>
<item >Agroinsumos</item>
<item >Audio Car</item>
<item >Arquitectura y Construcción</item>
<item >Autolavados</item>
<item >Autopartes</item>
<item >Balnearios</item>
</string-array>
</resources>
这是我的MainActivity及其布局:
package com.genebi.rubik;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.app.ListActivity;
import android.content.Intent;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] lista_categorias = getResources().getStringArray(R.array.categorias);
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.lista_categorias, R.id.nombreCategoria, lista_categorias));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
String categoria = ((TextView) v).getText().toString();
Intent i = new Intent(getApplicationContext(), ListaPorCategoria.class);
i.putExtra("categoria", categoria);
startActivity(i);
}
});
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/nombreCategoria"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip"
android:textSize="14sp"
android:textStyle="normal"
/>
</RelativeLayout>
当我点击MainActivity.class中显示的列表中的项目时,我的问题从第二个Activity开始
package com.genebi.rubik;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
public class ListaPorCategoria extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_por_categoria);
TextView txtCategoria = (TextView) findViewById(R.id.etiquetaCategoria);
Intent i = getIntent();
String categoriaSeleccionada = i.getStringExtra("categoria");
txtCategoria.setText(categoriaSeleccionada);
}
}
布局
<RelativeLayout 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: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=".ListaPorCategoria" >
<TextView android:id="@+id/etiquetaCategoria"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:padding="10dip"
android:textColor="#ffffff"/>
</RelativeLayout>
应用程序停止工作,我看不到我点击的类别的信息,显然它没有错误,但我不知道为什么不工作,我希望你能帮助我,谢谢。
修改
logcat的
06-05 13:45:14.667: D/OpenGLRenderer(20800): Enabling debug mode 0
06-05 13:45:19.692: D/AbsListView(20800): Get MotionRecognitionManager
06-05 13:45:19.732: I/Adreno200-EGLSUB(20800): <ConfigWindowMatch:2087>: Format RGBA_8888.
06-05 13:45:39.671: D/AndroidRuntime(20800): Shutting down VM
06-05 13:45:39.671: W/dalvikvm(20800): threadid=1: thread exiting with uncaught exception (group=0x40e40360)
06-05 13:45:39.671: E/AndroidRuntime(20800): FATAL EXCEPTION: main
06-05 13:45:39.671: E/AndroidRuntime(20800): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
06-05 13:45:39.671: E/AndroidRuntime(20800): at com.genebi.rubik.MainActivity$1.onItemClick(MainActivity.java:31)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.widget.AdapterView.performItemClick(AdapterView.java:301)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3071)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.widget.AbsListView$1.run(AbsListView.java:3973)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.os.Handler.handleCallback(Handler.java:615)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.os.Looper.loop(Looper.java:137)
06-05 13:45:39.671: E/AndroidRuntime(20800): at android.app.ActivityThread.main(ActivityThread.java:4950)
06-05 13:45:39.671: E/AndroidRuntime(20800): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 13:45:39.671: E/AndroidRuntime(20800): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 13:45:39.671: E/AndroidRuntime(20800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-05 13:45:39.671: E/AndroidRuntime(20800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-05 13:45:39.671: E/AndroidRuntime(20800): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
这是从listView上的clicked项获取值的错误方法:
String categoria = ((TextView) v).getText().toString();
因为v
是您在列中的行,您无法将其强制转换为TextView并从中获取文本,
您需要使用以下代码:
TextView c = (TextView) v.findViewById(R.id.TextViewID); // i think this is nombreCategoria
String categoria = c.getText().toString();
或者您可以使用position
从列表中获取值,例如以下代码:
String categoria = lista_categorias[position]
另一种方式是:
String categoria =(String) arg0.getItemAtPosition(position);