2016-08-02 13 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; 
       } 
      } 
     }); 
    } 
} 

アイテムをクリックするとテキストを変更するにはどうすればよいですか?このコードをチェックして、それは本当にこれを修正してください。このレイアウト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" /> 

を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 

を私はアンドロイドスタジオの最新バージョンを使用していると私はAPIでこのプロジェクトをビルドする必要があります非常に重要

+0

ポストをlogcat出力してください。 – adhirajsinghchauhan

+0

TextViewが見つかり、TVがここでNULLでないことが確かですか?TextView tv =(TextView)view.findViewById(R.id.textView); –

+0

'TextAdapter 'を使ってレイアウト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

checkout now投稿しました – prashant

関連する問題