gpt4 book ai didi

java - 如何在菜单项中创建 ListView ?

转载 作者:行者123 更新时间:2023-11-30 02:45:03 28 4
gpt4 key购买 nike

我是 android 的新手,我尝试使用 This 向我的应用程序添加一个简单的 ListView 。教程。当我将其作为一个单独的项目进行时,我很成功。我想在我当前的以新闻作为菜单项的大学项目中使用相同的代码。在此菜单项中,我想显示新闻标题的 ListView 。我按照教程进行了相应的工作,但没有运气。请有人帮助我。我将我的代码发布到 Logcat 以供引用。

主要 Activity .java 包 com.example.university;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.home:
Intent h = new Intent(getApplicationContext(), Home.class);
startActivity(h);
return true;
case R.id.about:
Intent a = new Intent(getApplicationContext(), About.class);
startActivity(a);
return true;
case R.id.news:
Intent n = new Intent(getApplicationContext(), News.class);
startActivity(n);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

res/layout/news.xml

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

<string-array android:name="adobe_products">

<item android:layout_width="wrap_content">Lorem Lipsum1</item>
<item>Lorem Lipsum2</item>
</string-array>
</ListView>

新闻1.java 包 com.example.university;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class News1 extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.news1);

TextView txtProduct = (TextView) findViewById(R.id.news1);

Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
txtProduct.setText(product);

}
}

res/layout/news1.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="fill_parent"
android:layout_height="fill_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" >

<TextView
android:id="@+id/news1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Lorem Lipsum1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16dp" />
</RelativeLayout>

同样创建News2.java和news2.xml

list .xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.university"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are required for location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.university.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".About"
android:label="@string/title_activity_about" >
</activity>
<activity
android:name=".Home"
android:label="@string/title_activity_home" >
</activity>
<activity
android:name=".Contact"
android:label="@string/title_activity_contact" >
</activity>



<activity
android:name=".News1"
android:label="Lorem Lipsum1" >
</activity>

<activity
android:name=".News2"
android:label="Lorem Lipsum2">
</activity>

</application>

</manifest>

这是我的 Logcat: [2014-08-08 11:33:40 - 大学] Dx 写输出有问题:已经准备好了 [2014-08-08 11:33:41 - Dex Loader] 无法执行 dex:多个 dex 文件定义 Landroid/support/annotation/AnimRes; [2014-08-08 11:33:41 - 大学] 转换为 Dalvik 格式失败:无法执行 dex:多个 dex 文件定义 Landroid/support/annotation/AnimRes;

最佳答案

如果您的构建路径中包含多个版本的库,也会发生此问题。从 libs 路径中删除未使用的库,清理并保存项目,重新启动 Eclipse 并再次重新编译。右键单击您的项目>转到属性>构建路径检查 android-support-v4 库

关于java - 如何在菜单项中创建 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25197345/

28 4 0