0

私が設計しているアプリは、ユーザーが EditText に入力し、ボタンをクリックしてテキストを送信することによってアイテムが作成されるメニューに基づいています。また、ユーザーが作成したメニュー項目をクリックすることに基づいて、新しいアクティビティに移動できるようにしたいと考えています。これまでの私のJavaコードは次のとおりです。

    package com.example.doodlestudy;

    import java.io.FileOutputStream;
    import java.util.ArrayList;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;

    public class MainActivity extends Activity {

private OnClickListener AddToString;

Button Badd;
EditText ETadd;
ListView LVmain;

String filename = "Main_Activity";
String input;
FileOutputStream outputStream;

ArrayAdapter<String> m_adapter;
ArrayList<String> m_listItems = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Badd = (Button)findViewById(R.id.Badd);
    ETadd = (EditText)findViewById(R.id.ETadd);
    LVmain = (ListView)findViewById(R.id.LVmain);

    m_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_listItems);
    LVmain.setAdapter(m_adapter);

    Badd.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            input = ETadd.getText().toString();

            m_listItems.add(new String(input));
            m_adapter.notifyDataSetChanged();   
        }
    });

    try {
        outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
        outputStream.write(input.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
    }

ここに私のxmlコードがあります:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/ETadd"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Click to Edit..." >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/Badd"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Add" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ListView
        android:id="@+id/LVmain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

これが私のマニフェストです:

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.doodlestudy.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>
</application>

新しいアクティビティへの移行を許可するコードを取得できないため、マニフェストにはまだ触れていません。ユーザーが作成した文字列を EditText に格納するには、別の方法が必要かもしれないと考えていますが、肯定的ではありません。

4

1 に答える 1

0

あなたが望むのは、次の に移動するボタンのように、 にClickListenerを設定することです。これは次のようになります。ListViewActivity

        LVmain.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
//              intent.putExtra(name, value);
//              take the time to put extra information that you need for the next activity here
                startActivity(intent);
            }
        });

次に、マニフェストで新しいアクティビティも認識させる必要があります。

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.doodlestudy.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="SecondActivity"></activity> 
        // add your activities to your manifest
    </application>
于 2012-12-30T07:08:48.603 に答える