1

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

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();
    }
}

这是我的课程,我可以选择在列表中输入新的国家:

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,而这一类没有。

4

2 回答 2

3

The difference between ListActivity and a standard Activity is that the ListActivity handled the OnItemClickListener interface mapping for you, and just provided an extra callback method. Without ListActivity, you'll need to add that plumbing yourself; i.e.

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
    }

    ...
}
于 2013-03-30T01:10:19.733 回答
0

在您的 MainActivity 添加

@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
        super.onListItemClick(l, v, position, id);

        // a list item is click do whatever you want
}
于 2013-03-30T00:46:12.563 回答