2016-08-02 85 views
0

当我点击项目0(培根),然后应用程序关闭。如何更改Android ListView中的文本?

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     ListView buckyListView = (ListView) findViewById(R.id.buckyListView); 
     String[] foods = {"Bacon", "Tuna", "Ham", "Bucky", "Tommy", "Sunny", "Mommy", "Burger", "Pizza", "HotDog", "Muniraka", "Mouse", "Keyboard", "Tenda", "Iron", "Public", "Life", "Good", "Keyboard", "PenDrive", "RedFort"}; 

     ListAdapter buckysAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, foods); 

     buckyListView.setAdapter(buckysAdapter); 

     buckyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       switch(position) { 
        case 0: 
         TextView tv = (TextView) view.findViewById(R.id.textView); 
         tv.setText("TextView was Clicked"); 
         break; 
        default: 
         break; 
       } 
      } 
     }); 
    } 
} 

如何在点击项目时更改文本?我使用最新版本的Android工作室,我必须建立在API项目15

08-03 10:35:46.037 707-997/? E/MountService: Failed to read response to volume shared /storage/emulated/0 ums 
08-03 10:35:46.203 707-3517/? E/MountService: Failed to read response to volume shared /storage/emulated/0 ums 
08-03 10:35:46.339 707-997/? E/MountService: mount service regis[email protected]42f00498 
08-03 10:35:46.339 707-853/? E/MountService: Failed to read response to volume shared /storage/emulated/0 ums 
08-03 10:37:34.868 707-717/? E/Sensors: new acc setDelay handle(0),ns(20000000) err! go to hwmsen 
08-03 10:38:37.970 7618-7618/? E/dalvikvm: /system/framework/mediatek-op.jar odex has stale dependencies 
08-03 10:38:38.000 7618-7618/? E/dalvikvm: /system/framework/am.jar odex has stale dependencies 
08-03 10:38:38.004 7618-7618/? E/cutils-trace: Error opening trace file: No such file or directory (2) 
08-03 10:38:38.174 7618-7618/? E/memtrack: Couldn't load memtrack module (No such file or directory) 
08-03 10:38:38.174 7618-7618/? E/android.os.Debug: failed to load memtrack module: -2 
08-03 10:38:38.411 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:38.412 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:38.412 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:38.412 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:38.414 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:38.416 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:38.416 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:38.418 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:38.423 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:38.425 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:38.427 7634-7634/com.example.android.bucky45 E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.android.bucky45.MainActivity.access$super 
08-03 10:38:54.435 7634-7634/com.example.android.bucky45 E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.example.android.bucky45, PID: 7634 
                     java.lang.NullPointerException 
                      at com.example.android.bucky45.MainActivity$1.onItemClick(MainActivity.java:37) 
                      at android.widget.AdapterView.performItemClick(AdapterView.java:299) 
                      at android.widget.AbsListView.performItemClick(AbsListView.java:1152) 
                      at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014) 
                      at android.widget.AbsListView$3.run(AbsListView.java:3865) 
                      at android.os.Handler.handleCallback(Handler.java:808) 
                      at android.os.Handler.dispatchMessage(Handler.java:103) 
                      at android.os.Looper.loop(Looper.java:193) 
                      at android.app.ActivityThread.main(ActivityThread.java:5345) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:515) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
                      at dalvik.system.NativeStart.main(Native Method) 
08-03 10:41:35.160 707-717/? E/Sensors: new acc setDelay handle(0),ns(20000000) err! go to hwmsen 
08-03 10:41:57.947 7720-7720/? E/lcdc_cap: [DDMSCap]pmem_alloc size = 0x00384020, addr = 0xb6ad8008 
08-03 10:43:28.480 7729-7729/? E/dalvikvm: Could not find class 'com.google.android.apps.gsa.assist.GsaVoiceInteractionSession', referenced from method com.google.android.apps.gsa.assist.GsaVoiceInteractionSession_MembersInjector.au 
08-03 10:43:28.499 7729-7729/? E/dalvikvm: Could not find class 'com.google.android.apps.gsa.tasks.VelvetBackgroundTasksJobService', referenced from method com.google.android.apps.gsa.tasks.l.au 
08-03 10:43:28.522 945-1860/? E/ctxmgr: [AppIntervalImpl]closeInterval: ongoing 
08-03 10:43:28.726 7729-7750/? E/dalvikvm: Could not find class 'android.content.pm.LauncherApps', referenced from method com.google.android.apps.gsa.search.shared.multiuser.l.connect 
08-03 10:43:29.234 7729-7729/? E/CardSyncManagerImpl: onConnectionFailed: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null} 
08-03 10:43:29.234 7729-7760/? E/CardSyncManagerImpl: Failed to connect to GoogleApiClient: null 
08-03 10:43:29.416 7729-7763/? E/dalvikvm: Could not find class 'android.content.pm.LauncherApps', referenced from method com.google.android.apps.gsa.shared.s.a.a.n 
08-03 10:43:29.587 7729-7729/? E/dalvikvm: Could not find class 'org.chromium.net.s', referenced from method org.chromium.net.NetworkChangeNotifierAutoDetect.<init> 
08-03 10:43:30.616 7729-7764/? E/GeofenceHelper: Failed: remove geofences by PendingIntent 

这种布局XML

<?xml version="1.0" encoding="utf-8"?> 
<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="com.example.android.bucky45.MainActivity"> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/buckyListView" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="147dp"></ListView> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Large Text" 
    android:id="@+id/textView" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="48dp" /> 

检查这个代码,请更正此这是真的非常重要

+0

后一看logcat的输出吧。 – adhirajsinghchauhan

+0

你肯定可以找到的TextView和电视不为空的位置:TextView的电视=(TextView的)view.findViewById(R.id.textView); –

+0

请使用'ArrayAdapter '所以当然你需要使用'TextView的电视=(TextView的)view.findViewById(android.R.id.textView1)添加布局XML – xAqweRx

回答

0

我只是实现一个自定义适配器

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     ListView buckyListView = (ListView) findViewById(R.id.buckyListView); 
     String[] foods = {"Bacon", "Tuna", "Ham", "Bucky", "Tommy", "Sunny", "Mommy", "Burger", "Pizza", "HotDog", "Muniraka", "Mouse", "Keyboard", "Tenda", "Iron", "Public", "Life", "Good", "Keyboard", "PenDrive", "RedFort"}; 
     CustomAdapter buckysAdapter = new CustomAdapter(this, android.R.layout.simple_list_item_1, Arrays.asList(foods)); 

     buckyListView.setAdapter(buckysAdapter); 

     buckyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       CustomAdapter adapter = (CustomAdapter) parent.getAdapter(); 
       adapter.updateItem(position, "New String"); 
      } 
     }); 

    } 

    private class CustomAdapter extends ArrayAdapter<String> { 
     List<String> mStrings; 
     public CustomAdapter(Context context, int resource, List<String> strings) { 
      super(context, resource); 
      mStrings = strings; 
     } 

     @Override 
     public int getCount() { 
      return mStrings.size(); 
     } 

     @Override 
     public String getItem(int position) { 
      return mStrings.get(position); 
     } 

     public void updateItem(int index, String string) { 
      mStrings.set(index, string); 
      //To redraw we must notify the list the underlying data has changed 
      notifyDataSetChanged(); 
     } 
    } 
0

请把这个片段:)

public class MainActivity extends Activity { 

TextView tv; 
ListView buckyListView; 
ArrayAdapter<String> buckysAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

    tv = (TextView) findViewById(R.id.textView); 
    buckyListView = (ListView) findViewById(R.id.buckyListView); 

    String[] foods = {"Bacon", "Tuna", "Ham", "Bucky", "Tommy", "Sunny", "Mommy", "Burger", "Pizza", "HotDog", "Muniraka", "Mouse", "Keyboard", "Tenda", "Iron", "Public", "Life", "Good", "Keyboard", "PenDrive", "RedFort"}; 
    buckysAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, foods); 
    buckyListView.setAdapter(buckysAdapter); 

    buckyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      switch(position){ 
       case 0: 
        tv.setText("TextView was Clicked"); 
        break; 
       default: 

        break; 
      } 

     } 
    }); 
} 
} 
+1

这是行不通的 – prashant

+0

请发表您的logcat :) – alway5dotcom

+0

结帐现在我已经发布 – prashant