如何创建菜单项列表视图? [英] How create a listview in menu item?

查看:93
本文介绍了如何创建菜单项列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手到Android,我曾尝试使用的的教程。我全成时,我做它作为一个单独的项目。我想impliment同一code在我目前有消息作为一个菜单项目的Unversity的项目。这个菜单项我想显示的新闻标题的列表视图。因此,我曾按照教程,但没有运气。请sombody帮助我。我张贴我的code,以供参考的logcat的。

I am a newbie to android, I have tried to add a simple listview to my app using This tutorial. 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.

MainActivity.java
    包com.example.university;

MainActivity.java package 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 /布局/ news.xml

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>

News1.java
    包com.example.university;

News1.java package 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 /布局/ news1.xml

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

Similarly created News2.java and 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>

下面是我的logcat的:
    [2014-06-19 11点33分四十秒 - 学报霉素
    麻烦写输出:已经prepared
    [2014-06-19十一点33分41秒 - 地塞米松装载机]无法执行DEX:多DEX文件定义Landroid /支持/注释/ AnimRes;
    [2014-06-19十一点33分41秒 - 大学]转换为Dalvik的格式失败:无法执行DEX:多DEX文件定义Landroid /支持/注释/ AnimRes;

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; [2014-08-08 11:33:41 - University] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Landroid/support/annotation/AnimRes;

推荐答案

如果您有库的多个版本,这个问题也发生在包含构建路径身体。除去库路径,干净未使用的库和保存项目,重新启动Eclipse和重新编译它。
右键单击您的项目>进入属性>构建路径
检查Android的支持-V4库

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. Right click on your project > go to properties > Build Path Check android-support-v4 library

这篇关于如何创建菜单项列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆