简体   繁体   English

ListView中LinearLayout上的事件

[英]Event on LinearLayout in ListView

I try to get data which is in a LinearLayout which is himself in a ListView . 我尝试获取在LinearLayout数据,而LinearLayout本身在ListView So , in my logic, i put an id on the LinearLayout , and i add an OnToucheListener . 因此,按照我的逻辑,我将一个id放在LinearLayout ,然后添加一个OnToucheListener But when i run the app the application quits unexpectedly. 但是,当我运行该应用程序时,该应用程序意外退出。

I would like to get the TexTview which is in the LinearLayout , for after to allow of change data in an AlertDialog . 我想在LinearLayout获取TexTview ,以允许在AlertDialog中更改数据。

I implements my ListView with a SimpleAdapter in onPostExecute() of my AsyncTask , so until now, all works, but i don't know how to get the element(textview) of the linearLayout who is my listView.. 我在AsyncTask onPostExecute()中使用SimpleAdapter实现了ListView ,所以到目前为止,所有工作都onPostExecute() ,但是我不知道如何获取我的listView的linearLayout的element(textview)。

So, it's my code to try to get the linearLayout with AlertDialog to allow to change data. 因此,这是我的代码,尝试使用AlertDialog获取linearLayout以允许更改数据。

LayoutInflater factory = LayoutInflater.from(this);
    final View alertDialogView = factory.inflate(R.layout.alert_dialog_divers, null);


    LinearLayout linear = (LinearLayout) findViewById(R.id.test);
    linear.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            AlertDialog.Builder adb = new AlertDialog.Builder(listview.this);       
            adb.setView(alertDialogView);
            adb.setTitle("Divers");             
            adb.setIcon(android.R.drawable.ic_dialog_alert);            
            adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {

                    EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1);

                }
            });

            adb.setNegativeButton("Annuler", null);
            //on affiche la boite de dialogue
            adb.show();
        }
    });

And it's my listView xml file. 这是我的listView xml文件。 (I pet in red color to better see where is my linearLayout) (我用红色宠爱,以更好地了解我的linearLayout在哪里)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="230dp"
    android:layout_weight="0.56"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@drawable/border"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/collector_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="> B10954-1"
            android:textSize="30sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/adress"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:layout_weight="0"
            android:text="AIDER BOURGOGNE 14 Rue de la Breuchillière 21000 DIJON "
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginLeft="80dp"
            android:layout_marginTop="5dp"
            android:text="@string/depose"
            android:textSize="25sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/product_code"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:padding="5dp"
            android:text="3 F50D - 2 C50HD - 3 F50D - 2 C50HD - 4 BA300 - 5CE2 - 4F30E"
            android:textSize="15sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/border"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.75"
            android:background="@drawable/border"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/border" >

                <TextView
                    android:id="@+id/begin_hour"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:paddingLeft="5dp"
                    android:text="07:30-12:30 "
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/end_hour"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:paddingLeft="5dp"
                    android:text="14:00-19:00"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"                    
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:text="@string/ordre"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textSize="25sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/order"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:text="2"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textSize="20sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:text="@string/tpsarret"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textSize="25sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/downtime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:text="2 min"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textSize="20sp" />

                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

Below, the linearLyout in question with id : test ---------------------------------

        <LinearLayout
            android:id="@+id/test"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.5" 
            android:background="@drawable/linear_option"
            android:clickable="true"               
            android:orientation="vertical" >
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/border"
                android:weightSum="2" >
                <TextView
                    android:id="@+id/fax"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:paddingLeft="5dp"
                    android:text="03 80 91 03 03 "
                    android:textSize="20sp"
                    android:textStyle="bold" />
                <TextView
                    android:id="@+id/tel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text=" 06 43 66 53 16"
                    android:textSize="20sp"
                    android:textStyle="bold" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/crayon_icon" />
                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:text="@string/divers"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textSize="25sp"
                    android:textStyle="bold" />
            </LinearLayout>
            <TextView
                android:id="@+id/miscellaneous"
                android:layout_width="333dp"
                android:layout_height="fill_parent"
                android:layout_marginLeft="5dp"
                android:paddingLeft="40dp"
                android:textSize="20sp" />
        </LinearLayout>

End of the linearLayout in question ----------------------------------------------------

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:background="@drawable/linear_option"
            android:clickable="true"  
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView6"
                android:layout_width="fill_parent"
                android:layout_height="28dp"
                android:background="@drawable/border" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/crayon_icon" />

                <TextView
                    android:id="@+id/textView12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:text="@string/infosco"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textSize="25sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <TextView
                android:id="@+id/infos"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginLeft="5dp"
                android:paddingLeft="40dp"
                android:textSize="20sp" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="118dp"
    android:layout_height="230dp"
    android:background="@drawable/border"
    android:orientation="vertical" 
    android:descendantFocusability="blocksDescendants">

    <FrameLayout
        android:layout_width="66dp"
        android:layout_height="73dp"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:background="@drawable/editer_doc" >

    </FrameLayout>

</LinearLayout>

As you can see, there are many LinearLayout parent. 如您所见,有很多LinearLayout父级。 So when i touche the layout with id = test, i want to get the textView with id : miscellaneous and allow to change it. 所以当我用id = test触摸布局时,我想用id:杂项获取textView并允许对其进行更改。

I hope that you will understand. 希望您能理解。

EDIT 1 : 编辑1:

02-12 08:45:54.370: E/AndroidRuntime(9076): FATAL EXCEPTION: main
02-12 08:45:54.370: E/AndroidRuntime(9076): java.lang.RuntimeException: Unable to start              activity ComponentInfo{com.main/com.main.listview}: java.lang.NullPointerException
02-12 08:45:54.370: E/AndroidRuntime(9076):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at  android.os.Looper.loop(Looper.java:137)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at java.lang.reflect.Method.invoke(Method.java:511)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at  dalvik.system.NativeStart.main(Native Method)
02-12 08:45:54.370: E/AndroidRuntime(9076): Caused by: java.lang.NullPointerException
02-12 08:45:54.370: E/AndroidRuntime(9076):     at  com.main.listview.onCreate(listview.java:68)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at android.app.Activity.performCreate(Activity.java:4465)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-12 08:45:54.370: E/AndroidRuntime(9076):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

EDIT 2 : 编辑2:

public class listview extends Activity {

private ListView maListViewPerso;   
ImageButton imgButton;
Boolean isConnected;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color
            .parseColor("#000000")));

    maListViewPerso = (ListView) findViewById(R.id.listviewperso);
    isConnected = isOnline(); 

    getData(isConnected);

     //On instancie notre layout en tant que View
    LayoutInflater factory = LayoutInflater.from(this);
    final View alertDialogView = factory.inflate(R.layout.alert_dialog_divers, null);


    LinearLayout linear = (LinearLayout) findViewById(R.id.test);
    linear.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            AlertDialog.Builder adb = new AlertDialog.Builder(listview.this);       
            adb.setView(alertDialogView);
            adb.setTitle("Divers");             
            adb.setIcon(android.R.drawable.ic_dialog_alert);            
            adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {

                    EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1);

                }
            });

            adb.setNegativeButton("Annuler", null);
            //on affiche la boite de dialogue
            adb.show();
        }
    });

my listeView load whend getData() is called. 我的listeView加载whend getData()被调用。 (this function call an Asynctask and fill a Hashmap who fill the listView with SimpleAdapter) (此函数调用Asynctask并填充Hashmap,后者使用SimpleAdapter填充listView)

EDIT 3 : 编辑3:

package com.main;


import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.pdf.GetViewPDF;

public class listview extends Activity {

private ListView maListViewPerso;   
ImageButton imgButton;
Boolean isConnected;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color
            .parseColor("#000000")));

    maListViewPerso = (ListView) findViewById(R.id.listviewperso);
    isConnected = isOnline(); 

    getData(isConnected);
    setdivers();




}

public void setdivers(){
     //On instancie notre layout en tant que View
    LayoutInflater factory = LayoutInflater.from(this);
    final View alertDialogView = factory.inflate(R.layout.alert_dialog_divers, null);


    LinearLayout layout = (LinearLayout) findViewById(R.id.test);
    layout.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            AlertDialog.Builder adb = new AlertDialog.Builder(listview.this);       
            adb.setView(alertDialogView);
            adb.setTitle("Divers");             
            adb.setIcon(android.R.drawable.ic_dialog_alert);            
            adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {

                    EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1);

                }
            });

            adb.setNegativeButton("Annuler", null);
            //on affiche la boite de dialogue
            adb.show();
        }
    });
}

public boolean isOnline() {
    ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}



@Override
public void onBackPressed() {
    Toast.makeText(this,"Action Désactivée", Toast.LENGTH_LONG).show();
    //              getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
    //              getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}   

public void GoToPDF(){
    Intent intent = new Intent(this, GetViewPDF.class);
    startActivity(intent);          
}
public void getData(Boolean isConnected){

    if(isConnected){
        asyncTask asynchrone_task_tournee = new asyncTask(listview.this, maListViewPerso);
        asynchrone_task_tournee.execute();  

    }else{
        //J'initialise toutes les variables
        JSONArray jArray = null;
        JSONObject json_data=null;     
        HashMap<String, String> map;
        ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();         
        InputStream is = null;      
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];

        //je récupère le fichier .txt de donner JSON dans le repertoir asset
        AssetManager am = listview.this.getAssets();
        try {
            is = am.open("donnees_tourne.txt");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


        //Je lis le fichier .txt
        try {
                Reader reader = null;
                int n;
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    while ((n = reader.read(buffer)) != -1) 
                    {
                        writer.write(buffer, 0, n);
                    }


                } catch (UnsupportedEncodingException e) {                  
                    e.printStackTrace();
                } catch (IOException e) {                   
                    e.printStackTrace();
                }

            } finally {
                try {
                    is.close();
                } catch (IOException e) {                   
                    e.printStackTrace();
                }
            }
        //recuperation des donnees json dans jsonString
        String jsonString = writer.toString();

        Log.i("JSON_NOT_connected",jsonString);

        //traitement du json dans le tableau json JSONArray
        try {
            jArray = new JSONArray(jsonString);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           

        //Insertion des données json dans un Hashmap
        for(int i=0;i < jArray.length();i++)
        {               
            try {

                json_data = jArray.getJSONObject(i);

                map = new HashMap<String, String>();
                map.put("collector_id", json_data.getString("collector_id"));
                map.put("adress", json_data.getString("adress"));
                map.put("product_code", json_data.getString("product_code"));
                map.put("begin_hour", json_data.getString("begin_hour"));
                map.put("end_hour", json_data.getString("end_hour"));
                map.put("order", json_data.getString("order"));
                map.put("downtime", json_data.getString("downtime"));
                map.put("fax", json_data.getString("fax"));
                map.put("tel", json_data.getString("tel"));
                map.put("miscellaneous", json_data.getString("miscellaneous"));
                map.put("infos", json_data.getString("infos"));

                listItem.add(map);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }     
        }

        SimpleAdapter mSchedule = new SimpleAdapter (listview.this, listItem, R.layout.list_type_tournee,
                //noms donnés en clé dans le Hashmap 
                new String[] {"collector_id", "adress","product_code","begin_hour",
                              "end_hour","order","downtime","fax",
                              "tel","miscellaneous","infos"}, 
                //variables textview dans la list_type_tournee.xml            
                new int[] {R.id.collector_id, R.id.adress,R.id.product_code,R.id.begin_hour,
                           R.id.end_hour,R.id.order,R.id.downtime,R.id.fax,
                           R.id.tel,R.id.miscellaneous,R.id.infos});

        maListViewPerso.setAdapter(mSchedule);



    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.point_menu, menu); 
    menu.getItem(0).setTitle(UtilClass.stringNow());
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_now:
        return true;
    case R.id.menu_login:
        Intent activiteU = new Intent(listview.this, LoginActivity.class);
        startActivity(activiteU);
        return true;
    case R.id.menu_tour_inf:
        Intent activiteT = new Intent(listview.this, TourActivity.class);
        activiteT.putExtra("userid", 10);
        startActivity(activiteT);
        return true;
    case R.id.menu_point_list:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

} }

Am I correct in understand the NullPointerException is on this line? 我是否正确理解此行上的NullPointerException?

LinearLayout linear = (LinearLayout) findViewById(R.id.test);

It looks like whatever findViewById is implicitly being called on something that either doesn't have the layout you posted, or is being called before the layout has been loaded. 看起来好像在没有发布布局的东西上隐式调用了findViewById,或者在布局被加载之前被调用。 Could you post the rest of the code up as well? 您是否还可以发布其余的代码?

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

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