简体   繁体   English

Android上的Activity和ListView问题

[英]Issue with Activities and ListView on Android

I'm new in the world of Android and I have a problem with the use of activities, my problem that I want to make an app using ListView to show a list of categories of business and every business category will has another list with the names of the companies. 我是Android领域的新手,我在使用活动时遇到问题,我的问题是我想使用ListView制作一个应用程序以显示业务类别列表,而每个业务类别都会有另一个名称列表公司。

My array.xml 我的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>

This is my MainActivity and its layout: 这是我的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);
            }
        });
    }

}

Layout 布局

    <?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>

And my problem starts here with the second Activity when i give click on a item of the list showed in MainActivity.class 当我单击MainActivity.class中显示的列表项时,我的问题就从第二个活动开始

  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);
    }

}

Layout 布局

    <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>

The application just stop working and I can't see the info of the category that i clicked, apparently it has not errors but i have no idea why is not working, i hope you can help me, thank you. 该应用程序刚刚停止工作,我看不到我单击的类别的信息,显然它没有错误,但是我不知道为什么不工作,希望您能为我提供帮助,谢谢。

EDIT 编辑

Logcat 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)

this is wrong way for getting value from clicked item on listView: 这是从listView的单击项中获取价值的错误方法:

String categoria = ((TextView) v).getText().toString();

as v is your row in list you can't cast that to TextView and get text from that, 由于v是列表中的行,因此无法将其强制转换为TextView并从中获取文本,

you need use following code: 您需要使用以下代码:

TextView c = (TextView) v.findViewById(R.id.TextViewID);  // i think this is nombreCategoria
String categoria = c.getText().toString();

or you can use position for getting value from list, like following code: 或者您可以使用position从列表中获取价值,例如以下代码:

String  categoria = lista_categorias[position]

and another way is: 另一种方法是:

String categoria =(String) arg0.getItemAtPosition(position);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM