2

我已将 checkBoxView 添加到我的 ListView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip" >

    <TextView
        android:id="@+id/textViewWord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/fstRow"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewTranslate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewWord"
        android:layout_below="@+id/textViewWord"
        android:textColor="#ff1009"
        android:text="@string/scndRow"        
        android:textAppearance="?android:attr/textAppearanceMedium" />   

    <CheckBox
        android:id="@+id/starCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:button="@android:drawable/btn_star"
        android:focusable="false" 
        />

</RelativeLayout>

这是我的代码

package com.wts.ui;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {        
    public final static int REQUEST_CODE = 1; 

    private WordsDBAdapter dbAdapter;
    private SimpleCursorAdapter dataAdapter;    
    private Button button;
    private EditText editWord;
    private EditText editTranslate;
    private ListView listView;  
    private String selectedWord;    
    private Cursor cursor;

    //context menu
    private final static int IDM_EDIT = 101;
    private final static int IDM_DELETE = 102;

    //options menu
    private static final int IDM_ABOUT = 201;
    private static final int IDM_EXIT = 202;    

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

        dbAdapter = new WordsDBAdapter(this);
        dbAdapter.open();

        button = (Button)findViewById(R.id.buttonAddWord);

        listView = (ListView)findViewById(R.id.listWords);
        displayListView();      
        registerForContextMenu(listView);                       

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Add some words, please",
                        Toast.LENGTH_LONG).show();
            }
        });

        //================ListView onLongClick========================  
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {              
                 cursor = (Cursor)listView.getItemAtPosition(arg2);
                 selectedWord = cursor.getString(0);
                 return false;                                                               
            }
        });         
        //================Button onClick========================                    
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) { 
                editWord = (EditText)findViewById(R.id.editWord);
                editTranslate = (EditText)findViewById(R.id.editTranslate);

                String word = editWord.getText().toString();
                String translate = editTranslate.getText().toString();

                if(word.length() > 0 && translate.length() > 0){
                    dbAdapter.insertWord(word,translate , "noDscrpt");
                    displayListView();

                    editWord.setText("");
                    editTranslate.setText("");
                }
            }
        });                     
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) { 
        if (v.getId() == R.id.listWords) { 
            String[] menuItems = getResources().getStringArray(
                    R.array.contextMenuItems);

            menu.add(Menu.NONE, IDM_EDIT, Menu.NONE, menuItems[0]);
            menu.add(Menu.NONE, IDM_DELETE, Menu.NONE, menuItems[1]);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case IDM_EDIT:

            Intent intent = new Intent(this, EditActivity.class);
            intent.putExtra(getResources().getString(R.string.fstRow),
                    cursor.getString(1));
            intent.putExtra(getResources().getString(R.string.scndRow),
                    cursor.getString(2));
            intent.putExtra(getResources().getString(R.string.thrdRow),
                    cursor.getString(3));

            startActivityForResult(intent, REQUEST_CODE);
            break;
        case IDM_DELETE:
            dbAdapter.deleteWord(selectedWord);
            displayListView();
            break;
        }
        return true;
    }

    @SuppressWarnings("deprecation")
    private void displayListView() {
        Cursor cursor = dbAdapter.fetchAllWords();
        String[] columns = new String[] { 
                WordsDBAdapter.KEY_WORD,
                WordsDBAdapter.KEY_TRANSLATION,             
                };

        int[] to = new int[] {
                R.id.textViewWord,
                R.id.textViewTranslate,             
                };

        dataAdapter = new SimpleCursorAdapter(this, R.layout.word_info, cursor,
                columns, to);

        listView.setAdapter(dataAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);

        String[] menuItems = getResources().getStringArray(R.array.options_menu_items);

        menu.add(Menu.NONE,IDM_ABOUT,Menu.NONE,menuItems[1]);
        menu.add(Menu.NONE,IDM_EXIT,Menu.NONE,menuItems[2]);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item)
    {
        switch(item.getItemId())
        {   
        case IDM_ABOUT:
        {
            Intent intent = new Intent(MainActivity.this,AboutActivity.class);
            startActivity(intent);
            break;
        }       
        case IDM_EXIT:
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            break;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent intent)
    {
        if(resultCode == RESULT_OK && requestCode == REQUEST_CODE)
        {
            if(intent.hasExtra(getResources().getString(R.string.fstRow)))
            {
                dbAdapter.changeValue(
                        selectedWord,
                        intent.getExtras().getString(getResources().getString(R.string.fstRow)),
                        intent.getExtras().getString(getResources().getString(R.string.scndRow)),
                        intent.getExtras().getString(getResources().getString(R.string.thrdRow))
                        );
                displayListView();
            }
        }
    }


}

如何获取包含我已单击的 CheckBox 的 listViewItem ?我需要在 CheckBox 上设置 OnClickListener 吗?但接下来呢?请帮助thnx!

4

1 回答 1

3

您应该创建您的自定义适配器,并且您可以将您的复选框捕获它的 getView 方法

public class CustomListAdapter extends ArrayAdapter<String> {  
    private Activity context;  
    private ListItemRow itemRow;  
    private String[] list;  
    private LayoutInflater layoutInflater;  

    public CustomListAdapter(Activity context, String[] list) {  
       super(context, R.layout.main, list);  
       this.context = context;  
       this.list = list;  
    }  

    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
       View rowView = convertView;  
       if (rowView == null) {  
          itemRow = new ListItemRow();  
          layoutInflater = context.getLayoutInflater();  
          rowView = layoutInflater.inflate(R.layout.word_info, null, true);  

          itemRow.starCheck= (ChechBox) rowView.findViewById(R.id.starCheck);  
          itemRow.textViewTranslate= (TextView) rowView.findViewById(R.id.textViewTranslate);  

       } else {  
          itemRow = (ListItemRow) rowView.getTag();  
       }  

       itemRow.starCheck.setOnClickListener(new OnClickListener() { 
    @Override
    public void onClick(View v) {
              itemRow.textViewTranslate.setText("starCheck clicked");
      }
    });

       return rowView;  
    }  

    private class ListItemRow {  
       private ChechBox starCheck;  
       private TextView textViewTranslate;  
    }  
 } 
于 2012-12-25T09:59:17.070 回答