Android 如何在没有onClick的情况下突出显示ListView中的行?

Android 如何在没有onClick的情况下突出显示ListView中的行?,android,listview,Android,Listview,我需要自动高亮显示ListView中的一行,而不需要单击。我的应用程序所做的是,当食品到期日低于90天时,对特定食品项目发出警报(突出显示)。我已经搜索了Stack Overflow以寻找解决方案,但所有突出显示都是使用onClick实现的。我已经为我的ListView实现了onClick突出显示(这不是我想要的),当食品到期日达到90天以下时,有没有办法自动实现突出显示?任何帮助都将不胜感激 当我单击产品Milo时,会出现突出显示,如何使突出显示自动显示在90天内过期的食品上 这是我的XML

我需要自动高亮显示ListView中的一行,而不需要单击。我的应用程序所做的是,当食品到期日低于90天时,对特定食品项目发出警报(突出显示)。我已经搜索了Stack Overflow以寻找解决方案,但所有突出显示都是使用onClick实现的。我已经为我的ListView实现了onClick突出显示(这不是我想要的),当食品到期日达到90天以下时,有没有办法自动实现突出显示?任何帮助都将不胜感激

当我单击产品Milo时,会出现突出显示,如何使突出显示自动显示在90天内过期的食品上

这是我的XML布局

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_above="@+id/AddProduct"
        android:id="@+id/listView2"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:choiceMode="multipleChoice"
        android:listSelector="@drawable/Selector"
        />

我的Java文件

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inventory);
    AddProduct = (Button) findViewById(R.id.AddProduct);
    camera = (ImageButton) findViewById(R.id.camera);
    AddProduct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Inventory.this, AddProduct.class));
        }
    });
    camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), BarcodeCaptureActivity.class);
            startActivityForResult(intent, BARCODE_READER_REQUEST_CODE);

        }
    });

    mList = (ListView) findViewById(R.id.listView2);
    arrayAdapter = new ItemsAdapter(this, android.R.layout.simple_list_item_1, items);
    mList.setAdapter(arrayAdapter);
    database = FirebaseDatabase.getInstance().getReference("All Barcodes");
    final List<String> keys = new ArrayList<>(); // will contain list of keys corresponding to listview item
    database.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(final DataSnapshot dataSnapshot) {
            arrayAdapter.clear();
            for(final DataSnapshot snapshot : dataSnapshot.getChildren()){
                arrayAdapter.notifyDataSetChanged();

                Calendar c = Calendar.getInstance();
                SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                String dateNow = df.format(c.getTime());
                String exp = snapshot.child("expiration").getValue().toString();
                String barcode = snapshot.child("barcode").getValue().toString();
                String name = snapshot.child("pname").getValue().toString();
                String q =  snapshot.child("quantity").getValue().toString();

                Date d1 = null;
                Date d2 = null;
                try{
                    d1 = df.parse(exp);
                    d2 = df.parse(dateNow);
                }
                catch(Exception e){
                }

                long diff =  d1.getTime() - d2.getTime();
                long hours = diff/(60*60*1000);
                long days = hours/24;

                if (days<=90){
                    NotificationGenerator.openActivityNotification(getApplicationContext(),barcode, name, days);
                    Log.i( "Item expring in: " , days + " days");
                    Log.i("Item's barcode is " , barcode);



                }
                else{
                    Log.i("Item expring in: " , days + " days");
                }

                String value = (String) snapshot.child("expiration").getValue();

                items eachItem = new items(name,value.toString(),Long.toString(days),q);

                arrayAdapter.add(eachItem);

                keys.add(snapshot.getKey());


            }
            arrayAdapter.notifyDataSetChanged();

            mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {

                    view.setSelected(true);
                    String myKey = keys.get(i);
                    Intent intent  = new Intent(Inventory.this,Edit.class);
                   intent.putExtra("value",myKey);
                   startActivity(intent);
                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory);
AddProduct=(按钮)findViewById(R.id.AddProduct);
摄像头=(图像按钮)findViewById(R.id.camera);
AddProduct.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
startActivity(新意图(Inventory.this、AddProduct.class));
}
});
camera.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent Intent=new Intent(getApplicationContext(),BarcodeCaptureActivity.class);
startActivityForResult(目的、条形码阅读器、请求码);
}
});
mList=(ListView)findViewById(R.id.listView2);
arrayAdapter=newItemsAdapter(这是android.R.layout.simple\u list\u item\u 1,items);
mList.setAdapter(阵列适配器);
database=FirebaseDatabase.getInstance().getReference(“所有条形码”);
final List keys=new ArrayList();//将包含与listview项对应的键列表
database.addValueEventListener(新的ValueEventListener(){
@凌驾
公共无效onDataChange(最终数据快照数据快照){
arrayAdapter.clear();
对于(最终DataSnapshot快照:DataSnapshot.getChildren()){
arrayAdapter.notifyDataSetChanged();
Calendar c=Calendar.getInstance();
SimpleDataFormat df=新的SimpleDataFormat(“dd/MM/yyyy”);
字符串dateNow=df.format(c.getTime());
字符串exp=snapshot.child(“到期”).getValue().toString();
字符串条形码=snapshot.child(“条形码”).getValue().toString();
字符串名称=snapshot.child(“pname”).getValue().toString();
字符串q=snapshot.child(“数量”).getValue().toString();
日期d1=空;
日期d2=空;
试一试{
d1=df.parse(exp);
d2=df.parse(dateNow);
}
捕获(例外e){
}
long diff=d1.getTime()-d2.getTime();
长时间=差异/(60*60*1000);
长日=小时/24;

如果(天您应该在添加到期日时在适配器中执行此操作,检查到期日是否超过90天,然后给出您想要的颜色。

您应该使用xml选择器为列表项状态的
列表视图的每个项使用背景。下面给出了完整的代码片段-

ListView
项的布局
Layout/Layout\u ListView\u item.xml
-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="@drawable/listview_item_bg">
        <TextView       android:id="@+id/textForList"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:padding="10sp"  />
.
.
.
</LinearLayout>
并将
layout\u listview\u item.xml
添加到适配器的
getView()方法上的
listview

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

        View v = convertView;

        // first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.

        if (v == null) {

            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            //inflating the tex views from list_items.xml
            v = inflater.inflate(R.layout.layout_listview_item, null);
        }

.
.
.
.
.

    }

您的项目对象可能有一个字段引用过期日期(如果达到90天以下),因此在适配器中的(get view)方法中,您需要在项目数组中的每个项目对象中检查该字段。如果该字段达到90天以下,则更改该项目列表的背景色,使其高亮显示

    <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true">
    <shape>
        <solid android:color="#66FFFFFF" />
    </shape>
</item>
    <item>
    <shape>
        <solid android:color="#FF666666" />
    </shape>
</item>

</selector>
 @Override
    public View getView(int position,  View convertView, ViewGroup parent) {

        View v = convertView;

        // first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.

        if (v == null) {

            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            //inflating the tex views from list_items.xml
            v = inflater.inflate(R.layout.layout_listview_item, null);
        }

.
.
.
.
.

    }