0

嗨,我想用 ListView 构建一个 android 应用程序,如果我在这个 ListView 中选择一个值,我想用这个值启动另一个活动。问题是我不知道如何单击 ListView 中的值而其他活动获取信息。这是我的代码:

我的第一个活动:

package de.android.shilfe;

import de.android.shilfe.DatenbankManager;
import de.android.shilfe.R;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class MainActivity extends ListActivity {

    private SQLiteDatabase mDatenbank;
    private DatenbankManager mHelper;

    private static final String KLASSEN_SELECT_ROW = 
            "SELECT _id, name FROM Stundenplan";

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

        mHelper.STUNDENPLAN_CREATE = "CREATE TABLE Stundenplan(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, "+
                "name TEXT NOT NULL)";

       mHelper = new DatenbankManager(this);

       //what I must do here ??? :(
    }

    @Override
    protected void onPause() {
        super.onPause();

        mDatenbank.close();

        Toast.makeText(this, 
                getResources().getString(R.string.db_close),
                Toast.LENGTH_SHORT).show(); 
    }

    @Override
    protected void onResume() {     
        super.onResume();

        mDatenbank = mHelper.getReadableDatabase(); //Datenbank öffnen

        Toast.makeText(this, 
                getResources().getString(R.string.db_open),
                Toast.LENGTH_SHORT).show(); 

        ladeDaten();
    }

    private void ladeDaten() {
        Cursor KlassenCursor = mDatenbank.rawQuery(KLASSEN_SELECT_ROW, null); // Gibt ein Index zurück
        startManagingCursor(KlassenCursor); //Durchläuft diesen

        android.widget.SimpleCursorAdapter KlassenAdapter = new android.widget.SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, 
                KlassenCursor, 
                new String[] {"name"},
                new int[] {
                android.R.id.text1
                });

        setListAdapter(KlassenAdapter);


    }

    public void onButtonClick(View view){

        EditText et =(EditText) findViewById(R.id.editText1);

        ContentValues werte = new ContentValues();
        werte.put("name",et.getText().toString());

        mDatenbank.insert("stundenplan", null, werte);

        ladeDaten();
        et.setText("");
    }

}

我的第一个活动布局 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="5">

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/create_defaulttext">

            <requestFocus />
        </EditText>

        <Button
        android:id="@+id/sf_daten_datenbanken_einfuegen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/sf_daten_datenbanken_einfuegen"
        android:onClick="onButtonClick" />

    </LinearLayout>

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">

        <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    </ListView>

        <TextView
        android:id="@+id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tx_daten_datenbank_leer"/>

    </LinearLayout>

</LinearLayout>

我使用以下代码在其他应用程序中尝试它:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List valueList = new ArrayList<String>(); 

        for(int i=0;i<10;i++)
        {
            valueList.add("value" + i); 
        }

        ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,valueList); 

        final ListView lv = (ListView)findViewById(R.id.listview1);
        lv.setAdapter(adapter); 

        lv.setOnItemClickListener(new OnItemClickListener() { 

             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){  
                 Intent intent = new Intent();
                 intent.setClassName(getPackageName(), getPackageName() + ".Show_Activity"); 
                 intent.putExtra("selected",lv.getAdapter().getItem(arg2).toString()); 
                 startActivity(intent); 

             }

         });




    }

但在另一个应用程序中,我通过 EditText 控件填充 ListView

在此处输入图像描述

我需要帮助 :(

4

4 回答 4

1
lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, int position, long id){  
         Intent intent = new Intent(context, Show_Activity.class);
         intent.putExtra("selected", (String) lv.getItemAtPosition(position)); 
         startActivity(intent); 
     }
});

您已经将值传递给另一个活动putExtra

于 2013-01-07T14:38:30.973 回答
0

Hey You question is not complete but I am giving this answer as per what I understand. In the onListItemClick() method last parameter of long datatype is id of a row selected. in your case, it will _id of table. If you have 10 rows in your table with id 10 to 20 and displayed the same in listview. Now if you click on 5 item then its id will be 15 which was stored in database. Pass this value to next intent and fetch the details of that row and display in the next activity.

于 2013-01-07T14:29:59.040 回答
0

When you use a ListActivity so you can use onListItemClick method.This method will be called when an item in the list is selected.

于 2013-01-07T15:09:24.860 回答
-1

You can to create a custom list adapter and handle the onClick event in the getView method of the adapter. The item will be available in this method.

public class AccountListAdapter extends ArrayAdapter<Account> {

    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        final Account item = this.getItem(position);

        // item = the particular list item you clicked      

           // layoutItem = layout view passed into the adapter
           layoutItem.setOnClickListener(new View.OnClickListener() {           
               @Override
               public void onClick(View v) {
                   callActivity(item);
               }
            });

    }




}

While handling the event, create an intent with extras and pass the value(s) you desire via the extras. You can use a simplelistadapter, but this way gives you considerably more control and power!

于 2013-01-07T14:29:29.097 回答