getPosition() не работает хорошо. Я пытаюсь изменить одну строку с получением позиции CursorAdapter

Я пытался в деятельности mAactivity сделать if(position == 3) view.set... но он работает по частям, есть несколько строк, которые становятся черными, а не только строка 3 .

Во втором упражнении я попытался создать цикл, который работал бы только в определенной позиции и что-то менять, но это помогало частично, потому что каждую секунду менялась другая строка.

И я попробовал эти руководства и все виды вещей, и я не нашел ничего, чтобы помочь мне.

1. https://www.youtube.com/watch?v=cNVAhzaLYtw

2. Как я могу обновить одну строку в ListView?

3 . http://www.worldbestlearningcenter.com/tips/Android-listview-update-item.htm

Это: mAtivity:

 public class MActivity extends AppCompatActivity {

 DatabaseHandler mDBHandler;
 ListView mListView;
 SimpleCursorAdapter mSCA;
 MyCustomCursorAdapter mMCA; // <<<<<
 Cursor mCsr;
 Button mDoItButton;
 Context mContext; //<<<<<
 long[] mCheckedIdList; //<<<<<
    @Override
       protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = this;
    mListView = this.findViewById(R.id.lv001); // Get the ListView from it'sid

   mDBHandler = new DatabaseHandler(this); // Get an instance of the Database 
   Handler
    TextView tx = (TextView)findViewById(R.id.textView);
    TextView tx1 = (TextView)findViewById(R.id.textView2);
    TextView tx2 = (TextView)findViewById(R.id.textView3);

    Typeface custom_font = Typeface.createFromAsset(getAssets(),  
      "fonts/MiriamLibre-Bold****f");
    tx.setTypeface(custom_font);
    tx1.setTypeface(custom_font);

    tx2.setTypeface(custom_font);



    if 
     (DatabaseUtils.queryNumEntries(mDBHandler.
    getWritableDatabase(),DatabaseHandler.T
     ABLE_RECORDS) < 1) {}

    // get all rows into a Cursor
    mCsr = mDBHandler.getAllRecords();

    // Prepare a list of the columns to get the data from, for the ListViewt
     String[] columns_to_get_data_from = new String[]{
            DatabaseHandler.KEY_ID,
            DatabaseHandler.KEY_PRICE,
            DatabaseHandler.KEY_ITEM,
            DatabaseHandler.KEY_DETAILS,
            DatabaseHandler.KEY_DATE
     };

    // Prepare a list of the Views into which to place the data
     int[] itemviews_to_place_data_in = new int[]{
            R.id.record_id,
            R.id.Price_TEXT_VIEW,
            R.id.Item_TEXT_VIEW,
            R.id.Details_TEXT_VIEW,
            R.id.Date_TEXT_VIEW
  };

    // get and instance of SimpleCursorAdapter the listviewitem_record layout
   mSCA = new SimpleCursorAdapter(this,
            R.layout.listviewitem_record,
            mCsr,
            columns_to_get_data_from,
            itemviews_to_place_data_in,
            0);
    // Tie the adapter to the Listview
   mListView.setAdapter(mSCA);

    // <<<<<< Alternate Custom Cursor Adapter
  mMCA = new MyCustomCursorAdapter(this,
            R.layout.listviewitem_record,
            mCsr,
            columns_to_get_data_from,
            itemviews_to_place_data_in,
            R.id.record_checkbox1 //<<<<<< id of the checkbox
  );
    // Hijack the Listview
     mListView.setAdapter(mMCA); //<<<<<<



    // <<<<< DO IT BUTTON HANDLER i.e. get list of ID's for checked items
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     toolbar.setTitle(R.string.app_name);
      //toolbar.setSubtitle("Subtitle");
     setSupportActionBar(toolbar);

    toolbar.setNavigationOnClickListener(
            new View.OnClickListener() {
                @Override
  public void onClick(View v) {
                    Intent intent = new Intent( MActivity.this, Graph.class);
                    startActivity(intent);
                }
            }
    );

//        mMCA.InvisibleCheckBOX();
//     refreshListView();
 }

и это адаптер

public class MyCustomCursorAdapter extends CursorAdapter {

private String[] mColumns;
private int[] mViews;
private int mLayout;
private boolean[] mCheckBoxStates;
private int mCheckBoxView;
protected int mCountCheckBox;
private Cursor CC;
private int mLayout1;
private int ii=0;
boolean g = true;
private ListViewCompat List;
int x=0;

// Constructor for the Custom Cursor Adapter
MyCustomCursorAdapter(Context context, int layout, Cursor csr, String[] from_columns, int[] to_views, int checkbox_view, ListViewCompat list) {
    super(context, csr, false);
    mContext = context;
    mLayout = layout;
    mColumns = from_columns;
    mViews = to_views;
    mCheckBoxView = checkbox_view;
    CC = csr;
    List =list;
  }

  @Override
   // Inflate the layout we are going to use (as passed via 2nd parameter)
  public View newView(Context context, Cursor csr, ViewGroup parent) {
    // Initialise an int array for the checkboxes (all will be 0)
    mCheckBoxStates = new boolean[csr.getCount()];
    mCountCheckBox = csr.getCount();

    return LayoutInflater.from(context).inflate(mLayout, parent, false);
  }

  @Override
  // Tie the from_columns to the display views
  public void bindView(View view, Context context, Cursor csr) {

    final Cursor fcsr = csr;
    final Cursor fcsrj = csr;


    // Place the data from the cursor into the respective View (TextView)
    for (int i = 0; i < mColumns.length; i++) {
        ((TextView) 
 view.findViewById(mViews[i])).setText
(csr.getString(csr.getColumnIndex(mColumns[i
])));
    }



    final int position = fcsr.getPosition();
    int restore_cursor_position = fcsr.getPosition();
    fcsr.moveToPosition(position);
    fcsr.moveToPosition(restore_cursor_position);
    if (position==3) {
  view.setBackgroundColor(Color.BLACK);
   }



    // Set the checkbox (note should be false, unless otherwise changed)
    CheckBox currentCheckBox = (CheckBox) view.findViewById(mCheckBoxView);
    currentCheckBox.setChecked(mCheckBoxStates[csr.getPosition()]);
    currentCheckBox.setTag(new 
Long(csr.getLong(csr.getColumnIndex(DatabaseHandler.KEY_ID))));


    currentCheckBox.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener() {
        final int position = fcsr.getPosition();

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
isChecked) {
            mCheckBoxStates[position] = ((CheckBox) buttonView).isChecked();
            Log.i("s", "A" + position);
            int restore_cursor_position = fcsr.getPosition();
            fcsr.moveToPosition(position);
            fcsr.moveToPosition(restore_cursor_position); //restore the 
     Cursor's position
        }
    });

0 ответов

Другие вопросы по тегам