简体   繁体   中英

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. 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 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/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 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/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>

Similarly created News2.java and news2.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; [2014-08-08 11:33:41 - University] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define 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. Right click on your project > go to properties > Build Path Check android-support-v4 library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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