android中listview的问题 [英] Problem with listview in android
问题描述
先生,我创建了一个带导航抽屉的Android应用程序。当用户点击应用程序时,应该去进行新的活动。为此我编写了这个
if (id == R.id.maths){
Intent intent = new Intent(Main。 this ,Mathematics。 class );
startActivity(intent);
}
并成功转移到新活动。我想在我的新活动中添加一个列表视图,我为此编码,为空白活动以显示列表视图
public class 数学 extends AppCompatActivity {
@ Override
protected void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.content_mathematics);
String [] classes = { A1 , A2, A3, A4, A5, < span class =code-string> A6, A7, A8};
ArrayAdapter adapter = new ArrayAdapter< String>( this ,R.layout.content_mathematics,classes );
ListView listView =(ListView)findViewById(R.id.mathList);
listView.setAdapter(adapter);
}
}
我还在xml文件中添加了列表视图,
< ListView
android:id = @ + id / mathList
android:layout_width = match_parent
android :layout_height = wrap_content>
< / ListView>
我已经使用零警告和零错误构建了应用程序但是当我运行应用程序应用程序崩溃
请帮助我这个先生
谢谢你的时间先生
我尝试了什么:
我尝试使用我的应用程序运行模拟器和模拟器显示黑屏几个小时。我参考了教程点并修改了代码,但仍然是相同的情况。我无法理解,因为应用程序编译为零错误。
最后使它工作。
字符串 [] topics = new String [] { A1, A2, A3, A4, A5, A6 , A7, A8};
ListAdapter适应= <跨度类= 代码关键字>新跨度> ArrayAdapter<串GT;(<跨度类= 代码关键字>此跨度>,android.R.layout.simple_list_item_1 ,话题);
ListView视图=(ListView)findViewById(R.id.maths);
view.setAdapter(adapt);
< / string>
修改:
以另一种方式声明字符串(我知道两者都是相同的)
正如Richard MacCutchan所建议我从Android文档中推荐了当前的教程并将此行修改为此,
ArrayAdapter adapter = new ArrayAdapter< string>(this,R.layout.content_mathematics,classes);< / string>
要
ListAdapter适应= <跨度类= 代码关键字 >新跨度> ArrayAdapter<串GT;(<跨度类= 代码关键字>此跨度>,android.R.layout.simple_list_item_1,主题);< / string>
我不需要此列表视图中的图像所以我将布局设置为基本(simple_list_item_1管理)
Sir, I have created an android application with navigation drawer. When the user clicks the application should go for the new activity.For that I have coded this
if (id == R.id.maths) {
Intent intent = new Intent(Main.this, Mathematics.class);
startActivity(intent);
}
and it shifts to the new activity successfully. I want to add a list view in my new activity for that I have coded like this, for the blank activity inorder to show the list view
public class Mathematics extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_mathematics);
String[] classes = {"A1","A2","A3","A4","A5","A6","A7","A8"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.content_mathematics, classes);
ListView listView = (ListView) findViewById(R.id.mathList);
listView.setAdapter(adapter);
}
}
I have also added list view in the xml file,
<ListView
android:id="@+id/mathList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
I have build-ed the application with zero warnings and zero errors but when I run the application the app crashes
Kindly help me with this sir
Thank you for your time sir
What I have tried:
I tried running my application using an emulator and the emulator shows black screen for hours. I referred tutorials point and modified the code but still the same case. I couldn't figure out since the application compiled with zero errors.
Finally made it to work.
String[] topics = new String[]{"A1","A2","A3","A4","A5","A6","A7","A8"}; ListAdapter adapt = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,topics); ListView view = (ListView)findViewById(R.id.maths); view.setAdapter(adapt); </string>
Modifications:
Declared String in alternate way(I know both are same)
As suggested by Richard MacCutchan I have Referred current tutorial from Android documentation and modified this line to this,
ArrayAdapter adapter = new ArrayAdapter<string>(this, R.layout.content_mathematics, classes);</string>
To
ListAdapter adapt = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,topics);</string>
I don't need images in this list view so I've set the layout to its basic (simple_list_item_1)
这篇关于android中listview的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!