在基于listview的edittext输入字段中突出显示单个项目的颜色

时间:2018-06-14 07:43:36

标签: android listview

我是android新手。 在我的项目中,我显示了“列表视图”。 当我点击按钮,我正在编辑文本值然后我想匹配“列表视图”中的任何一项我需要突出显示该项目的颜色。 如何突出显示特定列表项?谢谢

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_scan:
                AlertDialog.Builder mBuilder = new AlertDialog.Builder(PurchaseOrderLineItemActivity.this);
                mBuilder.setCancelable(false);
                View mView = getLayoutInflater().inflate(R.layout.scan_popup, null);
                final EditText upc_edit = (EditText) mView.findViewById(R.id.upc_text);
                final ImageView mImageView = (ImageView) mView.findViewById(R.id.imageView_close);
                Button mLogin = (Button) mView.findViewById(R.id.upc_scan);
                mBuilder.setView(mView);
                final AlertDialog dialog = mBuilder.create();
                dialog.show();
                mLogin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                     String result = upc_edit.getText().toString();
                     String item_result,Received_items;
                     boolean no_upc_match = true;

                     for(ItemLineList itemLineLists : List) {
                            item_result =  itemLineLists.getItem_barcode();
                            if(result.equals(itemLineLists.getItem_barcode()) ) {
                                 Received_items = itemLineLists.getScan_item_quantity();
                                int present_value_int = Integer.parseInt(Received_items);
                                present_value_int++;
                                String final_received_items = String.valueOf(present_value_int);
                                itemLineLists.setScan_item_quantity(final_received_items);
                                adapter.notifyDataSetChanged();
                                dialog.dismiss();
                                no_upc_match = false;
                                 break;
                            }
                            else
                            {
//                                Toast.makeText(getApplicationContext(), "no match" , Toast.LENGTH_SHORT).show();
                            }
                        }

1 个答案:

答案 0 :(得分:0)

  int i = 0; 
  for(ItemLineList itemLineLists : List)
   {
     .
     .
     .
      if(result.equals(itemLineLists.getItem_barcode()) )
        {
           getView(i,listView).setBackgroundColor(R.color.red);                 
        }
      else
       {

       }
     i += 1;
  }

<强> Function to Get View:-

public View getView(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}