android - OnListItemClick 在 Activity 中

标签 android database list android-activity

在我的 Android 应用程序中,我有一个国家/地区列表,我可以在应用程序运行时随时添加新条目。该列表保存在数据库中。我已经尝试了几种不同的方法来尝试实现 OnListItemClick 但我无法让它工作。这是我的类(class),其中包含我的列表:

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity
{
    private DBManager db;
    Cursor cursor;
    Button goEdit;
    ListView listContent;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.initial_activity);

        listContent = (ListView)findViewById(R.id.list);
        goEdit = (Button)findViewById(R.id.goedit);

        //Open database
        db = new DBManager(this);
        db.openToRead();

        cursor = db.queueAll();

        String[] from = new String[]{DBManager.KEY_ID, DBManager.KEY_YEAR, DBManager.KEY_CONTENT, DBManager.KEY_DESC};
        int[] to = new int[]{R.id.editcountry, R.id.yeartext, R.id.countrytext};

        SimpleCursorAdapter cursorAdapter =  new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

        listContent.setAdapter(cursorAdapter);

        //go to add/delete screen
        goEdit = (Button)findViewById(R.id.goedit);
        goEdit.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                Log.i("Test", "Now moving to the edit activity");
                Intent intent = new Intent(MainActivity.this, EditList.class);
                startActivity(intent);
            }
        });
    }

    //life cycles
    protected void onPause()
    {
        super.onPause();
        db.close();
    }

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        db.close();
        finish();
    }
}

这是我的类(class),我可以在其中选择将新国家/地区输入列表:

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class EditList extends Activity
{
    private DBManager db;
    Cursor cursor;
    EditText editCountry, editYear, editDesc;
    Button add, delete, back;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editCountry = (EditText)findViewById(R.id.editcountry);
        editYear = (EditText)findViewById(R.id.edityear);
        editDesc = (EditText)findViewById(R.id.editdesc);
        add = (Button)findViewById(R.id.add);
        delete = (Button)findViewById(R.id.delete);
        back = (Button)findViewById(R.id.backmain);

        //Open database and fill it with content, then close it
        db = new DBManager(this);
        db.openToWrite();

        cursor = db.queueAll();

        add.setOnClickListener(buttonAddOnClickListener);
        delete.setOnClickListener(buttonDeleteAllOnClickListener);

        //handle switching back to main screen
        Log.i("Test", "back to main");
        back = (Button)findViewById(R.id.backmain);
        back.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                db.close();

                //can't use "finish()" because then the list won't refresh with the new data
                Log.i("Test", "Going back to the main screen");
                Intent intent = new Intent(EditList.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    //insert new country button
    Button.OnClickListener buttonAddOnClickListener = new Button.OnClickListener()
    {  
        @Override
        public void onClick(View arg0) 
        {
            Toast.makeText(getApplicationContext(), "Added!", Toast.LENGTH_LONG).show();

            int year = Integer.parseInt(editYear.getText().toString());
            String country = editCountry.getText().toString();
            String desc = editDesc.getText().toString();

            db.insert(year, country, desc);

            updateList();

            //clear text fields after use
            editYear.setText(null);
            editCountry.setText(null);
            editDesc.setText(null);
        }
    };

    //delete all button
    Button.OnClickListener buttonDeleteAllOnClickListener = new Button.OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
            Toast.makeText(getApplicationContext(), "Your list has been deleted!", Toast.LENGTH_LONG).show();
            db.deleteAll();
            updateList();
        }
    };

    private void updateList()
    {
        cursor.requery();
    }

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        db.close();
        finish();
    }
}

我之前在一个类中实现了 OnListItemClick,其中数据静态保存在一个数组中。该类还扩展了 ListActivity,而这个类没有。

最佳答案

ListActivity 和标准 Activity 的区别在于 ListActivity 为您处理了 OnItemClickListener 接口(interface)映射,并且只是提供了一个额外的回调方法。如果没有 ListActivity,您需要自己添加管道;即

public class MainActivity extends Activity implements AdapterView.OnItemClickListener
{
    ...
    ListView listContent;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        ...

        listContent.setOnItemClickListener(this);

        ...
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        //Callback logic here for clicked items
    }

    ...
}

关于android - OnListItemClick 在 Activity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15713584/

相关文章:

java - 错误 : syntax error at or near "$1" postgresql query with params

python - 在Python中创建一个按字符分隔的列表

Android Spotify SDK 登录错误意外 token

ANDROID STUDIO 3.0 升级: Error:Could not resolve all files for configuration ':app:xxxxxxxDebugCompileClasspath'

android - 删除查询是否修改了 Android 选择查询游标?

javascript - 我的循环运行了两次,没有给出正确的答案任何解决方案

r - 将列表中的数据框导出到带有命名工作表的 xlsx

java - 语音识别,有没有办法可以通过正则表达式确定用户说的话?

php - 在 Javascript 中循环遍历 php

database - 我们可以存档 AWS QLDB 数据吗,因为每个企业对可以存储的历史记录量都有限制