如何在新窗口中打开ListView项目的详细信息?

时间:2016-05-19 23:21:30

标签: android listview

我是android开发新手并且正在努力解决这个问题。基本上我有一个数据库支持的字典应用程序。现在,当用户点击某个单词时,该定义会出现在祝酒词上。我想在新窗口中打开单词+定义:

以下是我的MainActivity中的相关部分:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDB = new DictionaryDatabase(this);
        mEditTextWord = (EditText)findViewById(R.id.editTextWord);
        mEditTextDefinition = (EditText)findViewById(R.id.editTextDefinition);
        Button buttonAddUpdate = (Button)findViewById(R.id.buttonAddUpdate);

        buttonAddUpdate.setOnClickListener(
                new  View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                           saveRecord();
                    }
                });

        mListView = (ListView)findViewById(R.id.listView);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View
                     view, int position, long id) {
                 Toast.makeText(MainActivity.this,
                         mDB.getDefinition(id), Toast.LENGTH_SHORT).show();
             }
        });
        mListView.setOnItemLongClickListener(new  AdapterView.OnItemLongClickListener() {
             @Override
             public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                 Toast.makeText(MainActivity.this,
                         "Records deleted = " + mDB.deleteRecord(id),
                         Toast.LENGTH_SHORT).show();
                 updateWordList();
                 return true;
             }
        });
        updateWordList();

    }

ListView布局:

<?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="match_parent"
    android:layout_height="match_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"
    tools:context="com.naqishop.sqlitedatabase.MainActivity">


        <EditText
            android:id="@+id/editTextWord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:hint="Word"/>
        <EditText
            android:id="@+id/editTextDefinition"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editTextWord"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:hint="Definition"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save"
            android:id="@+id/buttonAddUpdate"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true" />
        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_below="@+id/editTextDefinition"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true" />


    </RelativeLayout>

我试图在SO上使用类似问题的答案来提出解决方案,但是没有成功。

更新:这是我的DictionaryDatabase.java

DictionaryDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_DICTIONARY +
            "(_id integer PRIMARY KEY," +
            FIELD_WORD + " TEXT, " +
            FIELD_DEFINITION + " TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //Handle database upgrade as needed
}

public void saveRecord(String word, String definition) {
    long id = findWordID(word);
    if (id>0) {
        updateRecord(id, word,definition);
    } else {
        addRecord(word,definition);
    }

    }

public long addRecord(String word, String definition) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(FIELD_WORD, word);
    values.put(FIELD_DEFINITION, definition);
    return db.insert(TABLE_DICTIONARY, null, values);
}
public int updateRecord(long id, String word, String definition) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("_id", id);
    values.put(FIELD_WORD, word);
    values.put(FIELD_DEFINITION, definition);
    return db.update(TABLE_DICTIONARY, values, "_id = ?",
            new String[]{String.valueOf(id)});
}
public int deleteRecord(long id) {
    SQLiteDatabase db = getWritableDatabase();
    return db.delete(TABLE_DICTIONARY, "_id = ?", new
            String[]{String.valueOf(id)});
}

public long findWordID(String word) {
    long returnVal = -1;
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery(
            "SELECT _id FROM " + TABLE_DICTIONARY +
                    " WHERE " + FIELD_WORD + " = ?", new String[]{word});
    Log.i("findWordID","getCount()="+cursor.getCount());
    if (cursor.getCount() == 1) {
        cursor.moveToFirst();
        returnVal = cursor.getInt(0);
    }
    return returnVal;
}

public String getDefinition(long id) {
    String returnVal = "";
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery(
            "SELECT definition FROM " + TABLE_DICTIONARY +
                    " WHERE _id = ?", new String[]{String.valueOf(id)});
    if (cursor.getCount() == 1) {
        cursor.moveToFirst();
        returnVal = cursor.getString(0);
    }
    return returnVal;
}
public Cursor getWordList() {
    SQLiteDatabase db = getReadableDatabase();
    String query = "SELECT _id, " + FIELD_WORD +
            " FROM " + TABLE_DICTIONARY + " ORDER BY " + FIELD_WORD +
            " ASC";
    return db.rawQuery(query, null);
}

2 个答案:

答案 0 :(得分:1)

你可以开始这样的活动:

Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

您可能还希望将定义发送到新启动的活动,如下所示:

public class MainActivity extends AppCompatActivity {
    public static String EXTRA_DEFINITION = "definition";

    ...

    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra(EXTRA_DEFINITION, mDB.getDefinition(id));
    startActivity(intent);

这样您就可以在新活动的onCreate方法中执行此操作:

Intent intent = new Intent(); //get the intent that launched activity
String definition = intent.getStringExtra(MainActivity.EXTRA_DEFINITION);
//display the definition

有关详细信息,请参阅this

答案 1 :(得分:1)

您可以在DetailActivity.java中新建manifest。 单击单词时,将意图移至详细活动。

Intent intent = new Intent(this,DetailActivity.class);
startActivity(intent);

您可以使用putExtra

传递具有此意图的信息
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View
                     view, int position, long id) {
                   Intent intent = new Intent(view.getContext(),DetailActivity.class);
                   intent.putExtra("key",mDB.getDefinition(id)+""); // your value that ypu want to send
                   startActivity(intent);
             }
        });

DetailActivity

中获取此值
try{
   String value = getIntent().getStringExtra("key");
}
catch(Exception e){
   e.printStackTrace();
}