gpt4 book ai didi

java - 如何使用基于 ListView 选择的字符串数组填充 Activity

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:37 25 4
gpt4 key购买 nike

我目前正在制作显示 ListView 的简单应用程序,点击它后应该会转到另一个显示所选项目详细信息的 Activity 。但是,当我单击时,应用程序崩溃了。我决定使用包含描述字符串数组的资源 XML 文件。

更新

我已经完成更改,现在可以在 ListView 中单击我的项目并且应用程序不会崩溃。但是,字符串数组不会显示在新 Activity 中。

更新2

我做了更多更改,因为我意识到我没有在细节类中设置 toydespos。我现在的问题是 String 只显示了一部分

enter image description here

您会在我的资源文件中看到这不是完整的字符串数组。

这是我目前的错误日志:

06-19 20:51:48.504 22289-22289/? I/zygote: Not late-enabling -Xcheck:jni (already on)
06-19 20:51:48.566 22289-22289/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
06-19 20:51:48.952 22289-22289/com.example.littlegrace.puppydex I/InstantRun: starting instant run server: is main process
06-19 20:51:49.282 22289-22316/com.example.littlegrace.puppydex D/OpenGLRenderer: HWUI GL Pipeline
06-19 20:51:49.346 22289-22305/com.example.littlegrace.puppydex I/zygote: Background concurrent copying GC freed 5689(2MB) AllocSpace objects, 0(0B) LOS objects, 55% free, 1210KB/2MB, paused 8.132ms total 66.730ms
06-19 20:51:49.463 22289-22316/com.example.littlegrace.puppydex I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
06-19 20:51:49.464 22289-22316/com.example.littlegrace.puppydex I/OpenGLRenderer: Initialized EGL, version 1.4
06-19 20:51:49.464 22289-22316/com.example.littlegrace.puppydex D/OpenGLRenderer: Swap behavior 1
06-19 20:51:49.464 22289-22316/com.example.littlegrace.puppydex W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
06-19 20:51:49.464 22289-22316/com.example.littlegrace.puppydex D/OpenGLRenderer: Swap behavior 0
06-19 20:51:49.487 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglCreateContext: 0xe5085180: maj 3 min 1 rcv 4
06-19 20:51:49.525 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:51:49.529 22289-22316/com.example.littlegrace.puppydex E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
06-19 20:51:49.531 22289-22316/com.example.littlegrace.puppydex E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
06-19 20:51:49.618 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:51:50.908 22289-22289/com.example.littlegrace.puppydex V/StudioProfiler: StudioProfilers agent attached.
06-19 20:51:50.972 22289-22357/com.example.littlegrace.puppydex V/StudioProfiler: Acquiring Application for Events
06-19 20:51:51.001 22289-22289/com.example.littlegrace.puppydex V/StudioProfiler: Transformed class: java/net/URL
06-19 20:51:51.002 22289-22289/com.example.littlegrace.puppydex W/zygote: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
06-19 20:51:51.395 22289-22289/com.example.littlegrace.puppydex V/StudioProfiler: Memory control stream started.
06-19 20:51:53.049 22289-22363/com.example.littlegrace.puppydex V/StudioProfiler: Live memory tracking disabled.
06-19 20:51:53.054 22289-22363/com.example.littlegrace.puppydex V/StudioProfiler: Live memory tracking enabled.
06-19 20:51:53.055 22289-22363/com.example.littlegrace.puppydex V/StudioProfiler: JNIEnv not attached
06-19 20:51:53.275 22289-22363/com.example.littlegrace.puppydex V/StudioProfiler: Loaded classes: 5350
06-19 20:51:53.424 22289-22363/com.example.littlegrace.puppydex V/StudioProfiler: Tracking initialization took: 369734510ns
06-19 20:51:54.877 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:51:54.881 22289-22397/com.example.littlegrace.puppydex E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
06-19 20:51:54.882 22289-22397/com.example.littlegrace.puppydex I/chatty: uid=10081(com.example.littlegrace.puppydex) hwuiTask1 identical 3 lines
06-19 20:51:54.882 22289-22397/com.example.littlegrace.puppydex E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
06-19 20:51:54.883 22289-22398/com.example.littlegrace.puppydex E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
06-19 20:51:54.884 22289-22398/com.example.littlegrace.puppydex I/chatty: uid=10081(com.example.littlegrace.puppydex) hwuiTask2 identical 3 lines
06-19 20:51:54.885 22289-22398/com.example.littlegrace.puppydex E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
06-19 20:55:20.245 22289-22298/com.example.littlegrace.puppydex I/zygote: Do partial code cache collection, code=30KB, data=29KB
06-19 20:55:20.248 22289-22298/com.example.littlegrace.puppydex I/zygote: After code cache collection, code=30KB, data=29KB
Increasing code cache capacity to 128KB
06-19 20:55:20.904 22289-22298/com.example.littlegrace.puppydex I/zygote: Do partial code cache collection, code=53KB, data=58KB
06-19 20:55:20.911 22289-22298/com.example.littlegrace.puppydex I/zygote: After code cache collection, code=53KB, data=58KB
Increasing code cache capacity to 256KB
06-19 20:55:20.916 22289-22298/com.example.littlegrace.puppydex I/zygote: JIT allocated 56KB for compiled code of void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int)
06-19 20:55:20.984 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:22.531 22289-22298/com.example.littlegrace.puppydex I/zygote: Do full code cache collection, code=121KB, data=100KB
After code cache collection, code=121KB, data=81KB
06-19 20:55:22.731 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:22.776 22289-22316/com.example.littlegrace.puppydex I/chatty: uid=10081(com.example.littlegrace.puppydex) RenderThread identical 1 line
06-19 20:55:22.929 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:26.223 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:27.105 22289-22298/com.example.littlegrace.puppydex I/zygote: Do partial code cache collection, code=123KB, data=78KB
06-19 20:55:27.113 22289-22298/com.example.littlegrace.puppydex I/zygote: After code cache collection, code=123KB, data=78KB
Increasing code cache capacity to 512KB
06-19 20:55:31.244 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:31.288 22289-22316/com.example.littlegrace.puppydex I/chatty: uid=10081(com.example.littlegrace.puppydex) RenderThread identical 1 line
06-19 20:55:31.415 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:31.450 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:31.490 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:32.670 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:34.093 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:35.808 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:35.847 22289-22316/com.example.littlegrace.puppydex I/chatty: uid=10081(com.example.littlegrace.puppydex) RenderThread identical 1 line
06-19 20:55:35.891 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:35.942 22289-22316/com.example.littlegrace.puppydex D/OpenGLRenderer: endAllActiveAnimators on 0xcc55c100 (ListView) with handle 0xcb0a1760
06-19 20:55:35.967 22289-22298/com.example.littlegrace.puppydex I/zygote: Do full code cache collection, code=248KB, data=173KB
06-19 20:55:35.973 22289-22298/com.example.littlegrace.puppydex I/zygote: After code cache collection, code=248KB, data=143KB
06-19 20:55:37.841 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:39.358 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:39.395 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:39.557 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:39.597 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:39.614 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:40.834 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:43.836 22289-22298/com.example.littlegrace.puppydex I/zygote: Do partial code cache collection, code=248KB, data=151KB
06-19 20:55:43.837 22289-22298/com.example.littlegrace.puppydex I/zygote: After code cache collection, code=248KB, data=151KB
06-19 20:55:43.838 22289-22298/com.example.littlegrace.puppydex I/zygote: Increasing code cache capacity to 1024KB
06-19 20:55:43.991 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:55:44.700 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:56:38.076 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:56:39.322 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:56:39.466 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:56:39.515 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:56:42.759 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:59:09.925 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:59:09.971 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:59:10.000 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:59:10.044 22289-22316/com.example.littlegrace.puppydex I/chatty: uid=10081(com.example.littlegrace.puppydex) RenderThread identical 1 line
06-19 20:59:10.059 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:59:11.364 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 20:59:16.278 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 21:06:10.154 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 21:06:12.067 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 21:06:15.149 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 21:06:15.189 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 21:06:15.223 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 21:06:15.260 22289-22316/com.example.littlegrace.puppydex I/chatty: uid=10081(com.example.littlegrace.puppydex) RenderThread identical 1 line
06-19 21:06:15.293 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)
06-19 21:06:16.597 22289-22316/com.example.littlegrace.puppydex D/EGL_emulation: eglMakeCurrent: 0xe5085180: ver 3 1 (tinfo 0xe5083320)

我的主课:

public class ToyGroup extends AppCompatActivity {

ListView tListView;

int[] images = {
R.drawable.fox,
R.drawable.havan,
R.drawable.malt,
R.drawable.papi,
R.drawable.pom,
R.drawable.poodle,
R.drawable.pug,
R.drawable.shihtzu,
R.drawable.yorkie,
R.drawable.chi};

String[] breed = {
"Fox Terrier",
"Havanese",
"Maltese",
"Papillon",
"Pomeranian",
"Poodle",
"Pug",
"Shih Tzu",
"Yorkshire Terrier",
"Chihuahua"
};

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toylist_view);


tListView = (ListView) findViewById(R.id.ToyList);

CustomAdapter customAdapter = new CustomAdapter();
tListView.setAdapter(customAdapter);

tListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String breeds = breed[position];
final int image = images[position];
String[] toydescription = getResources().getStringArray(R.array.toydescription);
final String toydespos = toydescription[position];

Intent intent = new Intent(getApplicationContext(),ToyDetails.class);
intent.putExtra("toydespos",toydespos);

startActivity(intent);

}
});

}

class CustomAdapter extends BaseAdapter{

@Override
public int getCount() {
return images.length;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.activity_toy_group, null);
ImageView tImageView = (ImageView) view.findViewById(R.id.thumbnail);
TextView tTextView = (TextView) view.findViewById(R.id.rowName);

tImageView.setImageResource(images[position]);
tTextView.setText(breed[position]);

return view;
}
}


}

细节类:

public class ToyDetails extends ToyGroup
{

String description;

public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.toy_detailactivity);

TextView toyDesc = (TextView) findViewById(R.id.toydesc);

Bundle extras = getIntent().getExtras();

if (extras != null)
{
description = extras.getString("toydescpos");
toyDesc.setText(description);
}
}
}

详情页布局 View :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp" >



<TextView
android:id="@+id/toydesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="fill_horizontal"
android:text="TextView" />


</RelativeLayout>

</ScrollView>

数组资源页面(只是一个 fragment ,因为它太大了):

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="toydescription">
<item>Outgoing Toy Fox Terriers have serious FOMO (fear of missing out):
whatever you're doing, they want to be along for the ride.
Literally, in some cases — this breed used to ride in the saddlebags of
hunters, who took them out to catch small prey.
They've also performed in circuses, impressing audiences with their
jumping skills and ability to walk a tightrope.
Sometimes, though, these feisty furballs just want to stay in with you
for movies and cuddles.

Toy Fox Terriers are sometimes called an “AmerToy” breed because — you
guessed it — this toy breed originated in the United States.
Those huge bat ears and naturally bobbed tail give the Toy Fox Terrier a
distinctive look.
</item>
</string-array>
<resources>

最佳答案

数组索引从零开始。当位置为零时,以下行的索引变为 -1:

String breeds = breed[position-1]; // breed[-1]
final int image = images[position-1]; // images[-1]
final String toydespos = toydescription[position-1]; //toydescription[-1]

抛出 ArrayIndexOutOfBoundsException。删除 -1 或根据您的要求修改代码。

编辑:

使用以下更改:

在您的 CustomAdapter 中:

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

在您的 onItemClick 中:

String breeds = breed[position];
final int image = images[position];
final String toydespos = toydescription[position]

关于java - 如何使用基于 ListView 选择的字符串数组填充 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50923241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com