Listview选择的项目突出显示来自数据库吗?

时间:2019-05-06 13:47:07

标签: java android android-sqlite

我是用Android创建宗教书籍的初学者,我有两个活动都具有listview的数据,当我单击从第一活动切换到第二活动的任何特定项目时,数据来自sqlite数据库,显示第二个活动的所有详细信息,也有listview我的问题如何突出显示第二个列表项,仅突出显示特定项!图片中的示例实际上是我想要的

picture one verse select

picture two verse highlight

如果有人单击第13节,则全部显示1-25节,但仅在下一个活动中突出显示第13节,这是我的代码。

    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.SearchView;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;


    import java.util.ArrayList;

    public class Chapters extends AppCompatActivity {
        private ListView listView;
        private ArrayList<String> stringArrayList;
        private ArrayAdapter<String> adapter;
        private DatabaseHelper mDBHelper;
        private SQLiteDatabase mDb;
        private   int booknumber;
        private  String bookname;
        TextView setbookname;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_chapters);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setbookname=(TextView)findViewById(R.id.bookname);
            Intent mIntent = getIntent();
             booknumber= mIntent.getIntExtra("booknumber", 0);
             bookname=mIntent.getStringExtra("bookname");


             setbookname.setText(bookname);

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

            Toast.makeText(this, ""+booknumber, Toast.LENGTH_SHORT).show();

            setData();

            listView =findViewById(R.id.list);

            adapter = new ChapterAdopter(Chapters.this, R.layout.item_listview, stringArrayList);

            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    int chapternumber=position+1;

                    Toast.makeText(Chapters.this, ""+chapternumber, Toast.LENGTH_SHORT).show();

                    Intent intent=new Intent(Chapters.this,Verse.class);

                  intent.putExtra("Booknumber",booknumber);

                  intent.putExtra("Chapternumber",chapternumber);


               startActivity(intent);
                }
            });

        }



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

            }

            return super.onOptionsItemSelected(item);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            MenuInflater inflater=getMenuInflater();

            inflater.inflate(R.menu.chapter,menu);
            MenuItem menuItem=menu.findItem(R.id.chapter_search);

            SearchView searchView=(SearchView)menuItem.getActionView();



            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String s) {
                    return false;
                }
            });
            return  super.onCreateOptionsMenu(menu);
        }

        private void setData() {



            stringArrayList = new ArrayList<>();

            mDBHelper = new DatabaseHelper(this);
            mDb = mDBHelper.getReadableDatabase();

            Cursor cursor = mDb.rawQuery("select DISTINCT c from t_asv where b="+booknumber, new String[]{});


            if(cursor!=null && cursor.getCount() > 0)
            {
                if (cursor.moveToFirst())
                {
                    do {
                        stringArrayList.add(cursor.getString(0));
                    } while (cursor.moveToNext());
                }
            }
        }


    }



package bible.swordof.God;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class Verse extends AppCompatActivity {
    private ListView listView;
    private ArrayList<String> stringArrayList;
    private ArrayAdapter<String> adapter;
    private DatabaseHelper mDBHelper;
    private SQLiteDatabase mDb;
    private int booknumber;
    private  int chapternumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verse);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        Intent mIntent = getIntent();
         booknumber = mIntent.getIntExtra("Booknumber", 0);
        chapternumber= mIntent.getIntExtra("Chapternumber", 0);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        setData();

        listView = findViewById(R.id.list);

        adapter = new VerseAdopter(Verse.this, R.layout.item_listview, stringArrayList);


        Toast.makeText(this, ""+booknumber, Toast.LENGTH_SHORT).show();

        listView.setAdapter(adapter);


    }


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

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.chapter, menu);
        MenuItem menuItem = menu.findItem(R.id.chapter_search);

        SearchView searchView = (SearchView) menuItem.getActionView();


        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    private void setData() {


        stringArrayList = new ArrayList<>();

        mDBHelper = new DatabaseHelper(this);
        mDb = mDBHelper.getReadableDatabase();

        Cursor cursor = mDb.rawQuery("select  v FROM t_asv where b="+booknumber+" AND c="+chapternumber+";", new String[]{});


        if (cursor != null && cursor.getCount() > 0) {
            if (cursor.moveToFirst()) {
                do {
                    stringArrayList.add(cursor.getString(0));
                } while (cursor.moveToNext());
            }
        }
    }


}

1 个答案:

答案 0 :(得分:0)

似乎您是将所选经文作为章节传递,然后在查询Cursor cursor = mDb.rawQuery("select v FROM t_asv where b="+booknumber+" AND c="+chapternumber+";", new String[]{});中因此从传递的书/章中选择所有经文。

您可能需要将点击的值作为经文以及书籍和章节进行传递,并在WHERE子句中使用包含该经文的查询,也许是cursor = mDb.rawQuery("select v FROM t_asv where b="+booknumber+" AND c="+chapternumber+" AND v=" + versenumber + " ;", new String[]{});