以编程方式滚动到Android ListView中的特定位置并更改背景颜色

时间:2019-06-08 10:46:47

标签: java android android-listview

您好朋友,我是Android的新朋友,使用setSelection(1)重定向到列表视图中的特定位置。我的问题是,如何更改特定项目位置的背景颜色,并且还希望获取此特定列表视图项目中存在的文本。这是我的代码,为此我使用了什么逻辑,我知道可以通过这种方法实现

mylistview.setOnItemClickListener(); 

但是我该如何使用setSelection()方法实现这一目标?

package bible.swordof.God;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import es.dmoral.toasty.Toasty;

public class ALLVERSE extends AppCompatActivity  implements NavigationView.OnNavigationItemSelectedListener {
    private ListView mylistview;
    private ArrayList<String>versenumber;
    private ArrayList<String>verselist;
    private ArrayList<String>id;
    private  ArrayList<String>refernce;
    private DatabaseHelper mDBHelper;
    private SQLiteDatabase mDb;
    private int booknumber;
    private  int chapternumber;
    private  String bookname;
    private TextView booknametitle;
    private  FullverseAdopter fullverseAdopter;



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



        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        booknametitle=findViewById(R.id.bookname);

        Intent mIntent = getIntent();
        booknumber = mIntent.getIntExtra("Boooknumber",0);
        chapternumber= mIntent.getIntExtra("Chapternumber", 0);
        bookname=mIntent.getStringExtra("Bookname");

        booknametitle.setText(bookname.toString() +"   "+  chapternumber);


        //Toast.makeText(this, ""+bookname, Toast.LENGTH_SHORT).show();
        setSupportActionBar(toolbar);
        toolbar.setTitle("ALL VERSE");


        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        setData();

        mylistview=findViewById(R.id.mylistview);


        BaseAdapter baseAdapter=new BaseAdopter(this,versenumber,id,verselist,refernce);


        mylistview.setAdapter(baseAdapter);

      mylistview.setSelection(3);


    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();

        }

        return super.onOptionsItemSelected(item);
    }




    private void setData() {
        versenumber=new ArrayList<>();
        verselist=new ArrayList<>();
        refernce=new ArrayList<>();
        id=new ArrayList<>();

        mDBHelper = new DatabaseHelper(this);
        mDb = mDBHelper.getReadableDatabase();
        Cursor cursor = mDb.rawQuery("SELECT id, v, t from t_kjv where b="+booknumber+" AND c="+chapternumber+";", new String[]{});
        if(cursor!=null && cursor.getCount() > 0)
        { if (cursor.moveToFirst())
        {
            do {
                id.add(cursor.getString(0));
                versenumber.add(cursor.getString(1));
                verselist.add(cursor.getString(2));

                refernce.add(bookname+" "+chapternumber);

            }

            while (cursor.moveToNext());
        }
        }
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        Fragment fragment;
        int id = item.getItemId();

        if (id == R.id.home) {

            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);

        } else if (id == R.id.favoruite)
        { Intent intent=new Intent(this,Favourite.class);
            startActivity(intent);
        }  else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}

0 个答案:

没有答案