单击复选框时更改ListActivity中的TextView(使用CursurAdapter)

时间:2014-03-15 13:35:47

标签: android listview checkbox oncheckedchanged

我的问题是,当我切换一个复选框(它是ListView的一部分)时,我无法解决将TextView的外观更改到正确位置的问题。 目标是STRIKE_THRU文本和(当下一步)在检查CheckBox时将更改后的状态返回给数据库。

每次更改第一行时,单击列表的复选框。如何更改代码,在受影响的行上获取STRIKE_THRU文本? (我刚开始使用Android)

重要的是,当点击TextView时,我仍然可以启动另一个活动来解释细节。

ListActivity的代码

import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

public class TaskListActivity extends ListActivity implements OnCheckedChangeListener {

private Cursor dbCursor;
private TaskAdapter listAdapter;
String lUID;
CheckBox cb;
TextView textview1, textview2;
ContentValues values;
ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    lUID = intent.getStringExtra("List");
    Uri uri = Uri.withAppendedPath(Provider.CONTENT_URI_TASKSLIST, lUID);
    dbCursor = getContentResolver().query(uri,
            null, null, null, OpenHandler.TASKS_NAME + " DESC");

startManagingCursor(dbCursor);

listAdapter = new TaskAdapter(this, dbCursor);
setListAdapter(listAdapter);

lv=getListView();
lv.setItemsCanFocus(true);
lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO
        Log.d("Position", "Test: " + position);
        //works fine    
        }
    });



    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int pos = (Integer)buttonView.getTag();
        //Gives the correct position of the clicked CheckBox
        System.out.println("Pos ["+pos+"]");

        if (pos != ListView.INVALID_POSITION) {

         CheckBox cb = (CheckBox) buttonView.findViewById(R.id.checkbox);
         textview1 = (TextView) findViewById(R.id.text1);
         textview2 = (TextView) findViewById(R.id.text2);

         if(cb.isChecked()==true){
            cb.setChecked(true);
            textview1.setTextColor(Color.GRAY);
            textview1.setPaintFlags(textview1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            textview2.setTextColor(Color.GRAY);
            textview2.setPaintFlags(textview2.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        }
         }

   }
}

适配器代码

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CursorAdapter;
import android.widget.TextView;



public class TaskAdapter extends CursorAdapter {


private LayoutInflater inflator;
private int ciTasksList, ciTasksName;
private Context context;
private Cursor c;
private CheckBox cb;


public TaskAdapter(Context context, Cursor c) {
    super(context, c);
    this.c = c;
        this.context = context;

    inflator = LayoutInflater.from(context);
    ciTasksName = c.getColumnIndex(OpenHandler.TASKS_NAME);
    ciTasksList = c.getColumnIndex(OpenHandler.TASKS_LIST);
} 

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

    if (!c.moveToPosition(position)) {
        throw new IllegalStateException("couldn't move cursor to position " + position);
    }
    View v;
    if (convertView == null) {
        v = newView(context, c, viewGroup);
    } else {
        v = convertView;
    }
    bindView(v, context, c);
    cb = (CheckBox) v.findViewById(R.id.checkbox);
    cb.setTag(position);
    cb.setOnCheckedChangeListener((TaskListActivity) context);

    return v;


}


@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView textview1 = (TextView) view.findViewById(R.id.text1);
    TextView textview2 = (TextView) view.findViewById(R.id.text2);
    String TasksName = cursor.getString(ciTasksName);
    String TasksList = cursor.getString(ciTasksList);

    textview1.setText(TasksName);
    textview2.setText("ID " + TasksList);

} 

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return inflator.inflate(R.layout.check_text_text, null);
}
}

和布局代码:     

<CheckBox android:id="@+id/checkbox"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:layout_width="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_height="fill_parent"/>         

<TextView android:id="@+id/text1"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_marginTop="4dip"
    android:layout_toRightOf="@id/checkbox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<TextView android:id="@+id/text2"
    android:textAppearance="?android:attr/textAppearanceSmall"  
    android:layout_below="@id/text1"
    android:layout_alignLeft="@id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</RelativeLayout>

修改 而不是

cb = (CheckBox) v.findViewById(R.id.checkbox);
cb.setTag(position);
cb.setOnCheckedChangeListener((TaskListActivity) context);

在Adapter类中我改为(非常感谢erdomester):

 cb = (CheckBox) v.findViewById(R.id.checkbox);
 cb.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
       cb = (CheckBox) view.findViewById(R.id.checkbox);
       textview1 = (TextView) v.findViewById(R.id.text1);
       textview2 = (TextView) v.findViewById(R.id.text2);

       if(cb.isChecked()==true){
         cb.setChecked(true);
         textview1.setTextColor(Color.GRAY);
         // [...]
       }
    }
 });

取代&#34; onClick&#34;来自前者&#34; onCheckedChanged&#34;活动。

EDIT2

这仍然不是预期的结果。我没有进行第一次测试的是,它现在只适用于最后一行。 (删除cb = (CheckBox) view.findViewById(R.id.checkbox);中的onClick时的结果相同)。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如何放弃onCheckedChangeListener()并在setOnClickListener()功能中使用getView()? (请记住将其从类启动行中删除)。

checkBox.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        final boolean isChecked = checkBox.isChecked();
        // Do something here.
    }
});