简体   繁体   English

如何在菜单项中创建列表视图?

[英]How create a listview in menu item?

I am a newbie to android, I have tried to add a simple listview to my app using This tutorial. 我是android的新手,因此我尝试使用教程向我的应用添加简单的listview。 I am successfull when I am doing it as a seperate project. 作为一个单独的项目进行时,我会很成功。 I want to impliment the same code in my current project of an Unversity having News as a menu Item. 我想在我当前以“新闻”作为菜单项的大学项目中隐含相同的代码。 In this menu Item I want to display the listview of the news titles. 在此菜单项中,我要显示新闻标题的列表视图。 I worked accordingly as per the tutorial , but no luck. 我按照教程进行了相应的工作,但是没有运气。 Please sombody help me. 请有人帮助我。 I am posting my code an Logcat for reference. 我将代码发布到Logcat中以供参考。

MainActivity.java package com.example.university; MainActivity.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 RES /布局/ 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>

News1.java package com.example.university; News1.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 RES /布局/ 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>

Similarly created News2.java and news2.xml 同样创建的News2.java和news2.xml

Manifest.xml 的Manifest.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>

Here is my Logcat: [2014-08-08 11:33:40 - University] Dx trouble writing output: already prepared [2014-08-08 11:33:41 - Dex Loader] Unable to execute dex: Multiple dex files define Landroid/support/annotation/AnimRes; 这是我的Logcat:[2014-08-08 11:33:40-大学] Dx麻烦写输出:已经准备好[2014-08-08 11:33:41-Dex Loader]无法执行dex:定义了多个dex文件Landroid /支撑/注解/ AnimRes; [2014-08-08 11:33:41 - University] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Landroid/support/annotation/AnimRes; [2014-08-08 11:33:41-大学]转换为Dalvik格式失败:无法执行dex:多个dex文件定义了Landroid / support / annotation / AnimRes;

This problem also happened if you have multiple version of libraries included in your build path physically. 如果您的构建路径中实际上包含多个版本的库,也会发生此问题。 Removing the unused libraries from libs path, clean and save the project, restarts Eclipse and recompile it again. 从libs路径中删除未使用的库,清理并保存项目,重新启动Eclipse并再次重新编译它。 Right click on your project > go to properties > Build Path Check android-support-v4 library 右键单击您的项目>转到属性>构建路径检查android-support-v4库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM